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 / General / July 2006

Tip: Looking for answers? Try searching our database.

JOptionPane window forced in foreground

Thread view: 
giangiammy@gmail.com - 26 Jul 2006 09:19 GMT
Hi all,

I'm using
JOptionPane.showMessageDialog(frame, s);
to show a message, and let the user press the OK button.
The behaviour I'd like is that the pop up window remaind
in front of all the current windows, and if I click on a background
window is cannot hide my optionpane window.

I have not find an option to do this, but I think it should be
present!
Any idea how I can do this?
(or link to examples)

thanks
giammy
Vincent van Beveren - 26 Jul 2006 09:39 GMT
> JOptionPane.showMessageDialog(frame, s);
> to show a message, and let the user press the OK button.
[quoted text clipped - 6 lines]
> Any idea how I can do this?
> (or link to examples)

It should actually work like that. Are you sure you specified the
correct frame argument? When you specify the frame argument correctly
its behavour is like you specified. If that doesn't work, you either
pass null or the wrong window in the frame argument.

Vincent
giangiammy@gmail.com - 26 Jul 2006 09:48 GMT
hi,

I'm almost a newbie in java: the code I used is
the following:

void showWarning(String s) {
       java.awt.Frame frame = new java.awt.Frame();
       JOptionPane.showMessageDialog(frame, s);
 }

so, my error is in creating the frame?
how should I create it?

thanks again
giammy

Vincent van Beveren ha scritto:

> > JOptionPane.showMessageDialog(frame, s);
> > to show a message, and let the user press the OK button.
[quoted text clipped - 13 lines]
>
> Vincent
Vincent van Beveren - 26 Jul 2006 10:09 GMT
> I'm almost a newbie in java: the code I used is
> the following:
[quoted text clipped - 6 lines]
> so, my error is in creating the frame?
> how should I create it?

Yes, this will cause the problem you describe. The frame you specify is
not the parent frame, but a newly created instance that is not visible.
You will not be able to focus on the newly created invisible frame....
unfortunatly no one will notice since its invisible... You must pass the
actual parent frame of the component (button/menu item?) that caused the
message.

If the showWarning(String s) method is defined in a JFrame (of JDialog)
class you can do the following:

void showWarning(String s) {
    JOptionPane.showMessageDialog(this, s);
}

If the method is defined outside a frame, you must pass it as an argument:

void showWarning(JFrame frame, String s) {
    JOptionPane.showMessageDialog(frame, s);
}

Somewhere else you can then:

try {
    doSomething();
} catch (SomeException e) {
    someOtherObject.showWarning(this, e);
}

If its in an event, you can use the SwingUtilities to get the window:

void actionPerformed(ActionEvent e) {

    JFrame frame = (JFrame)SwingUtilities.windowForComponent(
        (Component)e.getSource());
    showWarning(frame, "Don't press this button!");
}

Something like that.

Good luck,
Vincent


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.