Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If I have understood your question then I think you want to write a separate <code>ActionListener</code> class and perform action there which will enable or disable the <code>JCheckBox</code> in the UI-class. The below code shows that. Pass your checkbox reference to that <code>PerformAction</code> class and make it enabled or disabled by clicking on the button.</p> <pre><code>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 &lt; 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; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload