0

I have created a login Frame - Login Frame(1) using Java Swing where user authentication also happen.

  1. It looks like this:

So, if a user puts wrong email id / password, it should show a Message Dialog. That's why here I use:

JOptionPane.showMessageDialog( this,"plese enter correct id/password")

But it is showing an error..

Login.addActionListener((e) -> {
    ArrayList<Registration> list0;
    list0=UserDataReadWriteFromFile.readDataFromFile();
    int idpos=Search.searchId(tuid.getText().trim());
    if(idpos >=0){
        String ueid=tuid.getText().trim();
        String uupass=tpass.getText().trim();
        if(ueid.equals(list0.get(idpos).getId())&& uupass.equals(list0.get(idpos).getPassword())){
            new SearchDisp(idpos);
        }
        else
            JOptionPane.showMessageDialog( this,"plese enter correct id/password");
    }
});

What component should I use in JOptionPane.showMessageDialog()? here is the code of my Login frame.In this code with the Login Button I have added Action Listener,which is written in the above code i have asked.

public class LoginFrame{
public LoginFrame() {
    JLabel uid, upass;
    JTextField tuid;
    JPasswordField tpass;
    JButton Login;

    JFrame frame = new JFrame("Login");

    frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);

    frame.setLayout(null);
    JLabel background = new JLabel(new ImageIcon(
            "C:\\Users\\Tousif\\Desktop\\Login.jpg"));
    frame.setContentPane(background);

    uid = new JLabel("Email Id");
    uid.setBounds(60, 50, 120, 25);
    frame.add(uid);

    tuid = new JTextField(20);
    tuid.setBounds(120, 50, 150, 24);
    frame.add(tuid);

    upass = new JLabel("Password");
    upass.setBounds(53, 80, 120, 25);
    frame.add(upass);

    tpass = new JPasswordField(20);
    tpass.setBounds(120, 80, 150, 24);
    frame.add(tpass);

    Login = new JButton("Login");
    Login.setBounds(150, 110, 80, 25);
    frame.add(Login);

    frame.setSize(370, 216);
    frame.setResizable(false);


    frame.setLocationRelativeTo(null);

    frame.setVisible(true);
}

public static void main(String[] arg) {
    new LoginFrame();
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
T.Rashid
  • 43
  • 8
  • What's the error? –  Dec 31 '16 at 07:37
  • The method showMessageDialog(Component, Object) in the type JOptionPane is not applicable for the arguments (LoginFrame, String) – T.Rashid Dec 31 '16 at 07:46
  • This suggests that `LoginFrame` is not of type `Component`. Please post the complete code of your `LoginFrame` class. – hotzst Dec 31 '16 at 07:50
  • 2
    *`String uupass=tpass.getText().trim();`* A password field should be a `JPasswordField` and the password should never be obtained as a `String`, but as a character array (`char[]`) since string objects can be retained in memory & thereby offer a vector to attack the app. security. *"What component should I use in `JOptionPane.showMessageDialog()`?"* The JavaDocs provide the short, generic answer to that. Why not look at them? – Andrew Thompson Dec 31 '16 at 08:06
  • 1
    Instead of `setBound()`, try this [layout](http://stackoverflow.com/a/8504753/230513). See also [*Initial Threads*](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). – trashgod Dec 31 '16 at 10:35
  • @trashgod what's the problm with setBound() ?? – T.Rashid Dec 31 '16 at 10:38
  • 1
    For [example](http://stackoverflow.com/a/37801762/230513). Don't make me switch vendors. :-) – trashgod Dec 31 '16 at 10:46
  • posted the complete code of my LoginFrame class @hotzst – T.Rashid Dec 31 '16 at 11:52

1 Answers1

-1

You can just use null, for example:

JOptionPane.showMessageDialog(null, "Please Enter Correct ID and Password!", 
                     "Invalid ID or Password", JOptionPane.WARNING_MESSAGE);
DevilsHnd - 退職した
  • 8,739
  • 2
  • 19
  • 22
  • While that is certainly possible, it is neither optimal nor an answer to the question that was asked. – Andrew Thompson Dec 31 '16 at 08:07
  • @T.Rashid, I haven't seen anything that states using **null** is not optimal but I'm not here to argue the fact, I just can't find that statement within the JOptionPane JavaDocs. Even the JavaDocs you have been so stringently told to read use null as the parentComponent parameter within their examples: https://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html – DevilsHnd - 退職した Dec 31 '16 at 08:25
  • Whatever.. the down vote was for not answering the question. – Andrew Thompson Dec 31 '16 at 09:00