Reputation: 864
I have a couple of classes but I am only posting this Main class I have. Most of this code was taken from my professor's sample program. My problem is I want to run a check ONCE the window button is closed. The 'check' is going to see if a Boolean variable 'saved' from another class is True or False, and act accordingly. THEN, close the window. So far I have this, but I am getting an error from the JFrameL. How do I use the WindowClosing method from another class? I see that I am not supposed to call it on its own?
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Main {
public static JFrameL frame;
public static void main(String[] args) {
// Create a window.
frame = new JFrameL("Checking Account Actions");
// Set the size of the window.
frame.setSize(450, 350);
frame.setAlwaysOnTop(true);
// Specify what happens when the close button is clicked.
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
CheckingAccountActions panel = new CheckingAccountActions();
frame.getContentPane().add(panel);
frame.pack();
// Display the window
frame.setVisible(true);
}
public class JFrameL extends JFrame {
public JFrameL(String title) {
super(title);
FrameListener listener = new FrameListener();
addWindowListener(listener);
}
private class FrameListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.out.println("WindowListener method called: windowClosed.");
if(!CheckingAccountActions.saved) {
String message = "The data in the application is not saved.\n"
+ "Would you like to save it before exiting the application?";
int confirm = JOptionPane.showConfirmDialog (null, message);
if (confirm == JOptionPane.YES_OPTION)
CheckingAccountActions.chooseFile(2);
}
Main.frame.setVisible(false);
System.exit(0);
}
}
}
}
Error message I am receiving on line 2 from main():
"No enclosing instance of type Main is accessible. Must qualify the allocation with an enclosing instance of type Main (e.g. x.new A() where x is an instance of Main)."
Upvotes: 1
Views: 1224
Reputation: 285403
e.g.,
class MyWindowAdapter extends WindowAdapter {
private CheckingAccountActions checkActions;
public MyWindowAdapter(CheckingAccountActions checkActions) {
this.checkActions = checkActions;
}
// in your window closing method
// check the state of checkActions first before doing anything
public void windowClosing(WindowEvent e) {
// note -- don't check for saved in a static way
// use a method on the instance.
if(!checkActions.getSaved()) {
// etc...
}
JFrame frame = (JFrame)e.getSource();
frame.setVisible(false);
// Main.frame.setVisible(false);
System.exit(0);
}
}
Then in main:
public static void main(String[] args) {
frame = new JFrame("Checking Account Actions");
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
CheckingAccountActions panel = new CheckingAccountActions();
MyWindowAdapter winAdapter = new MyWindowAdapter(panel);
frame.addWindowListener(winAdapter);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
Edit
This smells of static over-use: CheckingAccountActions.saved
.
Upvotes: 3
Reputation: 9625
I suspect what you need to do is move panel
so you can access it later.
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Main {
public static JFrameL frame;
public static CheckingAccountActions panel;
public static void main(String[] args) {
// Create a window.
frame = new JFrameL("Checking Account Actions");
// Set the size of the window.
frame.setSize(450, 350);
frame.setAlwaysOnTop(true);
// Specify what happens when the close button is clicked.
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
panel = new CheckingAccountActions();
frame.getContentPane().add(panel);
frame.pack();
// Display the window
frame.setVisible(true);
}
public class JFrameL extends JFrame {
public JFrameL(String title) {
super(title);
FrameListener listener = new FrameListener();
addWindowListener(listener);
}
private class FrameListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.out.println("WindowListener method called: windowClosed.");
if(!panel.saved) {
String message = "The data in the application is not saved.\n"
+ "Would you like to save it before exiting the application?";
int confirm = JOptionPane.showConfirmDialog (null, message);
if (confirm == JOptionPane.YES_OPTION)
panel.chooseFile(2);
}
Main.frame.setVisible(false);
System.exit(0);
}
}
}
Upvotes: 0