Search code examples
javajoptionpane

Defining size and location for JOptionPane


I'm trying to write a program where I use JOptionPane at different places in the code.

Example:

String btcRate = JOptionPane.showInputDialog(screen, "insert \ntodays rate!");
JOptionPane.showMessageDialog(screen, "Name or password incorrect");

I'd like to have some kind of class or method that defines all my JOptionPanes, so that they all have a max width of 260 and 150 height and are at the same location on the screen. Is this possible?

I've looked at code like this:

JOptionPane pane = new JOptionPane();
JDialog dialog = pane.createDialog("ej");
dialog.setSize(300,300);
dialog.locate(10, 10);
dialog.show();

But how do I use it on for example showInputDialog, and can I make a class or method to handle them all?


Solution

  • But how do I use it on for example showInputDialog

    You can't. You can:

    1. use the showInputDialog convenience method to display the dialog in which case you need to live with the default behaviour.

    2. use code like you posted above to gain the benefits of most of the work being done for you

    3. create your own custom JDialog class that does exactly what you want.