0

I'm currently trying to add an event to a JTextField, depending on two checkboxes, but it seems that it's not working properly.

enter image description here

txKids is the JTextField that I want to modify according to the status of those two checkboxes, cbChildrenY and cbChildrenN.

This is the code that I have for those components;

private JCheckBox getCbChildrenY() {
    if (cbChildrenY == null) {
        cbChildrenY = new JCheckBox("Children (Y)");
        cbChildrenY.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                showMansions();
            }
        });
        cbChildrenY.setSelected(true);
    }
    return cbChildrenY;
}

    private JCheckBox getCbChildrenN() {
    if (cbChildrenN == null) {
        cbChildrenN = new JCheckBox("Children (N)");
        cbChildrenN.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                showMansions();
            }
        });
        cbChildrenN.setSelected(true);
    }
    return cbChildrenN;
}

    private JTextField getTxKids() {
    if (txKids == null) {
        txKids = new JTextField();
        txKids.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if ((getCbChildrenN().isSelected() == true) && (getCbChildrenY().isSelected() == false)){
                    getTxKids().setEnabled(false);
                    getTxKids().setEditable(false);
                }
            }
        });
        txKids.setColumns(10);
        txKids.setBounds(203, 350, 78, 20);
    }
    return txKids;
}

Hope you can help me a bit, thanks in advance.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Niconoid
  • 107
  • 2
  • 12
  • Are you trying to make txKids disabled & uneditable, when cbChildrenN is ckhecked and cbChildrenY is not checked? – 4J41 Dec 27 '13 at 19:24
  • What is your specific problem? What works, what doesn't? – Hauke Ingmar Schmidt Dec 27 '13 at 19:27
  • I'm trying to make txKids disabled mainly, I just added uneditable to check, when cbChildrenN is checked (No kids allowed) and cbChildrenY is hot checked (Kids allowed). The problem is that txKids is still enabled when the checkboxes are in that condition. – Niconoid Dec 27 '13 at 19:31
  • It will only be disabled when you perform some action on txKids. – 4J41 Dec 27 '13 at 19:34
  • If I change getTxKids() by txKids, the problem persists, that's another test thing that I've done before. What would be an option so whenever the checkboxes are in those states, the textfield puts itself disabled without performing any action? – Niconoid Dec 27 '13 at 19:37

1 Answers1

1

You should handle that operation when you click the check boxes. See my code below.

private JCheckBox getCbChildrenY() {
    if (cbChildrenY == null) {
        cbChildrenY = new JCheckBox("Children (Y)");
        cbChildrenY.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                showMansions();
                handleTxKids();
            }
        });
        cbChildrenY.setSelected(true);
    }
    return cbChildrenY;
}
private JCheckBox getCbChildrenN() {
    if (cbChildrenN == null) {
        cbChildrenN = new JCheckBox("Children (N)");
        cbChildrenN.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                showMansions();
                handleTxKids();
            }
        });
        cbChildrenN.setSelected(true);
    }
    return cbChildrenN;
}

private JTextField getTxKids() {
    if (txKids == null) {
        txKids = new JTextField();
        txKids.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //do something.
            });
        txKids.setColumns(10);
        txKids.setBounds(203, 350, 78, 20);
    }
    return txKids;
}
private void handleTxKids() {
    if ((getCbChildrenN().isSelected() == true) && (getCbChildrenY().isSelected() == false)){
        getTxKids().setEnabled(false);
   } else {
        getTxKids().setEnabled(true);
   }
}
4J41
  • 5,005
  • 1
  • 29
  • 41
  • Thanks a lot Ajai! So the problem was that the condition had to be inside the checkboxes, and not inside text field :) – Niconoid Dec 27 '13 at 19:42