File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Creating and showing JFrame from another JFrame Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Creating and showing JFrame from another JFrame" Watch "Creating and showing JFrame from another JFrame" New topic
Author

Creating and showing JFrame from another JFrame

Joshua Ebarvia
Ranch Hand

Joined: Sep 25, 2007
Posts: 70
I have 2 JFrames in my Application. The first JFrame acts as the main JFrame, simply put JFrame_main.

JFrame_main has 2 buttons, button1's action code is




JFrame_sub has 1 button, buttonDispose action code is


SO here is what I want to do. The first time I click button1 on jframe_main, of course, it creates jframe_sub and then displays it.
Once jframe_sub is displayed, I will click it's buttonDispose and as expected, it will be disposed. and jframe_main will be the active frame again.

Then I would click button1 on jframe_main to show jframe_sub (which is already disposed).. But my current code does not do it. How am I going to do it?

Regards


Lovin' java
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Joshua Ebarvia wrote:I have 2 JFrames in my Application. The first JFrame acts as the main JFrame, simply put JFrame_main.

You need one JFrame only here, the main JFrame. The other "frame" should be a JDialog, either a modal JDialog if you want to prevent interaction with the main JFrame until the dialog has been dealt with, or a non-modal JDialog if the converse is true. e.g.,
Francois Nadeau
Ranch Hand

Joined: Apr 09, 2009
Posts: 46
Hello Joshua,

The dispose() does not set the jframe_sub variable to null, so your code will not create a new Frame. If you simply want your current code to work, then change your if statement to


and use this.setVisible(false); instead of this.displose(); in your JFrame_sub buttonDispose action code.

Cheers.


[url]http://FrancoisNadeau.com[/url]
Ramses Butarbutar
Ranch Hand

Joined: Jun 16, 2009
Posts: 52
Use internal frame. So the main frame call internal frame.
Here example :


I hope this help you to understand the concept.


At the center of non-violence stands the principle of love
.... Martin Luther King, Jr.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Ramses and Francois: Does the OP's problem suggest the need for a dialog as the second window and not a JFrame? The second window is clearly dependent on the first, and a JDialog is the cleanest most straightforward way to enforce this dependency.
Francois Nadeau
Ranch Hand

Joined: Apr 09, 2009
Posts: 46
Hello Pete,

I agree with you that a JDialog may be a better GUI design, but I do not know what the requirements are for this code.

Joshua asked why his JFrame was not displaying a second time. The answer to this question is that his "if" statement checked for a null value, yet he did not set his jframe_sub variable to null after disposing it. Using a JDialog will not resolve this problem.

Cheers.
Ramses Butarbutar
Ranch Hand

Joined: Jun 16, 2009
Posts: 52
pete stein wrote:Ramses and Francois: Does the OP's problem suggest the need for a dialog as the second window and not a JFrame? The second window is clearly dependent on the first, and a JDialog is the cleanest most straightforward way to enforce this dependency.


He said he have two frame.. so my explanation show him how to deal with 2 frame and interaction with.
I like your opinion to use dialog.. yeah it straightforward to enforce dependency but not the cleanest.
It's depend on what he need on his application.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Francois Nadeau wrote:Hello Pete,
I agree with you that a JDialog may be a better GUI design, but I do not know what the requirements are for this code.

Agree, but if the OP is using two JFrames, I have to wonder if he knows about dialogs and their uses. It's probably better not to use two JFrames in this situation, especially if one window is dependent on the other.
Joshua asked why his JFrame was not displaying a second time. The answer to this question is that his "if" statement checked for a null value, yet he did not set his jframe_sub variable to null after disposing it. Using a JDialog will not resolve this problem.

He did ask for this, but often what OPs ask for is not always what they need as I think is the case here. The answer here is not to check for null.

Sorry to drone on about this....
Francois Nadeau
Ranch Hand

Joined: Apr 09, 2009
Posts: 46
Hello Pete,

I don't think that you are droning on (and I'll keep saying that if you say the same about me ).

