Positioning dialogboxes created with JOptionPane  
Author Message
Sameer





PostPosted: 2005-9-19 0:05:00 Top

java-programmer, Positioning dialogboxes created with JOptionPane The dialog boxes created using JOptionPane like showConfirmDialog,
showInputDialog gets displayed in the centre of the screen. I need to change
its position. How to do this?
These methods return int or none.
How to get access to the dialog boxes so that they
can be positioned by using appropriate method? or I need to create a custom
dialog box so that they can be positioned?




 
Roedy Green





PostPosted: 2005-9-19 1:57:00 Top

java-programmer >> Positioning dialogboxes created with JOptionPane On Sun, 18 Sep 2005 21:35:24 +0530, "Sameer"
<email***@***.com> wrote or quoted :

>The dialog boxes created using JOptionPane like showConfirmDialog,
>showInputDialog gets displayed in the centre of the screen. I need to change
>its position. How to do this?
>These methods return int or none.
>How to get access to the dialog boxes so that they
>can be positioned by using appropriate method? or I need to create a custom
>dialog box so that they can be positioned?

JOptionPane inherits from JComponent, hence it has the usual
setLocation, setX and setY methods. What happens when you try those?
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
Sameer





PostPosted: 2005-9-19 3:47:00 Top

java-programmer >> Positioning dialogboxes created with JOptionPane Consider some methods of JOptionPane:
1) static int showConfirmDialog(Component parentComponent, Object
message, String title, int optionType, int messageType, Icon icon)

2) static String showInputDialog(Component parentComponent, Object
message)

These are the methods which return int and String respectively.
If static methods doing the work to show dialog box, then how do i get
instance of JOptionPane to operate on i.e. to apply the method
setLocation etc.-

Please explain with the help of a code-snippet.

 
 
Roedy Green





PostPosted: 2005-9-19 5:49:00 Top

java-programmer >> Positioning dialogboxes created with JOptionPane On 18 Sep 2005 12:46:31 -0700, "Sameer" <email***@***.com>
wrote or quoted :

>Please explain with the help of a code-snippet.

Try setLocation on the JOptionPane to humour me. It is possible it
propagates that information to Windows it pops up in a way similar to
the way other nested components do.

Another approach to the problem is to too study the code for
JOptionPane to see how it determines where to pop up. That may give
you an hint as to how you might influence it.

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
 
Thomas Fritsch





PostPosted: 2005-9-19 5:54:00 Top

java-programmer >> Positioning dialogboxes created with JOptionPane "Sameer" <email***@***.com> wrote:
> The dialog boxes created using JOptionPane like showConfirmDialog,
> showInputDialog gets displayed in the centre of the screen. I need to
> change
> its position. How to do this?
All the static JOptionPane.show???Dialog methods take a
Component parentComponent
as their first parameter. The created dialog will be centered relative to
that parentComponent.
So it is important to to give a suitable component there, like
JOptionPane.show???Dialog(component, ...);
or better
JOptionPane.show???Dialog(JOptionPane.getFrameForComponent(component),
...);
It seems that you gave null as parentComponent, because then the dialog will
be centered in the screen.

> These methods return int or none.
> How to get access to the dialog boxes so that they
> can be positioned by using appropriate method? or I need to create a
> custom
> dialog box so that they can be positioned?

--
"TFritsch$t-online:de".replace(':','.').replace('$','@')


 
 
Sameer





PostPosted: 2005-9-19 17:34:00 Top

java-programmer >> Positioning dialogboxes created with JOptionPane The dialog box generated is a part of error-handling code and must be
executed to validate command line parameters.
Upto validation point there is no necessary to display GUI, so there is
no parent component to any of the dialog boxes so I have to pass null
as the parameter.

 
 
Thomas Fritsch





PostPosted: 2005-9-19 19:08:00 Top

java-programmer >> Positioning dialogboxes created with JOptionPane Sameer schrieb:

> Consider some methods of JOptionPane:
> 1) static int showConfirmDialog(Component parentComponent, Object
> message, String title, int optionType, int messageType, Icon icon)
>
> 2) static String showInputDialog(Component parentComponent, Object
> message)
>
> These are the methods which return int and String respectively.
> If static methods doing the work to show dialog box, then how do i get
> instance of JOptionPane to operate on i.e. to apply the method
> setLocation etc.-
>
> Please explain with the help of a code-snippet.
>
So, if the static JOptionPane-methods don't fit, use a constructor and
the non-static methods:
JOptionPane optionPane = new JOptionPane("message");
JDialog dialog = optionPane.createDialog(null, "title");
dialog.setLocation(100, 100);
dialog.setVisible(true);
Object value = optionPane.getValue();
int option;
if (value == null)
option = JOptionPane.CLOSED_OPTION;
else
option = ((Integer) value).intValue();

BTW: reading the javadoc of JOptionPane always helps.

--
"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')

 
 
Roedy Green





PostPosted: 2005-9-20 14:59:00 Top

java-programmer >> Positioning dialogboxes created with JOptionPane On 19 Sep 2005 02:34:24 -0700, "Sameer" <email***@***.com>
wrote or quoted :

>The dialog box generated is a part of error-handling code and must be
>executed to validate command line parameters.
>Upto validation point there is no necessary to display GUI, so there is
>no parent component to any of the dialog boxes so I have to pass null
>as the parameter.

A pragmatic person might say -- ok, pop up a parent, position the
parent then the child has to fit inside.

I will give my original answer for the third time. Try using the
setLocation methods on your JOptionPane. It IS a JComponent after all,
and that is how you position JComponents.


--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
 
E11





PostPosted: 2005-9-20 23:59:00 Top

java-programmer >> Positioning dialogboxes created with JOptionPane Roedy,

The setLocation in JOptionPane (or rather, Component, but inherited by
JOptionPane) is non-static. i.e. If you have an object instance of
JOptionPane, then you can call object.setLocation(...).

When you use the convenience static methods
JOptionPane.showConfirmDialog(...), they do not return you the handle
to the dialog box that is shown. Rather it only returns you the value
representing the user-input.

Sameer, if you have no other frame or dialog to centre the dialog box
against, i would suggest that you create a custom dialog class that
extends the JDialog. You can even make use of parameters and static
convenience methods to make it configurable, and work just like
JOptionPane.showConfirmDialog(...).



Regards,
Edwin

 
 
Sameer





PostPosted: 2005-9-23 4:32:00 Top

java-programmer >> Positioning dialogboxes created with JOptionPane Thanks!
At last the class which helped me to locate the dialog box
appropriately is:


import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JDialog;
import javax.swing.JOptionPane;

public class ErrorDialog {
String message;
String title;
int splashImageHeight;
int distanceFromSplashWindow;

public ErrorDialog(String title, String message, int splashHeight, int
distanceBetween) {
super();
distanceFromSplashWindow = distanceBetween;
this.message = message;
splashImageHeight = splashHeight;
this.title = title;

JOptionPane jop = new JOptionPane(message);
JDialog jdg = jop.createDialog(null, title);
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension screenDim = tk.getScreenSize();
int errW = screenDim.width / 2 - jdg.getWidth() / 2;
int errH = screenDim.height / 2 + splashImageHeight / 2+
distanceFromSplashWindow;
jdg.setLocation(errW, errH);
jdg.setVisible(true);
}

}

May be helpful to anybody.
Thanks again for posting for this thread.