views:

310

answers:

5

I'm programming my very first GUI app in Java using the Swing framework. I've coded a basic login system using the JTextField, JPasswordField, and JButton classes. Now, I'm writing the actionPerformed method for the button, which I want to remove these items as they are no longer necessary, but I am unsure as to the best way of achieving this.

I've considered using the setVisible method of these components, which seems to work, but I'm sure that's not the preferred way of doing it. Sorry if this is a bit of a dumb question..

+4  A: 

Have your login dialog separated from your main window. When you finished with the login, just hide the login dialog.

You can also save your text fields and buttons into a class field, and later call remove(Component) for each one.

kd304
Is it recommended to setVisible, or to remove?
Liran Orevi
I favor separation. My login is usually a popup like modal dialog. I rarely need to change the contents of a jpanel - causes trouble with the layout management.
kd304
If you use a dialog -- see JDialog -- then you'd normally dispose() it when done
kdgregory
+1  A: 

Generally, you would want to be able to do this in one line of code. So, you should consider wrapping the different things you'd like to show or hide in a JPanel. Then, you can dynamically show or hide the JPanels.

JoshJordan
If you change the contents of the JFrame, it is implicitely repaint()-ed.
kd304
Updated to reflect this.
JoshJordan
A: 

I agree with JPanel suggestion. Add the log-in components to a JPanel, then hide the JPanel once there is a log-in.

Chung Pow
hide the log-in Panel once the user clicks a successful log-in
Chung Pow
This is the same logic as the JDialog stated above... have a seperate JPanel for a login, then hide it once the user has a successful login
Chung Pow
I find this easier for the user because there arent any seperate windows open over the screen. Hope this revision helps!
Chung Pow
A: 

I will suggest you to use a JDialog for your login. After a successful login you just need to call “dialog.dispose()” and then load your interface. If the application you are building need to display several windows you should consider to use a JDesktopPane (http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JDesktopPane.html)

Dani Cricco
+1  A: 

You could have a login JPanel, which is set up and displayed and once the user's password is verified, you can show your application JPanel. This can easily be done using a CardLayout.

It treats each component in the container as a card. Only one card is visible at a time, and the container acts as a stack of cards.

Here is a tutorial.

Using a CardLayout, your code could look something like this:

instance variables:

static final String LOGINPANEL = "LOGINPANEL";
static final String MAINPANEL = "MAINPANEL";
JPanel cards;

where your panels are created:

JPanel loginPanel = new JPanel();
//add your stuff to the login panel
JPanel mainPanel = new JPanel();  
//add your stuff to the main panel

cards = new JPanel(new CardLayout());
cards.add(loginPanel, LOGINPANEL); 
cards.add(mainPanel, MAINPANEL);

then when your password is verified, in the AWT thread, you can do this:

CardLayout cl = (CardLayout)(cards.getLayout());
cl.show(cards, MAINPANEL);
akf