JOptionPane window forced in foreground

Discussion in 'Java' started by giangiammy, Jul 26, 2006.

  1. giangiammy

    giangiammy Guest

    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
     
    giangiammy, Jul 26, 2006
    #1
    1. Advertisements

  2. JOptionPane.showMessageDialog(frame, s);
    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
     
    Vincent van Beveren, Jul 26, 2006
    #2
    1. Advertisements

  3. giangiammy

    giangiammy Guest

    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:
     
    giangiammy, Jul 26, 2006
    #3
  4. I'm almost a newbie in java: the code I used is
    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
     
    Vincent van Beveren, Jul 26, 2006
    #4
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.