0

I have a code that opens up a JOptionPane dialog box once the user hits a button. The first thing I want to do is close the first JFrame as soon as the user hits one of the buttons. I have tried doing this

setVisible(false); // Delete visibility 
        dispose(); //Delete window

but the original JFrame doesn't close as soon as a button is pressed. The goal is to have two buttons. When one is pressed, show a JOptionPane box, while simultaneously closing the first JFrame window. How can I do that? Next, after the ok is pushed in the new JOptionPane I wan't to stimulate what would be an action listener. I do this by calling the method

sinceyoupressedthecoolbutton();

right after my JOptionPane declaration. Here is where the second issue starts, it displays the JOptionPane perfectly, but doesn't go to the method

sinceyoupressedthecoolbutton();

I don't know if the problem is in the method calling or the content of the method. Basically, after ok is pressed on the JOptionPane, I wan't to move to another method, which opens a new JLabel.

Here is the code:

package Buttons;

import java.awt.Dimension;
import java.awt.FlowLayout; //layout proper
import java.awt.event.ActionListener; //Waits for users action
import java.awt.event.ActionEvent; //Users action
import javax.swing.JFrame; //Window
import javax.swing.JLabel;
import javax.swing.JButton; //BUTTON!!!
import javax.swing.JDialog;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane; //Standard dialogue box

public class ButtonClass extends JFrame {

private JButton regular;
private JButton custom;

public ButtonClass() { // Constructor
    super("The title"); // Title
    setLayout(new FlowLayout()); // Default layout

    regular = new JButton("Regular Button");
    add(regular);


    custom = new JButton("Custom", b);

    add(custom);

    Handlerclass handler = new Handlerclass();
    Otherhandlerclass original = new Otherhandlerclass();
    regular.addActionListener(handler);
    custom.addActionListener(original);

   //THIS WAS MY FIRST PROBLEM, I WANT TO CLOSE THE FIRST JFRAME WINDOW AS THE USER HITS OK
    setVisible(false); // Close the show message dialog box
    dispose();

}

public class Handlerclass implements ActionListener { // This class is
                                                        // inside the other
                                                        // class

    public void actionPerformed(ActionEvent eventvar) { // This will happen
                                                        // when button is
                                                        // clicked
        JOptionPane.showMessageDialog(null, String.format("%s", eventvar.getActionCommand())); //opens a new window with the name of the button
    }
}

public class Otherhandlerclass implements ActionListener {

    public void actionPerformed(ActionEvent e) {

        JOptionPane.showMessageDialog(null,"Since you pressed that button, I will open a new window when you press ok, okay?");

        //Code works up until here 

        sinceyoupressedthecoolbutton(); //THIS METHOD SHOULD OPEN A NEW WINDOW! 
    }

    public void sinceyoupressedthecoolbutton() {

        JDialog YES = new JDialog();
        JLabel label = new JLabel("Here is that new window I promised you!");
        add(label);
        YES.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
        YES.setSize(500, 500);
        YES.setVisible(true);

    }

    public class okay implements ActionListener {
        public void actionPerformed(ActionEvent ok) {

        }

    }

}

}

Help would be appreciated!

Ruchir Baronia
  • 7,406
  • 5
  • 48
  • 83

1 Answers1

1

You're working in a Event Driven Environment, not a linear processing one. This means that you run some code and then wait (the waiting is done for you) for some event to occur and then you respond to it...

Nothing can happen till the user presses the custom button, so it's pointless trying to close the frame before then

When your Otherhandlerclass's actionPerformed is triggered, obviously you show the JOptionPane pane, this will block the flow of the execution until it's closed. At this point, you should have the opportunity to the dispose of the original window.

So instead of calling setVisible(false) and dispose in your constructor, it would be better to move it to Otherhandlerclass

public class Otherhandlerclass implements ActionListener {

    public void actionPerformed(ActionEvent e) {

        dispose();
        JOptionPane.showMessageDialog(null, "Since you pressed that button, I will open a new window when you press ok, okay?");

        sinceyoupressedthecoolbutton();
    }

    public void sinceyoupressedthecoolbutton() {

        JDialog YES = new JDialog();
        JLabel label = new JLabel("Here is that new window I promised you!");
        YES.add(label);
        YES.setSize(500, 500);
        YES.setVisible(true);

    }

    public class okay implements ActionListener {

        public void actionPerformed(ActionEvent ok) {

        }

    }

}

Having said that, I'd encourage you to have a read of The Use of Multiple JFrames, Good/Bad Practice? and maybe consider using something like How to Use CardLayout instead

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thank you for the feedback, now that I have moved the dispose() method out of my constructor, the windows are closing when "ok" is pressed. Now there is another issue though. My sinceyoupressedthecoolbutton() method is not executing. It should display a JDialog that has the JLabel "Here is that new window I promised you!" This is not displaying after "ok" is pushed though. How should I fix this? – Ruchir Baronia Aug 19 '15 at 04:34
  • `YES.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);` can't be applied to a dialog – MadProgrammer Aug 19 '15 at 04:37
  • Thank you, so JDialog already has EXIT_ON_CLOSE feature built in? Now after I push "ok" a new window opens up, but it is blank...It should have a JLabel saying "Here is that new window I promised you!" Why is it blank? – Ruchir Baronia Aug 19 '15 at 04:40
  • 1. No, `JDialog` can't be used to close an application, it's meant as only a temporary/short lived window. 2. You're adding the label to the original window, not the dialog. – MadProgrammer Aug 19 '15 at 04:44
  • Then how do I add the label to the dialog? This is my first time using JDialog, and I am only using it because I know that using multiple JFrames is not good practice. Could you tell me how I can add the label? – Ruchir Baronia Aug 19 '15 at 04:46
  • The same way you add any component to a container. `YES.add(label);`, using dialogs instead of frames is the same problem you're trying to avoid, multiple windows – MadProgrammer Aug 19 '15 at 04:48
  • Thank you, do you have any suggestions of what other type of window I should use in this program? – Ruchir Baronia Aug 19 '15 at 04:50
  • Also how can I put a title, like this? YES.super("NEW WINDOW!"); – Ruchir Baronia Aug 19 '15 at 04:51
  • If your goal is to avoid using multiple windows, the a [`CardLayout`](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) or [`JTabbedPane`](http://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html) would be a better solution. To set the title of window, use `setTitle` – MadProgrammer Aug 19 '15 at 04:56
  • Thank you so much for your time and help; IT WORKED! I appreciate your commitment in helping out a fellow java programmer! – Ruchir Baronia Aug 19 '15 at 04:57
  • Glad it could help ;) – MadProgrammer Aug 19 '15 at 05:00