anuj
anuj

Reputation: 7

What's wrong with this action performed?

I have three buttons,out of three two are working fine ,but when I click on third (btn_Newuser) it does not respond? here is the code

if (e.getSource().equals(btn_cancel)) {
    System.exit(0);
} else if (e.getSource().equals(Btn)) {
    if (tf_Fname.getText().trim().length() == 0 && tf_Lname.getPassword().length == 0) {
        JOptionPane.showMessageDialog(null, "Text Fields cannot be blank! ", "Blank", JOptionPane.WARNING_MESSAGE);
    } else {
        try {
            selectfromdb();
        } catch (SQLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } finally {
            if (e.getSource().equals(btn_Newuser)) {
                System.out.println("You have clicked on" + btn_Newuser);
                new Newuser();

            }
        }
    }

}

Upvotes: 0

Views: 136

Answers (2)

John3136
John3136

Reputation: 29266

Your condition in the finally cannot be true.

Your code boils down to this.

else if (e.getSource().equals(Btn)) {
    ...
    // This can never happen because you are in the getSource == Btn block.
    if (e.getSource().equals(btn_Newuser)) {
}

The second if cannot be true unless Btn and btn_Newuser are the same.

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347244

btn_NewUser will never be true because your are trying to compare it within the Btn if branch

I "think" you want something more like...

if (e.getSource().equals(btn_cancel)) {
    System.exit(0);
} else if (e.getSource().equals(Btn)) {
    if (tf_Fname.getText().trim().length() == 0 && tf_Lname.getPassword().length == 0) {
        JOptionPane.showMessageDialog(null, "Text Fields cannot be blank! ", "Blank", JOptionPane.WARNING_MESSAGE);
    } else {
        try {
            selectfromdb();
        } catch (SQLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } finally {
        }
    }
} else if (e.getSource().equals(btn_Newuser)) {
    System.out.println("You have clicked on" + btn_Newuser);
    new Newuser();
}

Upvotes: 1

Related Questions