tags:

views:

71

answers:

2

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.

A: 

just use firstFrame.setVisible(false) on the first frame. This will make it hidden..

if you want a more general approach you could have a reference to the current displayed frame somewhere and change it when a new frame requests to be shown

JFrame currentFrame;

void showRequest(JFrame frame)
{
  currentFrame.setVisible(false);
  currentFrame = frame;
  currentFrame.setVisible(true);
}
Jack
i want the main frame still there just want to make sure the user only open one create frame. I was thinking to disabled to button, but is there any other good practice to implement ?
you can try with __.setFocusable(false)__, this will keep the frame but the user won't be allowed to click anything inside it..
Jack
Thank for your suggestion. I tried to do it like createButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { ... createFrame.setVisible(true); mainframe.setFocusable(false); }});But I still can click the create button on main frame?
+2  A: 

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.

akf
Hi it does exactly what I want. Thank you very much