wam090
wam090

Reputation: 2873

GUI... Why isnt my button responding to the first if and always heading to the next option

After the password is entered i want the window to disappear and pop a new window.

JButton btnEnter = new JButton("Enter");

    btnEnter.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) 
        {

            if(passwordField.equals("test"))
            {   
              frame.setVisible(false);

            }

            else if(!passwordField.equals("test"))
            {
                JOptionPane.showMessageDialog(null,"Access Denied!!");
            }

        }
    });
    btnEnter.setBounds(149, 184, 117, 29);
    frame.getContentPane().add(btnEnter);

Upvotes: 0

Views: 80

Answers (2)

dann.dev
dann.dev

Reputation: 2494

I'm assuming passwordField is a JTextField, if so, you need to get the text from it, just .getText() I think and store that in a string. Then test the string. At the moment you are testing if your JTextField equals the string.

Upvotes: 4

Eng.Fouad
Eng.Fouad

Reputation: 117597

Create 2 JFrames and make a reference for each one:

JFrame oldFrame = new JFrame();
// ...

JFrame newFrame = new JFrame();
// ...

// ...

if(passwordField.equals("test"))
{   
    oldFrame.setVisible(false);
    newFrame.setVisible(true);
}

Upvotes: 3

Related Questions