0

I tried putting a JFrame into a JOptionPane using the following code:

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setSize(300, 75);
    //frame.add(textField);
    frame.setVisible(true);
    frame.requestFocus();

    String input = JOptionPane.showInputDialog(frame, "What is the number? ");

This works. However, when I click OK or Cancel in the option Pane I expected the frame to close as well. But it doesn't.

Therefore, I tried passing null to the option pane instead of passing frame. This works too but when I do this the dialog does not appear (flashing on bottom of screen) as it does when I pass a frame object.

So basically is there a way to:

  1. Either automatically close the frame when option pane button is clicked?
  2. Or make an option pane flash (and be focused) just like a frame? (Using .requestFocus does not work for some reason)
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Net Nanny
  • 53
  • 3
  • 10
  • Make your own "Option pane" using a separate JFrame. JOptionPanes are 100% seperate from your main program. – Wyatt Lowery Sep 04 '15 at 22:12
  • *"Using .requestFocus does not work for some reason)"* That method will fail under a number of circumstances, including if the component is not visible, so it won't work if called before the option pane becomes visible. An option pane is a modal dialog, which the result that if you put the call to the method immediately *after* the option pane is shown, that call will not be made until after the option pane is closed. More generally, this seems like an XY problem. See [What is the XY problem?](http://meta.stackexchange.com/q/66377) – Andrew Thompson Sep 04 '15 at 22:12
  • 2
    @WyattLowery *"Make your own "Option pane" using a separate JFrame."* That's a bad idea. See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) *"JOptionPanes are 100% seperate from your main program."* ..and I don't even understand what that means. Besides being a top level container (that can be dragged around the screen) which does not seem relevant here, I cannot think of any situation where that is 'right'. Please explain. – Andrew Thompson Sep 04 '15 at 22:15
  • @WyattLowery Wouldn't creating an own JDialog class be a better choice? And of course you can close the frame with a WindowListener added to the dialog (and ActionListener to the buttons of the dialog)! – Lukas Rotter Sep 04 '15 at 22:31
  • Have you tried disposing the frame after the joptionpane returns? – MadProgrammer Sep 04 '15 at 22:41
  • 1
    I'm betting that this is a combination console / GUI program where you're getting information from the user from both the IDE's console, and a GUI or JOptionPane and that your console is covering your JOptionPane. If so, then make the program a pure GUI and only get information from the user via the GUI. – Hovercraft Full Of Eels Sep 04 '15 at 22:44
  • @AndrewThompson I set OptionPane to visible but requestFocus still does not works.. – Net Nanny Sep 06 '15 at 01:49
  • For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Sep 07 '15 at 03:28

0 Answers0