Search code examples
javaswingcomponentsjlabeljcheckbox

accessing swing component of other class


I have two classes mainpanel.java and subpanel.java. The subpanel.class contains a checkbox and some labels. I want to change the setSelected() and setText() of these components when i click some buttons in the mainpanel.java .

I have created a method in subpanel.java which i call from mainpanel.java and pass the boolean values.

public void schedulerchange(boolean check){
        System.out.println("checked"+check);
        scheduleenabler.setEnabled(check);
        scheduleenabler.setSelected(check);
        scheduleinfo.setText("Scheduler in On");
        //subpanel21.updateUI();
    }

When i call this function from mainpanel.java the function is called but the values don't change unless i make jcheckbox and jlabel static. But from what i learned we should not use static components unless very necessary. Is there some other way to change the components?


Solution

  • If I have understood your question then I think you want to write a separate ActionListener class and perform action there which will enable or disable the JCheckBox in the UI-class. The below code shows that. Pass your checkbox reference to that PerformAction class and make it enabled or disabled by clicking on the button.

    import java.awt.EventQueue;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    public class MainClass {
    
     MainClass() {
        JFrame jfrm = new JFrame("JTable Demo");
        jfrm.setLayout(new FlowLayout());
        jfrm.setSize(460, 180);
        jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        JCheckBox check = null;
        // Get the Panel from the subclass;
        JPanel panel = new CheckBox().getCheckBoxPanel();
    
        // From the compoenents present in the panel get the CheckBox compoenent.
        for(int i = 0; i < panel.getComponentCount(); i++) {
           if(panel.getComponent(i) instanceof JCheckBox) {
            check = (JCheckBox) panel.getComponent(i);
            }
        }
    
        JButton button = new JButton("Click");
    
        // Pass the CheckBox Compoenent to the ActionListener.
    button.addActionListener(new PerformAction(check));
    
        jfrm.add(button);
        jfrm.add(panel);
        jfrm.setVisible(true);
     }
    
      public static void main(String args[]) {
      EventQueue.invokeLater(new Runnable() {
    
          @Override
          public void run() {
              new MainClass();
          }
      });
     }
    }
    
    class PerformAction implements ActionListener {
    
    JCheckBox check = null;
    public PerformAction(JCheckBox checkBox) {
        check = checkBox;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        boolean checkStatus = check.isSelected();
        if(checkStatus == true) {
            check.setEnabled(false);
            check.setSelected(false);
        } else {
            check.setEnabled(true);
            check.setSelected(true);
          }
    }
    }
    
     class CheckBox {
    public JPanel getCheckBoxPanel() {
        JPanel checkPanel = new JPanel();
        JCheckBox check = new JCheckBox();
        checkPanel.add(new JLabel("CheckBox"));
        checkPanel.add(check);
    
        return checkPanel;
    }
    }