How to capture a JFrame's close button click event?
import javax.swing.JOptionPane;
import javax.swing.JFrame;
/*Some piece of code*/
frame.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
if (JOptionPane.showConfirmDialog(frame,
"Are you sure you want to close this window?", "Close Window?",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION){
System.exit(0);
}
}
});
If you also want to prevent the window from closing unless the user chooses 'Yes', you can add:
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
Java jFrame close button
First you need to set the JFrame's default close action to do nothing on close:
myJFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
Then pressing the close button will not close the JFrame, and you will have to dispose of it in code.
Closing jFrame after clicking JButton
Assuming your button has an actionListener, after clicking the "rules button" put in:
JFrame1.dispose(); //Remove JFrame 1
JFrame2.setVisible(true) //Show other frame
And then reverese them for the opposite reaction
How to close a JFrame based window with a JButton click event
The problem is that your createGUI method is not static. So I imagine you are creating first a UpgradePopupWindow, calling createGUI on that, which in turn creates a enw UpgradePopupWindow.
Try this instead:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TempTest
{
public static void main(String[] args)
{
UpgradePopupWindow.createGUI(null);
}
}
class UpgradePopupWindow extends JPanel implements ActionListener {
public static UpgradePopupWindow mainWindow;
static final long serialVersionUID = 0;
final String upgrade = "Continue Upgrade";
final String restore = "Restore";
JPanel panels;
JButton flashMe;
JButton helpMe;
JTextArea Message;
JFrame frame;
protected JTextArea addText(String text, boolean visible, int fontStyle) {
JTextArea textArea = new JTextArea(text);
textArea.setFont(new Font("SansSerif", fontStyle, 12)); //$NON-NLS-1$
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setEditable(false);
textArea.setForeground(Color.WHITE);
textArea.setOpaque(false);
textArea.setVisible(visible);
textArea.setAlignmentX(Component.CENTER_ALIGNMENT);
add(textArea);
return textArea;
}
public UpgradePopupWindow(JFrame frm, Object ft2) {
String text = "This is the random text for now. I will bother about the actual content later";
addLabel(text, Font.PLAIN, 12);
frame = frm;
flashMe = new JButton(upgrade);
flashMe.setActionCommand("upgrade");
flashMe.addActionListener(this);
flashMe.setEnabled(true);
add(flashMe);
helpMe = new JButton(restore);
helpMe.setActionCommand("restore");
helpMe.addActionListener(this);
helpMe.setEnabled(true);
add(helpMe);
}
protected JLabel addLabel(String text, int fontStyle, int size) {
JLabel label = new JLabel(text);
label.setFont(new Font("SansSerif", fontStyle, size));
label.setAlignmentX(Component.CENTER_ALIGNMENT);
label.setOpaque(false);
label.setVisible(true);
label.setForeground(Color.BLUE);
add(label);
return label;
}
public static void createGUI(Object obj) {
//Create and set up the frame.
JFrame frame = new JFrame("PopUp Dialog");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//create and setup the content pane
UpgradePopupWindow popUpContentPane = new UpgradePopupWindow(frame, obj);
popUpContentPane.setOpaque(true);
frame.setContentPane(popUpContentPane);
frame.pack();
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if("restore".equals(e.getActionCommand())) {
System.out.println("restore button selected");
frame.dispose();
SwingUtilities.getWindowAncestor(this).dispose();
} else if ("upgrade".equals(e.getActionCommand())) {
System.out.println("upgrade button selected");
frame.dispose();
}
}
}
The main change is that the createUI is static, and the UpgradePopupWindow takes the frame in the constructor.
Popup for JFrame close button
You can do it by following steps:
Replace the line
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
withframe.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
Implement
WindowListener
and override its all abstract methods. You can find it here.Override the
public void windowClosing(WindowEvent e)
method some this way:@Override
public void windowClosing(WindowEvent e){
int result = JOptionPane.showConfirmDialog(null, "Are you sure,"Confirm",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
if(result == JOptionPane.YES_OPTION){
System.exit(0);
}else{
//Do nothing
}
}
Using the exit button for executing a function
Have you tried adding a actionListener to the X?
Like it's done here:
Java Swing adding Action Listener for EXIT_ON_CLOSE
How to write a code for default close button in JFrame?
Check out Closing an Application for a couple of solutions:
- using a
WindowListener
and overriding thewindowClosing(...)
event to display your option pane. - using a simple API so you only need to provide a message or write an ActionListener for more complicated processing.
On Close button action Java
You need to add a WindowAdapter
to your JFrame
.
myFrame.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e){
// do something
}
});
Now, every time someone presses the close button, the windowClosing()
method will be called. Check if the user has saved the work. If not, either auto-save it like or promprt user to save it.
Related Topics
How to Disable or Bypass Hardware Graphics Acceleration(Prism) in Javafx
Log4J: How to Use Socketappender
Utf-8 Text Is Garbled When Form Is Posted as Multipart/Form-Data
JSONmanagedreference VS JSONbackreference
Differencebetween Double.Parsedouble(String) and Double.Valueof(String)
Validation of a List of Objects in Spring
Converting Array of Primitives to Array of Containers in Java
How to Automate Drag & Drop Functionality Using Selenium Webdriver Java
Why Is Javac Failing on @Override Annotation
Java - Reading, Manipulating and Writing Wav Files
Java Interfaces Methodology: Should Every Class Implement an Interface
Jackson JSON Deserialization with Root Element
Javafx Using Objects from Maincontroller or Other Controllers in Proper Controller Class
How to Correctly Decode Unicode Parameters Passed to a Servlet
Spring Autowiring Class VS. Interface
Passing Directly an Array Initializer to a Method Parameter Doesn't Work
What Is Better: Multiple "If" Statements or One "If" with Multiple Conditions