Results 1 to 8 of 8
  1. #1
    John3.16 is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default (new to forum) How to make JDialog on top of other always on top windows?

    Hi All,

    I have a JDialog which is a login screen for my application. There is a common scenario in which this dialog is opened when another application is already running on the screen where said dialog is supposed to appear. That application is always on top.
    I'm trying to make my dilaog appear on top of that application, but to no avail. I've tried setAlwaysOnTop(), getFocus() and toFront()

    Any suggestions?

    Thanks.

  2. #2
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    You should better use JFrame instead for it has
    alwaysontop
    statement. It is just in place thing )
    JDialog is not as independent as JFrame )

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    To Webuser: I disagree. JDialog also has a setAlwaysOnTop(true) method, and since this the original poster is using this window to function as a dialog, he should not use it as a JFrame.

    To the original poster, when I use the method above, it works for me. Code below:
    Java Code:
    import java.awt.Dimension;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.SwingConstants;
    
    public class AlwaysOnTop
    {
      private static final Dimension F_SIZE = new Dimension(300, 300);
      private static final Dimension D_SIZE = new Dimension(200, 200);
    
      private static void createAndShowUI()
      {
        JLabel frameLabel = new JLabel("JFrame", SwingConstants.CENTER);
        frameLabel.setPreferredSize(F_SIZE);
    
        JFrame frame = new JFrame("JFrame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(frameLabel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    
        JLabel dialogLabel = new JLabel("JDialog", SwingConstants.CENTER);
        dialogLabel.setPreferredSize(D_SIZE);
    
        JDialog dialog = new JDialog(frame, "JDialog", true);
        dialog.getContentPane().add(dialogLabel);
        dialog.pack();
        dialog.setLocationRelativeTo(null);
        dialog.setAlwaysOnTop(true);
        dialog.setVisible(true);
      }
    
      public static void main(String[] args)
      {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
          public void run()
          {
            createAndShowUI();
          }
        });
      }
    }
    If it doesn't work for you, then either you are doing something different from your code (please post a small compilable program and let us see), or your OS doesn't allow this method to work.

    Best of luck.

  4. #4
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    But author says that JDialog setAlwaysOnTop(true) method hasn't satisfied well enough (
    And still JDialog has no EXIT_ON_CLOSE const. Sure you may use System.exit(0) catching JDialog closing. And still there are many ways. But I suggest JFrame for it is more independent. What about lookAndFeel to control top JFrame pane view? )

  5. #5
    John3.16 is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default

    Thank you everyone for your replies. I'm using a JOptionPane to create my login screen, so I'm actually using both JFrame and JDialog when calling the method loginDialog = pane.createDialog(loginFrame, dialogTitle);
    Thing is, how is it detemined which application is on top when both are "always on top"? Is there a way to make the most recently called application to be on top of all the other "always on top apllication"?

    Thanks again.

  6. #6
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    You mean first one is the toppest one?

  7. #7
    John3.16 is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default

    Kinda. I mean that my application can't "beat" the other always on top application and be on top of it. I don't know if it has to do with which application has been activated first, or about properties. Either way, my application is covered by another always on top application eventhough I've used setAlwaysOnTop(true) and such.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    Take a look at java.awt.Window.toFront() inherited by JDialog. Note the caveats mentioned in the documentation.

    db

Similar Threads

  1. Replies: 1
    Last Post: 01-16-2009, 10:15 PM
  2. setLocation on a JDialog is ignored
    By ScottVal in forum AWT / Swing
    Replies: 7
    Last Post: 01-13-2009, 07:35 AM
  3. Replies: 1
    Last Post: 03-28-2008, 10:26 AM
  4. Welcome to our new forum: Forum Lobby
    By JavaForums in forum Forum Lobby
    Replies: 18
    Last Post: 02-07-2008, 05:40 AM
  5. help with jdialog
    By leonard in forum AWT / Swing
    Replies: 1
    Last Post: 08-05-2007, 05:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •