Search code examples
javaswinguser-interfacewindowjoptionpane

Show JOptionPane (with dropdown menu) on the top of other windows


I am working on a program that shows the following menu (menu_image) when it starts. I have a little problem: I'd want to show it on the top of the other windows, but I am not able to achieve this.

class Menu {
    public String showMenu(){
        Object[] options = {"option1", "option2", "option3"};
        Object selectionObject = JOptionPane.showInputDialog(null, "Choose", "Menu", JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
        String selectionString = selectionObject.toString();
        return selectionString;
    }
}

Can someone help me, please? Thank you in advance


Solution

  • Based on Berger's suggestion, I solved my problem in the following way...

    class Menu {
        public String showMenu(){
            //i solved my problem adding the following 2 lines of code...
            JFrame frame = new JFrame();
            frame.setAlwaysOnTop(true);
    
            Object[] options = {"option1", "option2", "option3"};
            //...and passing `frame` instead of `null` as first parameter
            Object selectionObject = JOptionPane.showInputDialog(frame, "Choose", "Menu", JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
            String selectionString = selectionObject.toString();
            return selectionString;
        }
    }