Reputation: 67
I am sorry, for I believe that this question has been asked already, but none of the answers have actually helped me out. I have a class with gui, it contains a JFrame with several textfields and buttons. Here's the main() I'm trying to get working:
Gui interface1 = new Gui();
interface1.setSize(200,200);
interface1.setVisible(true);
//hold main execution
//wait for the pressed button in gui
//close the jframe
//proceed with main()
call_some_functions();
I have tried setting default close operation to HIDE_ON_CLOSE but that doesn't seem to be what I want. I'm using a framework and I need main() to be alive for quite a long time. Basically, I need jframe to hold the main() for the time of user input and updating parameters, then close itself without terminating the process and continue with main() as if nothing has happened. Thanks in advance for any help.
Upvotes: 1
Views: 1946
Reputation: 3566
Use :
frame.setDefaultCloseOperation(frame.HIDE_ON_CLOSE);
EG:
import javax.swing.JFrame;
import javax.swing.JTextField;
public class TestFrame {
public static void main(String aa[])
{JFrame frame =new JFrame();
JTextField field=new JTextField("hello buddy..nuthin happnd");
frame.setDefaultCloseOperation(frame.HIDE_ON_CLOSE);
frame.add(field);
frame.pack();
frame.setVisible(true);
}
UPDATE AS PER YOUR NEEDS:
import javax.swing.JFrame;
import javax.swing.JTextField;
public class TestFrame {
public static void main(String aa[])
{
JFrame frame =new JFrame();
JTextField field=new JTextField("hello buddy..nuthin happnd");
frame.setDefaultCloseOperation(frame.HIDE_ON_CLOSE);
frame.add(field);
frame.pack();
frame.setVisible(true);
frame. addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentHidden(java.awt.event.ComponentEvent evt) {
formComponentHidden(evt);
}
});
}
private static void formComponentHidden(java.awt.event.ComponentEvent evt) {
somefunction();
}
public static void somefunction()
{
System.out.println("hii!! i am hidden!!");
}
}
Upvotes: 0
Reputation: 347184
Uses a JDialog
instead of JFrame
and make the JDialog
modal, using JDialog#setModal
to true
See How to use dialogs for more information
Upvotes: 5