pete stein wrote:
The answer here is not to check for null.


That is too general of a statement, and like Ramses pointed out with regards to the JDialog, it too depends on the application's needs. Not checking for null will mean that a new JFrame instance will be created every time the button is pressed (which is what Ramses's code does). It also means that pointers to previous JFrames will be lost. From a feature's point of view this would allow the user to see multiple dispose frames at the same time (by pushing the button1 before getting rid of the window). On the other hand if the requirements only needed one window at a time, then keeping the lazy initialization and simply calling setVisible(true) when the button is pushed will be more efficient.

Cheers.
Joshua Ebarvia
Ranch Hand

Joined: Sep 25, 2007
Posts: 70
Hi guys.. Thanks for the responses. To be honest, I'm now confused.

Ok so JDialogs will do also...

BUt the reason for me checking for null is because every time button1 is clicked, a new instance of jframe_sub is created. I dont want that to happen and I dont want to create a singleton also.


So after clicking buttondispose on jframe_sub, (then it will be disposed) how will jframe_main know that jframe_sub was disposed so that I can set jframe_sub to null in the code of jframe_main? I mean what would be the condition instead of the one i used?


Thanks
Francois Nadeau
Ranch Hand

Joined: Apr 09, 2009
Posts: 46
Hi Joshua,

Sorry about the confusion, I guess we got a little carried away. Based on what you just said, you need to change your code as follows.

JFrame_main has 2 buttons, button1's action code:


JFrame_sub has 1 button, buttonDispose action code:


Cheers.
Joshua Ebarvia
Ranch Hand

Joined: Sep 25, 2007
Posts: 70
Thanks Francois,

That worked..

Anyway, I would like to ask for the best design.. Here is my purpose.


I have a lot of applications jdbc/swing and I do it via a JFrame with JDialog Combinations.

However, I like to create some sort of a main menu (the reason I created a jframe_main) that would have buttons when clicked, will call my other applications. Those applications can independently run without jframe_main. But then, I would like to have some sort of a main portal that let's me call all my application.

What would be the best design for it? Am I on the right track? or is it that I have to do a lot of changes.


Regards
Francois Nadeau
Ranch Hand

Joined: Apr 09, 2009
Posts: 46
Hi Joshua,

It mostly depends on how you want your application to behave. The way I understand your GUI design is that you will have a toolbar-like-window from which you can start and view other windows. I personally don't like that design because it creates many windows when I am using a single application. But that is my own personal preference, there are applications out there that follow this pattern (for example Gimp).

If you are asking my opinion, I would suggest using a JTabPane instead. This will allow you to split-up your application in different views, without having multiple windows. In other words, your users will have only one window to deal with.

An other option would be to use JInternalFrames. But again that is not one of my favorite approaches since users still have to manage a bunch of windows.

If you persist on wanting separate frames, then your on the right track but may have problems if the user closes your frames with the X button (or alt+F4 on windows) instead of your dispose button. To solve that you will need to look into JFrame's setDefaultCloseOperation function.

I am willing to further discuss your GUI design, but we will need to have more details. For example, what will your application do, who will use it, how many screens will it have, and what type of processes/activity will it perform.

Hope this helps.
Joshua Ebarvia
Ranch Hand

Joined: Sep 25, 2007
Posts: 70
Francois Nadeau wrote:Hi Joshua,

It mostly depends on how you want your application to behave. The way I understand your GUI design is that you will have a toolbar-like-window from which you can start and view other windows. I personally don't like that design because it creates many windows when I am using a single application. But that is my own personal preference, there are applications out there that follow this pattern (for example Gimp).

If you are asking my opinion, I would suggest using a JTabPane instead. This will allow you to split-up your application in different views, without having multiple windows. In other words, your users will have only one window to deal with.

An other option would be to use JInternalFrames. But again that is not one of my favorite approaches since users still have to manage a bunch of windows.

If you persist on wanting separate frames, then your on the right track but may have problems if the user closes your frames with the X button (or alt+F4 on windows) instead of your dispose button. To solve that you will need to look into JFrame's setDefaultCloseOperation function.

