Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Java Swing Input Dialog Always on top

Tags:

java

dialog

swing

If I need to make my JOptionPane always on the top of everything, this is the way I can do it:

JOptionPane optionPane = new JOptionPane();
JDialog dialog = optionPane.createDialog("My Dialog");
dialog.setAlwaysOnTop(true);
dialog.setVisible(true);

But what about the input dialog of JOptionPane? Can we do similar thing to make sure the below input dialog appear on top?

JOptionPane.showInputDialog(null,"My Input");

Please note my application is not a GUI one, so I cannot set the parent window.

like image 718
Tharaka Deshan Avatar asked Sep 13 '25 14:09

Tharaka Deshan


1 Answers

Finally I found the solution by digging down the JOptionPane class itself.

JOptionPane has a method called setWantsInput() which we need to set true, to make it a input dialog.

Here is a working code:

JOptionPane optionPane = new JOptionPane("body of the dialog");
optionPane.setWantsInput(true); //this is what I added
JDialog dialog = optionPane.createDialog("Title Of the Dialog");
dialog.setAlwaysOnTop(true);
dialog.setVisible(true);
dialog.dispose();
System.out.println(optionPane.getInputValue());
like image 181
Tharaka Deshan Avatar answered Sep 16 '25 05:09

Tharaka Deshan