Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java GUI Design Advice

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..

like image 668
mportiz08 Avatar asked Jul 17 '26 23:07

mportiz08


2 Answers

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.

like image 146
akarnokd Avatar answered Jul 20 '26 13:07

akarnokd


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.

like image 34
JoshJordan Avatar answered Jul 20 '26 11:07

JoshJordan