aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes open a new JFrame only once when JButton is clicked Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "open a new JFrame only once when JButton is clicked" Watch "open a new JFrame only once when JButton is clicked" New topic
Author

open a new JFrame only once when JButton is clicked

Harini Kumaresan
Greenhorn

Joined: Sep 16, 2012
Posts: 5
hi all,
I need to open only one JFrame when a JButton is clicked from the main JFrame. I am calling that Jframe by a constructor. That constructor invokes a class. and that's how the frame opens. but however for each button click it opens a new frame again and again. I think the constructor is invoking the class again and again.


I set a flag to resolve this. The code is below.

This time it doesn't even open once. please tell me how to do this?
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Your code doesn't show much. But from description I would say you can disable a button after the JFrame is shown, so it won't be possible to invoke that action again.


The quieter you are, the more you are able to hear.
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 557
Your method for limiting the number of new JFrames should be unnecessary. There is nothing inherent in Swing that causes multiple JFrames to be created and set visible in response to a single request. So, trace your code starting at the button handler and determine where the extra JFrame is being created and set visible. I sometimes see this artifact in my own code when my constructor creates a JFrame using 'new' (rather than having the class extend JFrame) and sets it visible while another process, like a button handler, creates its own instance of that class and also sets it visble. Count the numbers of 'new's in your own code and eliminate the extra instance being created in that chain.


Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
Harini Kumaresan
Greenhorn

Joined: Sep 16, 2012
Posts: 5
Your code doesn't show much. But from description I would say you can disable a button after the JFrame is shown, so it won't be possible to invoke that action again.


I disabled the button and so i can open only one frame when i click the button. But now even after closing that frame the button remains disabled. I tried enabling button in the action performed block of the cancel button in that search frame. But since it is written entirely in a new class, it says that button is not found.
So what i want to do now is disable the button in the main JFrame after that sub frame is opened and then enable that button again in the main JFrame after the sub frame is closed. Please tell me thanks in advance
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
I can see some design I don’t like there. Using source == makes me suspect you are not using proper object‑oriented design for your listeners. Uing flag == 1 makes me suspect you are using ints intead of booleans.
Why are you trying to open more than one frame in the first place? You should use one frame per application, and everything else should be different classes, eg modal dialogs, internal frames. You can find more details in the Java Tutorials.

I shall move this discussion to our GUIs forum.

Edit: add Java tutorials link.
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 557
Ahhh. I misunderstood the first post. Good on Kemal for "getting it."

What you describe you want to do is entirely reasonable, though I would centralize the enabling/disabling of the button to a portion of code that defined the state of the main UI. What problems are you having implementing what you suggested?
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Harini Kumaresan wrote:...
So what i want to do now is disable the button in the main JFrame after that sub frame is opened and then enable that button again in the main JFrame after the sub frame is closed. Please tell me thanks in advance


This is an ideal scenario for using JDialog - create a modal dialog and once you show it your main frame can't receive any actions from user until you close the dialog. Also it won't be necessary to enable/disable any component in your first JFrame.
 
 
subject: open a new JFrame only once when JButton is clicked