0

Got a problem that i hope some could help me with. I'm using a JOptionPane in my program, but when i run the class, the first JOptionPane will pop up in the background, so you don't see it in eclipse untill you alt + tab and find it.

How do i get it so it runs up front?

My code of the GUI class, that will create my pane.

import java.io.IOException;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JFrame;

public class GUI {

@SuppressWarnings("unused")
public void getConfirmation() throws IOException {

    JFrame frame = new JFrame();        
    Object[] options = {"Continue","Stop procedure"};

    int n = JOptionPane.showOptionDialog(frame,
            "Would you like to continue or stop procedure?",
            "Plese confirm",
            JOptionPane.YES_NO_OPTION,
            JOptionPane.QUESTION_MESSAGE,
            null,
            options,
            options[0]);
            frame.toFront();
            frame.repaint();
            if(JOptionPane.YES_OPTION == 1) {

                // Program will keep going
            }

            else if (JOptionPane.NO_OPTION == 0) {

                System.out.println(" ");
                System.out.println("Procedure terminated");
                System.out.println(" ");
            }



}

}

I run the GUI class from another class like this: here you see some of the code from my client class.

System.out.println("You received " + fromServer + " from the server");
System.out.println("Please answer confirmationbox" + "\n\n");
GUI.getConfirmation();

But i found out, its only the first time you run it that it will start in background, after that i runs like normal when i try to run it again.

Pixel
  • 349
  • 1
  • 8
  • 17

1 Answers1

1

Try

frame.setAlwaysOnTop(true);

For more info have a look at Window#setAlwaysOnTop()

OR

Try with null parent as defined below:

int n = JOptionPane.showOptionDialog(null,
        "Would you like to continue or stop procedure?", "Plese confirm",
        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);

For more info look at JOptionPane

Defines the Component that is to be the parent of this dialog box. It is used in two ways: the Frame that contains it is used as the Frame parent for the dialog box, and its screen coordinates are used in the placement of the dialog box. In general, the dialog box is placed just below the component. This parameter may be null, in which case a default Frame is used as the parent, and the dialog will be centered on the screen (depending on the L&F).


--EDIT--

If nothing is working then find the answer at How to Make Dialogs.

Braj
  • 46,415
  • 5
  • 60
  • 76
  • still runs in the back and i still have to alt + tab to find it :( – Pixel Apr 16 '14 at 21:57
  • Look at [JOptionPane won't show its dialog on top of other windows](http://stackoverflow.com/questions/438463/joptionpane-wont-show-its-dialog-on-top-of-other-windows) – Braj Apr 16 '14 at 22:04
  • Well, i guess i have to make a dummy frame that can run first and just close after? you know how to make such frame? – Pixel Apr 16 '14 at 22:10
  • A sample is posted by @Walter in above link. Please have a look – Braj Apr 16 '14 at 22:11