Controlling where JDialog opens

L

Larry Coon

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();
}
}
 
L

Larry Coon

Larry Coon wrote:

(snip)

Found it by reading through the source of
JOptionPane. I needed setLocationRelativeTo().
 
S

Steve W. Jackson

Larry Coon <[email protected]> said:
: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 =
 
K

Kleopatra

Steve W. Jackson said:
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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top