I am willing to further discuss your GUI design, but we will need to have more details. For example, what will your application do, who will use it, how many screens will it have, and what type of processes/activity will it perform.

Hope this helps.



So this is what I'm doing....

I have been creating client-server swing/jdbc/mysql applications for
1. inventory
2. transmittal records
3. simple vehicle information management
4 .......

All of these where created independently so 1 user will be using inventory, the other user will be using the transmittal records, and another one will use the 3rd one. There are about 10 more...
So each user will be dealing with independent applications.

I created my project in netbeans 6.5.
All of those applications were JFrame and those JFrames, have JDialogs as a way to input data, the JFrame consists of a JTable which shows the records. All of the applications were designed that way.

I have created a logIn form in which when user log's in, the appropriate application, i.e... transmittal record will appear and he will start working with that. So there is no menu yet.

Now, I was asked to create such a main menu in which all applications could be accessible via a central JFrame(the one I was trying to do) so that if one person is absent, the other can do that job(of the absent) and his daily task even in his own computer.

All of the applications I created were designed for single user but then like the situation above, when one is absent, someone is asked to do that job of the absent person.

Therefore I have to create that main menu so that they will be able to access all the applications.



Francois Nadeau
Ranch Hand

Joined: Apr 09, 2009
Posts: 46
Hi Joshua,

Ok, that make sense to me. You say that each of the applications were developed separately. That sounds a little worrisome to me.

1. Do any of the applications call System.exit()? If so then your current design will cause all windows to close when it is called.
2. What happens if one of the applications crashes? Is it OK for all of them to die?
3. Does the applications use static variables to store internal data. This will most likely prevent you from creating new instances of your application, since some data will be left behind from one execution to an other.
4. What kind of logic has been added to each of the application's JFrame.windowListeners? This could get messy....

I can think of two options to solve these problems,
1. Only allow one application to run at a time, and keep a reference to the ones that have been created (this is basically what you have been doing, but will need a little black magic if logic has been inserted in windowlisteners).
2. Start each of the applications as an external application (in which case the easiest thing to do would be to create a bunch of batch files and put them in the a start-application-folder, or if you really want to program something then use Runtime.exec()).

I think option 2 would be the best. It would require no programming, but may be a little annoying to deploy.

Hope this helps.
Joshua Ebarvia
Ranch Hand

Joined: Sep 25, 2007
Posts: 70
Francois Nadeau wrote:Hi Joshua,

Ok, that make sense to me. You say that each of the applications were developed separately. That sounds a little worrisome to me.

1. Do any of the applications call System.exit()? If so then your current design will cause all windows to close when it is called.
2. What happens if one of the applications crashes? Is it OK for all of them to die?
3. Does the applications use static variables to store internal data. This will most likely prevent you from creating new instances of your application, since some data will be left behind from one execution to an other.
4. What kind of logic has been added to each of the application's JFrame.windowListeners? This could get messy....

I can think of two options to solve these problems,
1. Only allow one application to run at a time, and keep a reference to the ones that have been created (this is basically what you have been doing, but will need a little black magic if logic has been inserted in windowlisteners).
2. Start each of the applications as an external application (in which case the easiest thing to do would be to create a bunch of batch files and put them in the a start-application-folder, or if you really want to program something then use Runtime.exec()).

I think option 2 would be the best. It would require no programming, but may be a little annoying to deploy.

Hope this helps.


Yes, the applications were developed separately, but I was the one who did them all. Now, I managed to put/arrange them into packages so that they will all be in just one netbeans project.
I have changed each and every setDefaultCloseOperation from EXIT_ON_CLOSE to DISPOSE. Now, to be sure that only one application is running(meaning not two computers are running inventory), I have logged in the database which are currently executing, and only if that application was terminated is the time it can be opened again, (that's my locking mechanism)

Thank you very much for your help.....

Regards
 
jQuery in Action, 2nd edition
 
subject: Creating and showing JFrame from another JFrame