Results 1 to 8 of 8
  1. #1
    ScottVal is offline Member
    Join Date
    Jan 2009
    Posts
    35
    Rep Power
    0

    Default setLocation on a JDialog is ignored

    Hello-
    I'm trying to set the location of a JDialog.
    For some reason my setLocation() call seems to be ignored, and the JDialog just remains at 0,0 all the time.
    Does anyone know what I'm doing wrong?

    I've created a simple class below which illustrates the issue:

    import javax.swing.*;
    import java.awt.event.*;

    public class JDialogTest {

    public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setVisible(true);
    frame.setLocation(300,300);
    frame.setSize(300,300);

    JDialog dialog = new JDialog(frame, true /*modal*/);
    dialog.addWindowListener(new WindowAdapter() {

    public void windowClosing(WindowEvent e) {
    System.exit(0);
    }
    });
    dialog.add(new JLabel("Test"));
    dialog.pack();
    dialog.setVisible(true);
    dialog.setLocation(200,200);
    }
    }

    My setLocation(200,200) call is ignored, and the dialog remains at 0,0 (upper left corner of the screen).
    Interestingly, the frame.setLocation(300,300) is accepted, and you can see the frame is at 300,300.
    What do you think?
    -scott

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    First of all, please use code tags when you posting code segments here in the forum. Unformated codes are really hard to read.

    Before set the visible, you must set the location. Swing components cannot dynamically re-generated after visibility. Simple set the location before set visible to true.

  3. #3
    ScottVal is offline Member
    Join Date
    Jan 2009
    Posts
    35
    Rep Power
    0

    Default

    Yes, that was it. Thanks! -scott

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    Quote Originally Posted by Eranga View Post

    Before set the visible, you must set the location. Swing components cannot dynamically re-generated after visibility. Simple set the location before set visible to true.
    The solution is correct, but the reasoning is wrong. Try the same code with a non-modal dialog and you'll see that the location is set correctly.

    The correct reasoning, which applies to all GUI programming platforms, and not just Java/Swing, is that when a modal window is set to visible, the thread of code execution is halted till the modal window is dismissed.

    In this case, setLocation is actually executed after the dialog is closed. Which of course doesn't help.

    db
    Last edited by DarrylBurke; 01-12-2009 at 08:27 PM.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    What did you say is, code execution thread prevent the execution of GUI?

    What I'm trying to say is, setVisble() calls before set the location. So it's place in default location. Once the dialog is located in default locations not possible to move into another location. What I know is, once the window is located in a place the UI thread exist. Darryl correct me if I'm wrong.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    Illustrative SSCCE:
    Java Code:
    import java.awt.Frame;
    import javax.swing.JDialog;
    import javax.swing.SwingUtilities;
    
    public class LocateDialog {
    
       JDialog dialog;
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
    
             @Override
             public void run() {
                new LocateDialog().makeUI();
             }
          });
       }
    
       public void makeUI() {
          dialog = new JDialog((Frame) null, "One - non modal", false);
          dialog.setSize(200, 200);
          System.out.println("showing One");
          dialog.setVisible(true);
          System.out.println("One shown");
          dialog.setLocation(100, 100);
    
          dialog = new JDialog((Frame) null, "Two - non modal", false);
          dialog.setSize(200, 200);
          System.out.println("showing Two");
          dialog.setVisible(true);
          System.out.println("Two shown");
          dialog.setLocation(400, 100);
    
          dialog = new JDialog((Frame) null, "Three - modal", true);
          dialog.setSize(200, 200);
          System.out.println("showing Three");
          dialog.setVisible(true);
          System.out.println("Three shown (and closed!)");
          dialog.setLocation(100, 400);
       }
    }
    Code execution continues when a non modal dialog is setVisible(true), but is suspended when a modal dialog is shown. This is true for all visual platforms (VB, VFP, VC++ etc)

    db

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Returning data from a JFrame/JDialog?!
    By Joe2003 in forum AWT / Swing
    Replies: 6
    Last Post: 01-08-2009, 01:14 AM
  2. help with jdialog
    By leonard in forum AWT / Swing
    Replies: 1
    Last Post: 08-05-2007, 06:37 AM
  3. Close a JDialog Programmatically
    By Marcus in forum Advanced Java
    Replies: 1
    Last Post: 07-06-2007, 05:06 PM
  4. problems with jDialog in a JFrame
    By bbq in forum AWT / Swing
    Replies: 1
    Last Post: 07-05-2007, 05:14 AM
  5. Replies: 1
    Last Post: 04-09-2007, 01:05 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
  •