Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

open a new JFrame only once when JButton is clicked

 
Harini Kumaresan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Harini Kumaresan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49447
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic