Howcan
Howcan

Reputation: 313

Display pop-up message window in Java?

I read about JDialogs and JOptionPane messages but I still can't get it to work. I have a GUI class that extends JFrame. All I want to do is have a popup at the beginning of my program which informs the user about a couple of things. In my main I create the following gui:

GUI g = new GUI();

Right after that I was to display the window. I have tried the following in the main method:

JOptionPane.showMessageDialog(g, "work?");
JOptionPane.showMessageDialog(frame, "work?"); //(frame was used in documentation example so I tried it)

I also tried to add the pop up into the GUI class with the following

JOptionPane.showMessageDialog(this, "work?"); //(I'm not exactly sure what the Frame Owner parameter is supposed to be, unless I'm confusing this with JDialog.)

In any case, how would I make this window appear? Every single one of the methods I tried compiled, and nothing happened.

public class GUI extends JFrame implements ActionListener{
     private Container background;
     private static buttons etc...
     private static JLabel disp,edisp;
     private static JTextArea info;
     //setting up the GUI for my program, adding action listeners, I can post more if    necessary
}

And then I have the main where I want to call the pop up window

public static void main(String[] args){
    GUI g = new GUI();
    JOptionPane.showMessageDialog(g,"Work?");
}

Upvotes: 2

Views: 23719

Answers (1)

Otanan
Otanan

Reputation: 448

Make sure that these are called near the beginning, be it in the main method or not. Also, try just setting the first parameter as null. So it reads:

JOptionPane.showMessageDialog(null,"Work?");

Also, remember to import it!

Upvotes: 4

Related Questions