JOptionPane showInputDialog position

How to specify the position of JOptionPane. Can someone create a class that extends JOptionPane.showInputDialog, which also takes the position x, y?

+8
java swing joptionpane
source share
2 answers

You can use the JOptionPane method setLocation (...) . OR Instead of using JOptionPane you can extend the JDialog and then specify its location on the screen.

Here is one working code example recommended by @HovercraftFullOfEels, only this example will help you get user input for your request:

 import javax.swing.*; public class OptionPaneLocation { private void createAndDisplayGUI() { JOptionPane optionPane = new JOptionPane("Its me" , JOptionPane.PLAIN_MESSAGE , JOptionPane.DEFAULT_OPTION , null, null, "Please ENTER your NAME here"); optionPane.setWantsInput(true); JDialog dialog = optionPane.createDialog(null, "TEST"); dialog.setLocation(10, 20); dialog.setVisible(true); System.out.println(optionPane.getInputValue()); } public static void main(String... args) { Runnable runnable = new Runnable() { public void run() { new OptionPaneLocation().createAndDisplayGUI(); } }; SwingUtilities.invokeLater(runnable); } } 
+7
source share

A JOptionPane can easily be turned into a JDialog (look at the JOptionPane API and it will show you as an example). Then you can set the position of the created JDialog.

e.g. from the documentation:

  JOptionPane pane = new JOptionPane(arguments); pane.set.Xxxx(...); // Configure JDialog dialog = pane.createDialog(parentComponent, title); dialog.setLocation(....); // added! dialog.setModal(....); // added! Do you want it modal or not? // .... dialog.setVisible(true); 
+5
source share

All Articles