Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable main JFrame when open new JFrame

Tags:

java

swing

jframe

Example now I have a main frame contains jtable display all the customer information, and there was a create button to open up a new JFrame that allow user to create new customer. I don't want the user can open more than one create frame. Any swing component or API can do that? or how can disabled the main frame? Something like JDialog.

like image 331
user236501 Avatar asked Jun 12 '10 15:06

user236501


People also ask

How do I close the previous JFrame on the opening of a new JFrame?

The key is to set setDefaultCloseOperation(JFrame. DISPOSE_ON_CLOSE) .

How do you stop a JFrame?

Exiting from Java running process is very easy, basically you need to do just two simple things: Call java method System. exit(...) at at application's quit point. For example, if your application is frame based, you can add listener WindowAdapter and and call System.

How do I close a JFrame without exiting?

DISPOSE_ON_CLOSE -- The frame will be closed and disposed but the application will not exit. JFrame. DO_NOTHING_ON_CLOSE -- The frame will be closed but not disposed and the application will not exit.

How do I open a JFrame in maximized mode?

By default, we can minimize a JFrame by clicking on minimize button and maximize a JFrame by clicking on maximize button at the top-right position of the screen. We can also do programmatically by using setState(JFrame. ICONIFIED) to minimize a JFrame and setState(JFrame. MAXIMIZED_BOTH) to maximize a JFrame.


3 Answers

I think you should use this code for the main jframe when you trying to open new one :

this.setEnabled(false);

like image 102
Hatem Avatar answered Sep 21 '22 15:09

Hatem


I would suggest that you make your new customer dialog a modal JDialog so that you do not allow input from other dialogs/frames in your app while it is visible. Take a look at the modality tutorial for details.

like image 31
akf Avatar answered Sep 20 '22 15:09

akf


Sorry for the late answer but have you considered the Singleton design pattern? It will return the same instance of a class whenever you want the class. So if the user wants a frame to enter the details, there will only be one frame open (same instance)

It goes something like this:

private static MySingleFrame instance = null; //global var

private MySingleFrame() { } //private constructor 
private static MySingleFrame getInstance()
{

if(instance == null)
{
instance = new MySingleFrame();
}

//returns the same instance everytime MySingleFrame.getInstance() is called
return instance; 


}
like image 26
J--- Avatar answered Sep 21 '22 15:09

J---