I'm trying to show a dialog message if the user clicks a button and a text field is empty.

Action listener -

        if(event.getSource() == cmdSaveSettings) 
        if(txtPlayerOneName.getText().equals(emptyText))        
        {
             JOptionPane.showMessageDialog(null, "alert", "Please enter player one's name",                                  JOptionPane.ERROR_MESSAGE);
        }

Ive add an action to listener to the button and the textfield:
(mouse listener also as removes original text on click which works fine)

cmdSaveSettings = new JButton("Save Settings");
        this.cmdSaveSettings.addActionListener(this);

        txtPlayerOneName = new TextField("Player One", 15);
        this.txtPlayerOneName.addMouseListener(this);
        this.txtPlayerOneName.addActionListener(this);

It might help if you added brackets to the first if statement. (In red)

Action listener -

        if(event.getSource() == cmdSaveSettings) {
        if(txtPlayerOneName.getText().equals(emptyText))        
        {
             JOptionPane.showMessageDialog(null, "alert", "Please enter player one's name",                                  JOptionPane.ERROR_MESSAGE);
        }
}

No that's not it...

I would put in some code at the top of the actionPerformed function that displays a message saying the program is inside actionPerformed. Or you can add a breakpoint and do the same thing. If it doesn't get inside the actionPerformed function, then the problem is not there. If it does, put some code to display that the event source is indeed cmdSaveSettings. If it isn't, again the problem is probably outside of actionPerformed. Then narrow it down some more to see if it gets in the code belonging to this if statement:

if(txtPlayerOneName.getText().equals(emptyText))

Sorry I don't understand, message saying program is inside ActionPerformed?

From what I got from that was

JOptionPane.showMessageDialog(add.ActionListener.this)

but thats not even the correct syntax...Sorry I'm new to this, could you explain in a bit more detail?

Something like this:

public void actionPerformed(ActionEvent event)  
{
     System.out.println ("In actionPerformed");
     if(event.getSource() == cmdSaveSettings) 
     {
            System.out.println ("Source is cmdSaveSettings");
            if(txtPlayerOneName.getText().equals(emptyText))        
            {
                  System.out.println ("Inside if statement");
                  JOptionPane.showMessageDialog(null, "alert", "Please enter player one's name",  
                          JOptionPane.ERROR_MESSAGE);
            }
     }
}

Depending on which, if any, of these statements display as output when you press the button, you have a better handle on what is happening and what the problem is.

Thanks, I got source is cmdSaveSettings so that means theres something wrong with my second if statement?

I've solved it, never mind! Thanks anyway - The empty text was set to null so I just changed null to "" and it worked. I wish the java website said that it made a difference!

Well, equals compares the String values (the String equals method that is, of course) of the referenced Objects. The String value of the null reference is "null" not "". Keep that in mind. ;-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.