Reputation:
i have a frame that instantiates another frame but i don't want to use the close(x) button on the instantiated frame., so i created a button. how do i code that this button can be used to close the instantiated frame without quitting the JVM.
Upvotes: 2
Views: 24805
Reputation: 44821
Having your own close button is weird UI and should be avoided.
To get rid of a frame when your own button is clicked you can just do:
jFrame.setVisible(false);
or
jFrame.dispose();
if you want to get rid of it completely.
On frames you don't want to exit the JVM on the close button being clicked specify:
jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
or
jFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
Depending on which of the behaviour you want.
Upvotes: 5
Reputation: 552
I agree with Tom, I think that in the code you wrote down:
[framename].setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
EXIT_ON_CLOSE
means that it will exit the application completely.
You can close the window by clicking on its X button by using:
[framename].setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Here is a sample code:
package answers;
import javax.swing.JFrame;
public class Answers {
public static void main(String[] args) {
//frame 1
JFrame frame1 = new JFrame("this is frame 1");
frame1.setSize(500, 500);
frame1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame1.setLocationRelativeTo(null); // ignore this its just to place frame 1 on the center
//now this is the code for frame2
//frame 2
JFrame frame2 = new JFrame("this is frame 2");
frame2.setSize(500, 500);
frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame1.setVisible(true);
frame2.setVisible(true);
}
}
Upvotes: 0
Reputation: 1
You could try the setVisible() or dispose() functions
For your main class...
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//new DemoTryCatch()
new Frame1();
}
}
For your first frame class...
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Frame1 extends JFrame{
JButton newFrame=new JButton("Frame 2");
public Frame1() {
// TODO Auto-generated constructor stub
super("Frame 1");
add(newFrame);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300,300);
newFrame.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
new Frame2();\\instantiating your new Frame
}
});
}
}
For the instantiated frame...
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Frame2 extends JFrame{
JButton CloseFrame2=new JButton("CloseFrame2");
public Frame2() {
// TODO Auto-generated constructor stub
super("Frame 1");
add(CloseFrame2);
setVisible(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setSize(300,300);
CloseFrame2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
setVisible(false);\\You could also use dispose()
}
});
}
}
Upvotes: 0
Reputation: 353
You could use setVisible(false)
in order to just hide the instantiated JFrame from view
Upvotes: 0
Reputation: 11
Im not sure if im right but you could call dispose()
. The javadoc suggests you can reopen it though using show()
.
Upvotes: -1
Reputation: 1
Use this:
jFrame.setUndecorated(true);
jFrame.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
Upvotes: 0