2

I am starting of with Java desktop app development after having started with Android Development first (a little curious, but there it is).

What I have done is to create a Class title Login.java which is where the app should start and it does. After authenticating comparing with a MySQL Database table, I need to show a new Class titled Members.java.

To that effect, I tried the solution from here: Java swing application, close one window and open another when button is clicked and used this code in my Login.java file:

dispose();
new Members().setVisible(true);

What is happening however is, the Login.java window closes briefly (sort of) and then shows the Members.java. Is this behavior normal? Or am I coding it wrong? The Members.java needs to replace the Login.java after authentication.

Let me draw a quick comparison with Android to clarify a little further (in case the above does not). When I need to show a new class in Android, I can call it by firing a simple code such as this:

Intent showActivity = new Intent(this, SecondActivity.class);
startActivity(showActivity);

This does not give the impression that something closed and then something opened. Any help is appreciated.

P.S.: I tried searching a lot, but since I am not sure what to search for, it's been a no go on that front. Also, I am using Netbeans as my IDE as against Eclipse.

Community
  • 1
  • 1
SSL
  • 278
  • 2
  • 17
  • Swing and Android are two different frameworks. You shouldn't learn how to best use Android by looking at Swing code. BTW, what's your question? – JB Nizet Aug 17 '13 at 07:24
  • @JBNizet: The Android code is actually, merely an example to illustrate what I am trying to do in Swing. How to close the current class (Activity in Android) and open another is the question. As mentioned in the OP, there is a brief pause between closing the first and opening the second. And I am not sure that behavior is desirable. – SSL Aug 17 '13 at 07:30
  • OK, so you actually want to use Swing. That wasn't obvious from the question, especially since it's tagged android. Consider using JDialog, or using a single frame with a CardLayout. Swing has a huge tutorial covering almost everything. Google for Swing tutorial. – JB Nizet Aug 17 '13 at 07:33
  • @JBNizet: Actually, that where I start from. ;-) And I tagged it with Android to help draw the comparison. Thanks for your suggestion in any case. I am trying to learn desktop app development now that I have a decent hold of Android dev. :-) – SSL Aug 17 '13 at 07:36

1 Answers1

3

What is happening however is, the Login.java window closes briefly (sort of) and then shows the Members.java. Is this behavior normal?

Of course, that's exactly what you requested.

dispose();                      // close the Login window
new Members().setVisible(true); // and show another window

If you want to get rid of that brief pause, call dispose after new Members().setVisible.

Gabriel Negut
  • 13,860
  • 4
  • 38
  • 45
  • That does help. Thanks. Is this, in your opinion, a recommend approach? Or is this just wrong an there is something better I haven't come across? Also, let me give any potential answers a couple of minutes before accepting. :-) – SSL Aug 17 '13 at 07:42
  • You could use a single window and a `CardLayout` instead of switching between windows (see [here](http://stackoverflow.com/a/9573032/619252) for an example). – Gabriel Negut Aug 17 '13 at 08:03
  • That does seem to be a much better alternative. Thanks for your help. :-) – SSL Aug 17 '13 at 08:05