Search code examples
javaswingjtextfieldjcheckbox

JTextField wont work properly


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.


Solution

  • 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);
       }
    }