Search code examples
javaswingbooleanjtextfield

Get boolean from a JTextField


I am trying to take the values from the text fields below to use with parent.addNewRoom(roomNo,roomEnSuite); but roomEnSuite is a Boolean value in the parent class. What is the correct procedure to get a Boolean from a JTextField?

public void actionPerformed( ActionEvent ae)     
    {
        String item = ae.getActionCommand(); 

        if ( item.equals("Confirm"))         
        {
            String roomNo = nameJTextField.getText();
            String roomEnSuiteS = idJTextField.getText();
            parent.addNewRoom(roomNo,roomEnSuite);
            this.dispose();
        }
        else if ( item.equals("Cancel"))        
        {
        parent.resetButtons();
        this.dispose();
    }
}

Solution

  • To give a full answer from my above comments:

    Handling boolean input using a JTextField would not be a good way to go about things as there are many variations the user could type yes/no/true/false, etc. mispelling?

    Using a JRadioButton (for single answers) or JCheckbox (for multiple answers) would be a better way to go about handling true or false input. I would suggest a JRadioButton as you wouldn't want the user checking true and false.

    http://docs.oracle.com/javase/tutorial/uiswing/components/button.html