Question

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?

Was it helpful?

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.

OTHER TIPS

Try making a method that takes different "JComponents" as a parameter and set the size that way. Since JComponents are superclass of JOptionPane and others like it, you should be able to pass in a component parameter and set its size inside the method. I haven't tried it but It's something to look into.

setComponentSize(JComponent jc){
    jc.setPreferredSize(new Dimension(300, 300));//set size here
    jc.setLocation(new Point(10, 10)); //set location here
    //do other stuf partaining to certain components
    //like JOptionPanes, JDialogs
    if(jc instnceof JOptionPane){
        //stuff
    }else if(jc instance of JDialog){
        jc.show();
    }
}

JOptionPane pane = new JOptionPane();
JDialog dialog = pane.createDialog("ej");

setComponentSize(dialog);

of course if you do other stuff with each type you can rename the method. I found something you might check on for max sizes too. JComponent: how to set maximum width?

If it doesn't compile for some reason check this one out.Why doesn't instanceof work with JPanel and JComponent?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top