Controlling where JDialog opens

Discussion in 'Java' started by Larry Coon, Oct 23, 2003.

  1. Larry Coon

    Larry Coon Guest

    I'm trying to create a JDialog that opens over the center
    of its parent JFrame, like the JOptionPane.showXXXDialog
    dialogs do. Here's a contrived example to illustrate
    the problem. Pushing the "Dialog" button opens a JDialog,
    but it always opens at the top-left corner of the screen,
    no matter where the parent JFrame is. Is there a way to
    change this?

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

    public class Test extends JFrame {
    private JButton dialogButton;

    public Test() {
    super("This is the JFrame");

    dialogButton = new JButton("Dialog");
    dialogButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    new ModalDialog(Test.this);
    }
    });
    getContentPane().add(dialogButton, BorderLayout.SOUTH);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(200, 200);
    setVisible(true);
    }

    class ModalDialog extends JDialog {
    private JButton closeButton;

    public ModalDialog(JFrame parent) {
    super(parent, "This is a JDialog", true);

    closeButton = new JButton("Close");
    closeButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    dispose();
    }
    });
    getContentPane().add(closeButton);

    setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    pack();
    setVisible(true);
    }
    }

    public static void main(String[] args) {
    new Test();
    }
    }
     
    Larry Coon, Oct 23, 2003
    #1
    1. Advertising

  2. Larry Coon

    Larry Coon Guest

    Larry Coon wrote:

    (snip)

    Found it by reading through the source of
    JOptionPane. I needed setLocationRelativeTo().
     
    Larry Coon, Oct 23, 2003
    #2
    1. Advertising

  3. In article <>, Larry Coon <>
    wrote:

    >:I'm trying to create a JDialog that opens over the center
    >:eek:f its parent JFrame, like the JOptionPane.showXXXDialog
    >:dialogs do. Here's a contrived example to illustrate
    >:the problem. Pushing the "Dialog" button opens a JDialog,
    >:but it always opens at the top-left corner of the screen,
    >:no matter where the parent JFrame is. Is there a way to
    >:change this?
    >:
    >:import java.awt.*;
    >:import java.awt.event.*;
    >:import javax.swing.*;
    >:
    >:public class Test extends JFrame {
    >: private JButton dialogButton;
    >:
    >: public Test() {
    >: super("This is the JFrame");
    >:
    >: dialogButton = new JButton("Dialog");
    >: dialogButton.addActionListener(new ActionListener() {
    >: public void actionPerformed(ActionEvent e) {
    >: new ModalDialog(Test.this);
    >: }
    >: });
    >: getContentPane().add(dialogButton, BorderLayout.SOUTH);
    >:
    >: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    >: setSize(200, 200);
    >: setVisible(true);
    >: }
    >:
    >: class ModalDialog extends JDialog {
    >: private JButton closeButton;
    >:
    >: public ModalDialog(JFrame parent) {
    >: super(parent, "This is a JDialog", true);
    >:
    >: closeButton = new JButton("Close");
    >: closeButton.addActionListener(new ActionListener() {
    >: public void actionPerformed(ActionEvent e) {
    >: dispose();
    >: }
    >: });
    >: getContentPane().add(closeButton);
    >:
    >: setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    >: pack();
    >: setVisible(true);
    >: }
    >: }
    >:
    >: public static void main(String[] args) {
    >: new Test();
    >: }
    >:}


    That's the purpose of the setLocationRelativeTo() method. The owner
    JFrame or JDialog specifies the owning window if modality is involved.
    But this method specifies on-screen position. I don't know if it's
    still true, but some of our older code has comments indicating it didn't
    work right unless called after the pack(). And in the newer Java
    releases, you can now pass a null argument to have it center on the main
    screen.

    = Steve =
    --
    Steve W. Jackson
    Montgomery, Alabama
     
    Steve W. Jackson, Oct 23, 2003
    #3
  4. Larry Coon

    Kleopatra Guest

    "Steve W. Jackson" wrote:

    > That's the purpose of the setLocationRelativeTo() method. The owner
    > JFrame or JDialog specifies the owning window if modality is involved.
    > But this method specifies on-screen position. I don't know if it's
    > still true, but some of our older code has comments indicating it didn't
    > work right unless called after the pack(). And in the newer Java
    > releases, you can now pass a null argument to have it center on the main
    > screen.
    >


    It's delightful how fruitful reading the news often is: I had the
    problem that a setLocationRelativeTo() seemingly did not work - and the
    (in the afterthought :) obvious reason was that I called it before the
    pack. Thanks for sharing the comment.

    Greetings
    Jeanette
     
    Kleopatra, Oct 24, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexandr Molochnikov

    Re: JDialog in taskbar under Linux

    Alexandr Molochnikov, Jul 31, 2003, in forum: Java
    Replies:
    0
    Views:
    1,869
    Alexandr Molochnikov
    Jul 31, 2003
  2. venky
    Replies:
    1
    Views:
    3,267
    Chandler
    Nov 13, 2003
  3. cccc
    Replies:
    0
    Views:
    1,839
  4. Inertia_sublimation
    Replies:
    5
    Views:
    2,116
    Inertia_sublimation
    Jan 3, 2004
  5. (Pete Cresswell)
    Replies:
    47
    Views:
    3,666
    Jeffrey Silverman
    Oct 25, 2004
Loading...

Share This Page