Java close all jframe

How to close jframe panel or window

I have 2 frames/windows, I have Exit button on window 2, an from window 1 I launch window 2 and then exit it i.e setVisible(false); When I execute window 2 I can easily click button exit and hide the current window, however when I launch window 2 from window 1, and then click exit button I get NullPointerException Error. then I instantiated it in the beginning with static and this error was gone, however the window 2 is not being closed/hidden its still there with no effect of button. Window 1 code:

package com.my.jlms; import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JButton; import javax.swing.Icon; import javax.swing.ImageIcon; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class LibrarianMenu extends JFrame < private JPanel contentPane; private static LibrarianMenu frame; /** * Launch the application. */ public static void main(String[] args) < EventQueue.invokeLater(new Runnable() < public void run() < try < frame = new LibrarianMenu(); frame.setVisible(true); >catch (Exception e) < e.printStackTrace(); >> >); > /** * Create the frame. */ public LibrarianMenu() < setTitle("Librarian"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 385, 230); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JButton btnPasswd = new JButton("Change Pass"); btnPasswd.setBounds(202, 76, 146, 39); contentPane.add(btnPasswd); btnPasswd.addActionListener( new ActionListener() < public void actionPerformed(ActionEvent e) < ChangePwd framee = new ChangePwd(); framee.setVisible(true); >>); > > 
package com.my.jlms; import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JButton; public class ChangePwd extends JFrame < private JPanel contentPane; private static ChangePwd frame = new ChangePwd();; private JButton btnExit; /** * Launch the application. */ public static void main(String[] args) < EventQueue.invokeLater(new Runnable() < public void run() < try < frame = new ChangePwd(); frame.setVisible(true); >catch (Exception e) < e.printStackTrace(); >> >); > /** * Create the frame. */ public ChangePwd() < setResizable(false); setTitle("Password!"); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setBounds(100, 100, 266, 154); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); btnExit = new JButton("Exit"); btnExit.setBounds(20, 80, 89, 30); contentPane.add(btnExit); btnExit.addActionListener( new ActionListener() < public void actionPerformed(ActionEvent ae) < frame.setVisible(false); >>); > 

Источник

Читайте также:  Node js css path

How to programmatically close a JFrame?

What’s the correct way to get a JFrame to close, the same as if the user had hit the X close button, or pressed Alt + F4 (on Windows)? I have my default close operation set the way I want, via:

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

It does exactly what I want with the aforementioned controls. This question isn’t about that. What I really want to do is cause the GUI to behave in the same way as a press of X close button would cause it to behave. Suppose I were to extend WindowAdaptor , then add an instance of my adapter as a listener via addWindowListener() . I would like to see the same sequence of calls through windowDeactivated() , windowClosing() , and windowClosed() as would occur with the X close button. Not so much tearing up the window as telling it to tear itself up, so to speak.

Yes good question, I want a user click on an [x] button I explicitly supply on an undecorated override of a JFrame to act exactly as if the user had clicked on the OS supplied [x] button on an OS specific decorated JFrame window.

How about this.dispatchEvent(wev); instead of Toolkit.getDefaultToolkit. The former is what the accepted answer suggests.

16 Answers 16

If you want the GUI to behave as if you clicked the X close button then you need to dispatch a window closing event to the Window . The ExitAction from Closing An Application allows you to add this functionality to a menu item or any component that uses Action s easily.

frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING)); 

Do I need to add this.jframe.addWindowListener(new WindowAdapter() < @Override public void windowClosing( WindowEvent e ) < e.getWindow().dispose(); >>); prior to calling this? Or will it close down the window as well?

Читайте также:  Java file path with environment variable

«How to programmatically close a JFrame» — If the JFrame.EXIT_ON_CLOSE is not set, it will not close. Anyway, I was just pointing out that it might be needed to add to the answer, since jframe.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); won’t actually close it by triggering this event despite the user normally being able to close the window on X even if DO_NOTHING_ON_CLOSE is set, but firing this event won’t. See the difference?

@momomo, there was no need to point out anything. This answer will do exactly as the OP asked. It will do whatever the «X» close button does. The title of the question is not the question. The OP specifically stated the default close operation was set to «exit», so what the «do nothing» does is irrelevant since the OP was not asking about that. Please delete all your comments so you don’t confuse other people reading this answer, since the answer was given to directly answer the OP’s question, not your interpretation of the heading of the question.

This answer is useless for any non-trivial scenario, because in any non-trivial scenario, the reason why I would want to programmatically close the frame is precisely because I have already handled the «closing» event, taken various actions to determine whether the frame should in fact close, have determined that the frame should indeed close, and are now trying to actually close it. So, invoking the «closing» event again would cause infinite recursion.

setVisible(false); //you can't see me! dispose(); //Destroy the JFrame object 

Not if the default close operation is on EXIT_ON_CLOSE or the button listener performs any needed resource closing.

from what I observe, invoking dispose() does in fact cause the «deactivated» and «closed» listener events to be issued. So, I do not know what the commenters above are complaining about.

If by Alt-F4 or X you mean «Exit the Application Immediately Without Regard for What Other Windows or Threads are Running», then System.exit(. ) will do exactly what you want in a very abrupt, brute-force, and possibly problematic fashion.

If by Alt-F4 or X you mean hide the window, then frame.setVisible(false) is how you «close» the window. The window will continue to consume resources/memory but can be made visible again very quickly.

