Search code examples
javaswingarraylistactionlistenerjcheckbox

How can I enable/disable a JTextField with a JCheckBox? or what's wrong with my code?


I'm newbie in programming java, I have an array of JCheckBox next to an array of JTextfield.

I need to make a CheckBox Deactivate a JTextField when it's checked, but I don't have success in this

How can I make it works with actionlisteners?

This is my code:

public class Checklist_Complete extends JFrame {

    private JLabel      description;
    private JButton     send;
    private JTextField  text[]=new JTextField[10];
    private JCheckBox   cb[]=new JCheckBox[10];

    public Checklist_Complete() {

        setTitle("Activities");
        setSize(500,300);
        setupWidgets();
        setVisible(true);       
    }

    private void setupWidgets() {
        JPanel  pn_center   = new JPanel(new GridLayout(10,1));
        JPanel  pn_west     = new JPanel(new GridLayout(10,1));

        description     = new JLabel("List your activities and uncheck the irrelevant ones");
        send            = new JButton("Send Checklist");

        for (int i=0; i<10; i++) {
            text[i]  = new JTextField();
            cb[i]    = new JCheckBox("", true);
        }

        add(description, BorderLayout.NORTH);
        add(pn_center, BorderLayout.CENTER);
        add(pn_west, BorderLayout.WEST);
        add(send, BorderLayout.SOUTH);

        for (int i=0; i<10; i++){

            pn_center.add(text[i]);
            pn_west.add(cb[i]);
        }
    }

        private void setupEvents() {

            setDefaultCloseOperation(EXIT_ON_CLOSE);

            for (int i=0; i<10; i++) {

                cb[i].addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent event) {
                        if(cb[i].isSelected()){
                            text[i].setEnabled(false);
                        } else{
                            text[i].setEnabled(true);
                        }
                    }
                });
            }
        }

    public static void main(String[] args) {
        new Checklist_Complete();
    }
}

Solution

  • Here is a quick solution with an ItemListener.

     private void setupEvents() {
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            for (int i=0; i<10; i++) {
                final int finalI = i;
                cb[i].addItemListener(new ItemListener() {
                    public void itemStateChanged(ItemEvent e) {
                        text[finalI].setEnabled(e.getStateChange() == ItemEvent.SELECTED);
                    }
                });
            }
        }
    

    You can also do it with an ActionListener but the solution is a bit of a hack, and it is not as elegant. I am posting this because you can solve your issue like this also:

     private void setupEvents() {
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            for (int i=0; i<10; i++) {
                final int finalI = i;
                cb[i].addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        text[finalI].setEnabled(!text[finalI].isEnabled() && e.getID() == ActionEvent.ACTION_PERFORMED);
                    }
                });
            }
        }