How to close a JFrame based window with a JButton click event

38,014

Solution 1

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.

Solution 2

Your createGUI() method is a little confused, it creates ANOTHER instance of this class and you end up with one instance with a frame and one instance without. Minimal change to get it working is to change your createGUI method:

  public void createGUI(Object obj) {
    //Create and set up the frame.
    frame = new JFrame("PopUp Dialog");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//    //create and setup the content pane
//    UpgradePopupWindow popUpContentPane = new UpgradePopupWindow(obj);

    setOpaque(true);
    frame.setContentPane(this);

    frame.pack();
    frame.setVisible(true);
  }
Share:
38,014
LambeauLeap
Author by

LambeauLeap

Updated on June 25, 2020

Comments

  • LambeauLeap
    LambeauLeap almost 4 years

    I am new to the whole Java Swing/AWT dialogs (which explains how amateurish this dialog below looks like, any help in arranging it better is welcome :) and I am struggling to close this popUp window when any of the two JButtons are clicked.

    I have already tried options like frame.dispose(), frame.setVisible(false) and even SwingUtilities.getWindowAncestor(this).dispose();

    Again, this is a secondary popup window invoked by another main process running, so I just want this particular popUp window to close and not affect the main process. Otherwise I could use System.exit

    Like I mentioned, any other suggestions to improve the overall look and feel of the dialog are appreciated.

    My entire code is as below:

    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import javax.swing.SwingUtilities;
    
    public 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(Object ft) {
        String text = "This is the random text for now. I will bother about the actual content later";
        addLabel(text, Font.PLAIN, 12);
    
        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 void createGUI(Object obj) {
        //Create and set up the frame.
        frame = new JFrame("PopUp Dialog");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //create and setup the content pane
        UpgradePopupWindow popUpContentPane = new UpgradePopupWindow(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();
        }
      }
    }