Search code examples
javajoptionpane

JOptionPane cancel button and getting input


I'm trying to get the user to input there name if it is left blank it will ask again, if they fill it out it sets a JLabel or hit cancel to get out.
My last if statement is wrong it does not like nameEnt.

public Player() {
    //setBackground(Color.green);
    setSize(600, 400);
    name = new JLabel();//Input hint
    JOptionPane nameOption = new JOptionPane();
    String nameEnt = nameOption.showInputDialog("First Name: ");
    if (!nameEnt.matches("[a-zA-Z]+")) {
        name.setText(nameEnt);
    }
    if (nameEnt.length() == 0) {
        //if this condition is true JOption stays until name is entered or canceled 
    }
    if (nameEnt == nameOption.CANCEL_OPTION) {
        System.exit(0);
    }
}

Solution

  • As per the JOptionPane API, if the user cancels the dialog, null is returned.

    And so the correct solution is to to not to use equals, but rather to check the return value for null and to do this first, before checking its length.

    public Player() {
        //setBackground(Color.green);
        setSize(600, 400);
        name = new JLabel();//Input hint
        JOptionPane nameOption = new JOptionPane();
        String nameEnt = nameOption.showInputDialog("First Name: ");
        if (nameEnt == null) {
            // user canceled. get out of here. 
            System.exit(0);
    
            // or return;  
            // or throw some exception
        }
        if (!nameEnt.matches("[a-zA-Z]+")) {
            name.setText(nameEnt);
        }
        if (nameEnt.length() == 0) {
            //if this condition is true JOption stays until name is entered or canceled 
        }
        // if (nameEnt == nameOption.CANCEL_OPTION) {
           //  System.exit(0);
        // }
    }
    

    But why are you creating a JOptionPane this way? Better to use the static method of creation.

    // don't use null as the first parameter if the GUI is already showing
    String nameEnt = JOptionPane.showInputDialog(null, "First Name: ");
    if (nameEnt == null) {
        // user canceled. get out of here. 
        System.exit(0);
    }
    

    Or maybe something like this, if you're trying to loop to get input:

    public Player() {
        setSize(600, 400);  // This is not good to do. Ask for details and I'll tell.
    
        name = new JLabel();// Don't forget to add this to the GUI!
    
        String nameEnt = "";
        while (nameEnt.trim().isEmpty()) {
            // if the GUI is already showing, pass a component from it as the first param here, not null
            nameEnt = JOptionPane.showInputDialog(null, "First Name: ");
            if (nameEnt == null) {
                // user canceled. get out of here. 
                System.exit(0);
    
                // or return;  
                // or throw some exception
            } else if (!nameEnt.matches("[a-zA-Z]+")) {
                name.setText(nameEnt);
            } else {
                // set it to "" so that we keep looping
                nameEnt = "";
            }
        }
    }