简体   繁体   中英

Invoke a new window after password verification in Java

I'm working on a RMI client server program. And for my client class I want when I start the client to have a window for verification of the password. If the password is wrong a pop-up message will appear, and if it's correct the current window needs to close and another window with options to work with the server should appear. The code for such an action would be something like that:

//Button for checking password - actionListener
if(!checkPass(btnLogin.getPassword())
  JOptionPane.showMessageDialog(null,"ALERT MESSAGE",JOptionPane.WARNING_MESSAGE);
else
  // do something so this window closes and a new window, 
  //with say one button for example, pop-ups

How should I do something like that? The current code is just the back-bone of the client with the remote methods and it also inherits from a jFrame which is currently the password checking window. Keep in mind, that I'm trying to keep all the client gui and functionality in one class. Should the two windows be jPane s and how should I deal with them. I'm lost here so any kind of help is welcomed :)

You want to try and separate and isolate responsibility. That is, the login view should do nothing more then gather the credentials from the user an validate those credentials. It should NOT be responsible for moving the user onto the next view, that responsibility belongs to a different part of you application (or controller)

  1. Wrap the login component in a modal JDialog . From your main class, you can show this dialog, it will block until the dialog is closed (calling dispose on the dialog)
  2. Once the dialog has been closed, and the current user verified,you can create your main application window as per normal

This is an example of a MVC based login dialog, while it might seem complicated, it would be a good lesson in separation of responsibility and introduction in to the Model-View-Controller paradigm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM