Make the X Close button open a new JFrame in Java - java

I would like to make it so that when you click the X close button on the top right, a new JFrame will appear and the current one will close.
How would I be able to do it?

The question is, can another JFrame open automatically when one JFrame closes. As noted in comments, two possible common solutions include use of a WindowListener or use of a modal dialog (one example of which is as mentioned by Chance Hoard, a JOptionPane, which again is one solution, but certainly not the only solution).
For a WindowListener to work, you can open the new JFrame in the windowClosed() method, for example:
JFrame frame1 = new JFrame("Frame 1");
frame1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame1.add(Box.createRigidArea(new Dimension(500, 400)));
frame1.pack();
frame1.setLocationByPlatform(true);
frame1.setVisible(true);
frame1.addWindowListener(new WindowAdapter() {
#Override
public void windowClosed(WindowEvent e) {
JFrame frame2 = new JFrame("Frame 2");
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.add(Box.createRigidArea(new Dimension(500, 400)));
frame2.pack();
frame2.setLocationByPlatform(true);
frame2.setVisible(true);
}
});
The key here is to set the default close operation to not be JFrame.EXIT_ON_CLOSE so as not to close down the JVM after the initial JFrame window has closed. In the window listener's windowClosed() method, create and display the 2nd JFrame.
To solve this using a modal dialog, I would create the second window, which could be a JFrame, first, to give the dialog an parent application, but then I would write code to display it after displaying the modal dialog. This way, it only shows up once the dialog is no longer visible:
JFrame frame3 = new JFrame("Frame 3");
frame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame3.add(Box.createRigidArea(new Dimension(500, 400)));
frame3.pack();
frame3.setLocationByPlatform(true);
// don't display this JFrame yet
// create the dialog, passing in the JFrame and making the dialog "modal"
JDialog dialog1 = new JDialog(frame3, "Dialog 1", ModalityType.APPLICATION_MODAL);
dialog1.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog1.add(Box.createRigidArea(new Dimension(500, 400)));
dialog1.pack();
dialog1.setLocationRelativeTo(frame3);
// show the dialog first
dialog1.setVisible(true);
// this is called only after the dialog is no longer visible
frame3.setVisible(true);
Having said all this, I still recommend that you read the The Use of Multiple JFrames, Good/Bad Practice? link and I recommend avoiding both of the above "solutions" and instead recommend that one create and display a single main application window, a JFrame, and instead swap GUI views, usually JPanels, by using a CardLayout.

Related

Open second JFrame and components don't show

Before you say something, i know the implications of having more than one JFrame. I'm kinda delayed and i need to add the components manually.
So, i open a JFrame that i have designed with a button click:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
JFrame DataCalc = new JFrame();
DataCalc.setVisible(true);
DataCalc.setSize(500, 500);
DataCalc.setLocationRelativeTo(null);
}
Then the JFrame shows up but doesn't show my components. I read that if i setVisible before adding components they won't show, but they're already there cause i designed them.
If i change my code and add the setSize and setLocation like the following code, nothing happens besides the JFrame opening.
public DataCalc() {
this.setSize(500, 500);
this.setLocationRelativeTo(null);
initComponents();
}
Sry for the post, i'll edit my post if you need more info.
JFrame DataCalc = new JFrame();
Should be:
JFrame dataCalc = new DataCalc(); // use the CUSTOM frame!

JFrame: All the frames close when I try to close just one of them

