• Post Reply Bookmark Topic Watch Topic
  • New Topic

only one window opened

 
dejie lin
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
There's a button in main GUI, press the button-->a new GUI will jump out, if press the button twice, there will be another new GUI jump out, how can i close the first GUI automatically when the second GUI comes out?
The code looks like:
class MainFrame extends JFrame{
....
Container f = getContentPane();
JButton openFrame = new JButton("open");
openFrame.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
subFrame sf=new subFrame();
sf.show();
}
});
f.add(openFrame);
....
}
class subFrame extends JPanel{
subFrame(){}
...
}
Thanks in advance
dejie
 
Abhilash Vasudevan
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here, each time you click the button obviously new Windows will be opened. Each of these windows can be closed from inside them only.
If your requirement is to avoid the user opening new windows again and again you can extend JDialog for your subframe with the modal true constructor, which forces the control into it and the user will not be able to click the button of the parent frame unless he closes the sub frame.
Regards
 
dejie lin
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi abhilash_iv,
...my requirement is to allow user click the "open" button whenever they what, but there should be only one subframe appeared, which will shows the user's lastest modification from the mainFrame.
I can't understand your meaning well, can you specify it more?
thanks
dejie
 
Abhilash Vasudevan
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK let me be more specific.
Instead of making your subFrame extend a Panel let it extend a JDialog. In the Constructor of the subFrame, you just call the super's(JDialogs) constructor with 'modal' argument true.
ie, super(parent,true);
Here parent is a JFrame variable which you will pass as 'this' from your main window.
This will make your new window modal ie, the control can never be passed back to your parent frame, unless the JDialog window is closed. So it will restrict the user from clicking the 'open' button again when the new window is open.
Hope this clears the matter and hope this solves your problem.
Regards
 
dejie lin
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Abhilash Vasudevan,
er....when i replace extends JPanel by JDialog, the only error happens at "can not resolve symbol-->subFrame.revalidate()" when compling, i have already check the API for revalidate() of JDialog, but no result, can you tell me what' the code for it?
another question: after extends JDialog using your method, can the subFrame keeps updating and shows the latest information to users whenever the "open" be pressed?
thanks for your time
dejie
 
Abhilash Vasudevan
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this example code. Hope this will do.

regarding refreshing the JDialog each time you press the button, you can write a method in this class and call them each time and call show().

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!