Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / September 2005

Tip: Looking for answers? Try searching our database.

Positioning dialogboxes created with JOptionPane

Thread view: 
Sameer - 18 Sep 2005 17:03 GMT
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 - 18 Sep 2005 18:57 GMT
>The dialog boxes created using JOptionPane like showConfirmDialog,
>showInputDialog gets displayed in the centre of the screen. I need to change
[quoted text clipped - 3 lines]
>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?
Signature

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

Sameer - 18 Sep 2005 20:46 GMT
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 - 18 Sep 2005 22:48 GMT
>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.

Signature

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

Thomas Fritsch - 19 Sep 2005 12:08 GMT
Sameer schrieb:

> Consider some methods of JOptionPane:
> 1) static int showConfirmDialog(Component parentComponent, Object
[quoted text clipped - 9 lines]
>
> 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.

Signature

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

Sameer - 22 Sep 2005 21:31 GMT
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.
Thomas Fritsch - 18 Sep 2005 22:49 GMT
> 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?

Signature

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

Sameer - 19 Sep 2005 10:34 GMT
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.
Roedy Green - 20 Sep 2005 07:58 GMT
>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.

Signature

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

E11 - 20 Sep 2005 16:59 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.