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);
Related
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.
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!
Situation: there is Supplier_JinternalFrame in JDesktopPane. A Supplier_button is available for call SetVisible(true) in JDesktopPane to show the Supplier frame. but when i close the Supplier Frame through Dispose(); its hide and never see again after click button again.
there is and option to use setshow(); and setHide() instead of setDispose(). but i want to recreate Jinternalframe instead of previous form. here is my Supplier_button code for call the Supplier_JInternalFrame Object.
private void Suppliers_ButtonActionPerformed(java.awt.event.ActionEvent evt) {
Suppliers_JinternalFrame.setVisible(true);//call the object of frame to show the frame
}
and in Suppliers_JinternalFrame Close button code
private void Close_SupMangActionPerformed(java.awt.event.ActionEvent evt) {
Suppliers.dispose();//To close the frame
}
after click supplier_Button again. it never show frame again.
Any Idea, what should i do to recall the InternalFrame . Is there Any WindowEvents, i suggestest to create ?
When dispose is called, the JInternalFrame is made invisible, it is also removed from the JDesktopPane (think desktopPane.remove(...))
This means that making the frame visible again has no (visible) effect. You need to add the frame back onto the JDesktopPane
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.
I've got a JPanel, which I want to respond to a mouse click and then open a JDialog. The JDialog constructor needs an instance of JFrame and not JPanel - how do I work around this?
You should really try to attach the JDialog to a parent Dialog or Frame, especially if you want it modal (by passing a parent Window, the dialog will be attached to your Window and bringing the parent will bring the child dialog as well). Otherwise, the user experience can really go wrong: lost dialogs, blocking windows without seeing the modal dialog, etc...
To find your JPanel parent Window, all you need is this code:
JPanel panel = new JPanel();
Window parentWindow = SwingUtilities.windowForComponent(panel);
// or pass 'this' if you are inside the panel
Frame parentFrame = null;
if (parentWindow instanceof Frame) {
parentFrame = (Frame)parentWindow;
}
JDialog dialog = new JDialog(parentFrame);
...
If you don't know if you are in a Frame or Dialog, make the "instanceof" test for both classes.
Using the parameter free constructor will make the dialog owner-less. I think that the best thing to do would be to make the Frame that owns your Panel the owner of the dialog.
By that, I mean that you should use the getParent() from your JPanel to find its owner and then send this object found as the owner of your JFrame.
A crude code for that would be
java.awt.Container c = myPanel.getParent();
while (!(c instanceof javax.swing.JFrame) && (c!=null)) {
c = c.getParent();
}
if (c!=null) {
JFrame owner=(javax.swing.JFrame) c;
JDialog myDialog=new JDialog(owner);
}
I have not tested this code, but it is good enought for you to understand the idea.
If you decided to go with a JOptionPane, you could add a MouseListener to the JPanel with a mouseAdapter inner class to handle mouseClicked events. You would have to declare the panel final in order to access the panel from within the inner class.
final JPanel testPanel = new JPanel();
testPanel.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
JOptionPane.showMessageDialog(testPanel,"Title","InformationMessage",JOptionPane.INFORMATION_MESSAGE);
}});//end of mouseClicked method
There's a constructor that don't need argument:
JDialog dialog = new JDialog();
If what you want is to make the dialog modal, maybe you can get a static reference of you main JFrame, something like:
JDialog dialog = new JDialog(MyMainJFrame.getInstance());