Reputation: 25
I am trying to separate some of my code so I can have a reusable class for different projects. The class I have right now is called MainFrame all that it is doing right now is creating a window with a JMenuBar that has one JMenu File. That menu has one item JMenuItem Exit.
I'm trying to get the WindowListener class to work from my menu bar to be able to do the dispose() and System.gc() when closing the application.
I've been told this is a cleaner way to exit a application then System.exit(0);
public class MainFrame extends JFrame {
private MenuBar menuBar;
public MainFrame() {
super("Sales");
menuBar = new MenuBar();
setLayout(new BorderLayout());
setJMenuBar(createMenuBar());
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.out.println("test");
dispose();
System.gc();
}
});
setMinimumSize(new Dimension(500, 400));
setSize(600, 500);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
private JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem exitItem = new JMenuItem("Exit");
fileMenu.add(exitItem);
menuBar.add(fileMenu);
exitItem.setMnemonic(KeyEvent.VK_X);
exitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
exitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
WindowListener[] listeners = getWindowListeners();
for(WindowListener listener: listeners) {
listener.windowClosing(new WindowEvent(MainFrame.this, 0));
}
}
});
return menuBar;
}
}
Here are the two classes I'm trying to create.
public class MainFrame extends JFrame {
private MenuBar menuBar;
public MainFrame() {
super("Sales");
menuBar = new MenuBar();
setLayout(new BorderLayout());
setJMenuBar(menuBar.getMenuBar());
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.out.println("test");
dispose();
System.gc();
}
});
setMinimumSize(new Dimension(500, 400));
setSize(600, 500);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
}
and
public class MenuBar extends JMenuBar {
private JMenuBar menuBar;
private JMenu fileMenu, settingsMenu, helpMenu;
public MenuBar() {
menuBar = new JMenuBar();
setFileMenu();
menuBar.add(fileMenu);
}
//// Method to return the menu bar
public JMenuBar getMenuBar() {
return menuBar;
}
//// Private methods to set up the menu bar
private void setFileMenu() {
fileMenu = new JMenu("File");
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.setMnemonic(KeyEvent.VK_X);
exitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
fileMenu.add(exitItem);
exitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
WindowListener[] listeners = getWindowListeners();
for(WindowListener listener: listeners) {
listener.windowClosing(new WindowEvent(MainFrame.this, 0));
}
}
});
}
Any advice on how to get the WindowListener to work from the MenuBar class?
Upvotes: 1
Views: 598
Reputation: 324207
listener.windowClosing(new WindowEvent(MainFrame.this, 0));
If you want to generate an event then you need to use the dispatchEvent(...) method of the Component class. So you would end up dispatching the event to the window.
The basic code for this is:
Window window = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();
if (window != null)
{
WindowEvent windowClosing = new WindowEvent(window, WindowEvent.WINDOW_CLOSING);
window.dispatchEvent(windowClosing);
}
Also, you can get rid of the WindowListener and then just change the default close operation to:
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
The window will be disposed and if it is the last window open then the JVM will also exit.
Upvotes: 3