Why is dialog not on top of parent window?
When I use:
JOptionPane.showMessageDialog(this,"Message");
the dialog box appears directly over the parent window. However when I extend JDialog, it doesn't. My code is:
publicclass OptionsDialogextends JDialog{
...
/**
* This method initializes
*
*/
public OptionsDialog(Frame arg0, Preferences jPrf1){
super(arg0,true);
jPrefer = jPrf1;
initialize();
}
/**
* This method initializes this
*
*/
privatevoid initialize(){
this.setSize(new Dimension(384, 171));
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
this.setTitle("Options");
this.setContentPane(getJPanel());
}
The dialog I call via
OptionsDialog dlg1 =new OptionsDialog(this, jPrefer);
dlg1.setVisible(true);
Why doesn't it automatically appear over the parent like JOptionPane?
Thanks,
Ilan
[1574 byte] By [
Ilana] at [2007-11-26 18:08:01]

Thanks Graeme. I tried setLocationRelativeTo(frame) and it changed the location of the dialog, but still not what I want.
When I start my main frame it is in the upper left hand corner of the screen. I can move it to another postion.
If I call JOptionPane, the option pane will be over the new frame position (x,y position). If I call my dialog, it will again be in the upper left hand corner and NOT over the repositioned frame.
SetLocationRelativeTo(frame) puts the dialog in the center of the screen, but not over the "this" which represents my frame.
BTW, my dialog has lots of focusable components. I want to know why it behave differently from JOptionPane. (See if you see anything strange in the code I posted.)
Thanks,
Ilan
Ilana at 2007-7-9 5:39:34 >

> When I start my main frame it is in the upper left
> hand corner of the screen. I can move it to another
> postion.
You can call frame.setLocationRelativeTo(null) which will center it in the screen. BTW, make sure that the size of your frame is set or you have called frame.pack() before calling setLocationRelativeTo otherwise the frame will not be centered.
> SetLocationRelativeTo(frame) puts the dialog in the
> center of the screen, but not over the "this" which
> represents my frame.
>
> BTW, my dialog has lots of focusable components. I
> want to know why it behave differently from
> JOptionPane. (See if you see anything strange in the
> code I posted.)
Without having access to your Frame code, I mad a simple test and it seems to work for me. This is what I used
// Frame Code
public class TestFrame extends JFrame {
private JPanel mainPanel;
private JButton testButton;
public TestFrame() {
this.setContentPane(getMainPanel());
this.setSize(450, 250);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private JPanel getMainPanel() {
if (mainPanel == null) {
mainPanel = new JPanel();
mainPanel.setLayout(new FlowLayout());
mainPanel.add(getTestButton());
}
return mainPanel;
}
private JButton getTestButton() {
if (testButton == null) {
testButton = new JButton();
testButton.setText("Show Dialog");
testButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
testButtonActionPerformed(evt);
}
});
}
return testButton;
}
protected void testButtonActionPerformed(ActionEvent evt) {
OptionsDialog dlg = new OptionsDialog(this, null);
dlg.setVisible(true);
}
public static void main(String[] args) {
TestFrame fr = new TestFrame();
fr.setLocationRelativeTo(null);
fr.setVisible(true);
}
}
/////////////////////////////////////////////////////////////
// Dialog:
public class OptionsDialog extends JDialog {
private JPanel jPanel;
private Preferences jPrefer;
public OptionsDialog(Frame arg0, Preferences jPrf1) {
super(arg0, true);
jPrefer = jPrf1;
initialize();
setLocationRelativeTo(getOwner());
}
private void initialize() {
this.setSize(new Dimension(384, 171));
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
this.setTitle("Options");
this.setContentPane(getJPanel());
}
private JPanel getJPanel() {
if (jPanel == null) {
jPanel = new JPanel();
}
return jPanel;
}
}