If by Alt-F4 or X you mean hide the window and dispose of any resources it is consuming, then frame.dispose() is how you «close» the window. If the frame was the last visible window and there are no other non-daemon threads running, the program will exit. If you show the window again, it will have to reinitialize all of the native resources again (graphics buffer, window handles, etc).

dispose() might be closest to the behavior that you really want. If your app has multiple windows open, do you want Alt-F4 or X to quit the app or just close the active window?

The Java Swing Tutorial on Window Listeners may help clarify things for you.

Источник

How to close a jframe without closing the main program

I’m creating a program to keep track of a list of DVD’s. On the main page I have set up 2 JButtons. When the user clicks the one that says new, a new JFrame from another class comes up with fields to enter information about a new DVD. I’m trying to make a cancel button on this second frame work so that when one clicks the cancel button, it brings back up the main GUI and closes the new entry GUI. Code is below, I cant figure out how to do it. Main GUI page

package dvdlibrary; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; public class bootPage extends javax.swing.JFrame < /** Creates new form bootPage */ public bootPage() < initComponents(); >@SuppressWarnings("unchecked") private void initComponents() < jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout()); jLabel1.setText("DVD Library"); getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(168, 11, -1, -1)); jLabel2.setText("What would you like to do today?"); getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(113, 70, -1, -1)); jButton1.setText("Create Record"); jButton1.addMouseListener(new java.awt.event.MouseAdapter() < public void mouseClicked(java.awt.event.MouseEvent evt) < jButton1MouseClicked(evt); >>); jButton1.addActionListener(new java.awt.event.ActionListener() < public void actionPerformed(java.awt.event.ActionEvent evt) < jButton1ActionPerformed(evt); >>); getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 135, -1, -1)); jButton2.setText("View Library"); jButton2.addMouseListener(new java.awt.event.MouseAdapter() < public void mouseClicked(java.awt.event.MouseEvent evt) < jButton2MouseClicked(evt); >>); getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(224, 135, 111, -1)); pack(); >// private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) < >private void jButton1MouseClicked(java.awt.event.MouseEvent evt) < if(evt.getSource()== jButton1) dvdlibrary.createEntry.createE(); >private void jButton2MouseClicked(java.awt.event.MouseEvent evt) < if(evt.getSource()== jButton2) dvdlibrary.movieChooser.movChoo(); >public static void bootP() < java.awt.EventQueue.invokeLater(new Runnable() < public void run() < new bootPage().setVisible(true); new bootPage().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); >>); > // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; > 
package dvdlibrary; public class createEntry extends javax.swing.JFrame < public createEntry() < initComponents(); >@SuppressWarnings("unchecked") private void initComponents() < jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); jLabel6 = new javax.swing.JLabel(); jTextField1 = new javax.swing.JTextField(); jComboBox1 = new javax.swing.JComboBox(); jTextField2 = new javax.swing.JTextField(); jTextField3 = new javax.swing.JTextField(); jTextField4 = new javax.swing.JTextField(); jScrollPane1 = new javax.swing.JScrollPane(); jTextArea1 = new javax.swing.JTextArea(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); jButton4 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jLabel1.setText("Title"); jLabel2.setText("Genre"); jLabel3.setText("Length"); jLabel4.setText("Rating"); jLabel5.setText("Description"); jLabel6.setText("Year"); jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] < "Item 1", "Item 2", "Item 3", "Item 4" >)); jTextArea1.setColumns(20); jTextArea1.setRows(5); jScrollPane1.setViewportView(jTextArea1); jButton1.setText("Input"); jButton2.setText("Load"); jButton3.setText("Save"); jButton4.setText("Cancel"); jButton4.addMouseListener(new java.awt.event.MouseAdapter() < public void mouseClicked(java.awt.event.MouseEvent evt) < jButton4MouseClicked(evt); >>); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(19, 19, 19) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel5) .addGroup(layout.createSequentialGroup() .addComponent(jLabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel3) .addComponent(jLabel4) .addComponent(jLabel6)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jTextField2) .addComponent(jTextField4) .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE))))) .addGroup(layout.createSequentialGroup() .addGap(51, 51, 51) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(124, 124, 124) .addComponent(jButton1)) .addGroup(layout.createSequentialGroup() .addGap(45, 45, 45) .addComponent(jButton4) .addGap(18, 18, 18) .addComponent(jButton2) .addGap(18, 18, 18) .addComponent(jButton3))) .addGap(23, 23, 23)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(38, 38, 38) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton1)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2) .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(26, 26, 26) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel6) .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(29, 29, 29) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3) .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(37, 37, 37) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel4) .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(39, 39, 39) .addComponent(jLabel5) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton3) .addComponent(jButton2) .addComponent(jButton4))) .addContainerGap()) ); pack(); > private void jButton4MouseClicked(java.awt.event.MouseEvent evt) < >public static void createE() < java.awt.EventQueue.invokeLater(new Runnable() < public void run() < new createEntry().setVisible(true); >>); > private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JButton jButton4; private javax.swing.JComboBox jComboBox1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextArea jTextArea1; private javax.swing.JTextField jTextField1; private javax.swing.JTextField jTextField2; private javax.swing.JTextField jTextField3; private javax.swing.JTextField jTextField4; // End of variables declaration > 

Источник

Оцените статью