Java - Using Window Listeners

Prof.Merlin

Expert Member
Joined
Aug 2, 2006
Messages
1,682
Hi

Let me start with the basics. I have used netbeans's JFrame creator for this project(even though that should not make a difference). I have several classes, each class with its own JFrame.

The mainFrame has a menu with many menu items in it. Each time a menu item is selected, it opens the new JFrame in new class and hides the mainFrame(or setvisible(false)).

EG: I have a menuItem called 'about'. I select it, the about window appears. The mainFrame then disappears.

When the user selects the 'OK' button, the about window closes and the mainFrame then reappears again.

The current way I am doing it is in the about class, in the actionperformed method for the 'ok button' I have the following code:
Code:
dispose();
MainFrame frame = new MainFrame();
frame.setVisible(true);


I know this is incorrect as it creates a new instance of the mainFrame each time. And with lots of menu Items being selected, it will just cause problems.

I have read up on using a window Listener, but not entirely sure how to use it as i need to use different classes in the window listener.

Do any of you maybe know a solution to my problem?

Thanks
 

pestcontrol

Member
Joined
Jan 10, 2005
Messages
15
from the top of my head try this,

in the about constructor add a jframe

eg.

jframe parent;

public About(Jframe parent){
this.parent = parent;
}

then in the main frame change the about creation to

new about(this).setvisible(true);

then change your exit within the about jframe to

parent.setvisible(true);
this.setvisible(false);
dispose();

this is assuming that you are extending jframe (netbeans default)

best IMO is to change the about to a jdialog.

this is all free hand so typos expected.
 

MartinMorrison

Senior Member
Joined
Jul 27, 2005
Messages
813
I was taught to create GUIs in a different way in Netbeans and if you want to do it this way it's alot easier but your code can get really long and unstable (to the point where you get a 'code too large error' because of all the components that need to be created and declared if you just keep adding these components.

Inside your JFrame class go to design mode, on the bottom left of your screen you should be able to see 'other components'. Right click on it , add from pallete, swing windows, new JFrame. Now you can set its name and everything on it as usual. Lets say you name the JFrame you just created 'frmAbout'.

Code your 'about' menu button to just do:
setVisible(false); //(sets your main frame to invisible)
frmAbout.show();
frmAbout.setSize(whatever is right);
frmAbout.setLocationRelativeTo(null);

Now on the 'ok' button in your about window code:
setVisible(true); //makes the main frame visible again
frmAbout.hide();
frmAbout.dispose();

Another way you can do it is to just make your about frame open over your main frame, think of it as a popup window. This only works well if your main frame is alot larger otherwise it can be confusing for a user.

Hope it helps, I just coded a massive java project and I know how frustrating it can be when you have to work all these little things out by yourself.
 
Top