public class Scratch {
public static void main(String[] args) {
Dimension d = new Dimension(300,300);
JFrame frame1 = new JFrame("Frame-1");
JFrame frame2 = new JFrame("Frame-2");
frame1.setSize(250,250);
frame2.setSize(d);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setVisible(true);
frame2.setVisible(true);
}
}
When I run this, the two frames show up as expected, but when I close one of them, both of them close. I want to achieve the functionality where only the frame I click 'x' on closes and the other remains open until I click the 'x' on it. How do I do it?
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
The "EXIT" tells the JVM to stop so all the windows are closed:
So you could be using:
frame1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Then only the frame you click on will close. When both frames are closed the JVM will exit.
However, that is not a good solution. An application should generally only have a single JFrame and then use a JDialog for a child window. Using this approach the code would be:
JDialog dialog = new JDialog(frame1);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
Depending on you requirement you would make the dialog modal or non-modal.
Using this approach the JVM will exit when you close the frame however it will stay open when you close a child dialog.
Read this forum question about multiple JFrames: The Use of Multiple JFrames: Good or Bad Practice?. It will give more thoughts on why using 2 JFrames is not a good idea.
public class Scratch {
public static void main(String[] args) {
Dimension d = new Dimension(300,300);
JFrame frame1 = new JFrame("Frame-1");
JFrame frame2 = new JFrame("Frame-2");
frame1.setSize(250,250);
frame2.setSize(d);
frame1.setVisible(true);
frame2.setVisible(true);
}
}
you add one of them to your frame.
setVisible(false);
dispose();
setDefaultCloseOperation(DISPOSE_ON_CLOSE);

how to dispose() jFrame and setVisible() another jFrame?

I created a "application window" in a Eclipse and i used the following code to dispose 'frame' and open another jframe 'TableData' which worked as i expected.
frame.dispose(); //private JFrame frame;
TableData td = new TableData();
td.setVisible(true);
Now, my problem is, I want to create 'go back' button in 'TableData' class which dispose 'TableData' and open 'frame' again.
i tried many ways but non of those work as i thought.
How can I do it????
Just hide the frame as:
frame.setVisible(false);
TableData td = new TableData();
td.setVisible(true);
and then in 'go back'
frame.setVisible(true);

How to make a JFrame button open another JFrame class in Netbeans?

I have a JFrame class and it was made in the design section on Netbeans. I am trying to make a log in button that takes closes the current frame and opens another, is there anyway I can do that?
I have tried:
JFrame frame = new JFrame();
But I want it to be editable in the design section!
Double Click the Login Button in the NETBEANS or add the Event Listener on Click Event (ActionListener)
btnLogin.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
this.setVisible(false);
new FrmMain().setVisible(true); // Main Form to show after the Login Form..
}
});
new SecondForm().setVisible(true);
You can either use setVisible(false) or dispose() method to disappear current form.
This link works with me: video
The answer posted before didn't work for me until the second click
So what I did is Directly call:
new NewForm().setVisible(true);
this.dispose();//to close the current jframe
JFrame.setVisible(true);
You can either use setVisible(false) or dispose() method to disappear current form.

Java - how can we get the selected or active frame (getSelectedFrame()) and passe it to JDialog()?

I have a mainFrame with a custom class loader. My loader is loading Pannels in the mainFrame as needed. In one of the pannel i have a JDialog. I want the JDialog box to blocks all windows from the application.
in the pannel i would like to do somting like this.
myDialog = new JDialog(getSelectedFrame(),"",Dialog.ModalityType...);
Thanks!
You dont' need it. If you want to parent the dialog to a frame, dialog, or whatever you can simply call getTopLevelAncestor() on the component that is instantiating the JDialog. That makes knowing the frame irrelevant which helps ensure the person calling or reusing code can use it on any component: frame, another dialog, window, etc.
http://docs.oracle.com/javase/6/docs/api/javax/swing/JComponent.html#getTopLevelAncestor()
For example:
class MySpecialPanel extends JPanel {
public MySpecialPanel() {
JButton button = new JButton( new AbstractAction("Show") {
public void actionPerformed(ActionEvent event) {
JDialog dialog = new JDialog( (Window)getTopLevelAncestor(), "Some Title", Dialog.ModalityType.DOCUMENT_MODAL );
dialog.add( new DialogPanel() );
dialog.show();
}
});
}
}
Now by using MySpecialPanel.getTopLevelAncestor() the dialog you are creating doesn't need to know the exact component it is. And the client using MySpecialPanel is free to put this panel into any container it desires be that a JFrame, another JDialog, or whatever.

Categories