No components painted when calling setVisible from another frame

Hello,

I have a problem here so I could use a little help with this one.

I'm having a JFrame with a button on it. When the user clicks this button, a JDialog opens. When I call the setVisible(true) method from within the constructor of the JDialog, there is no problem. But due to some reasons (concerning modal dialog) I want to call the setVisible method from withing the actionPerformed method of the button which is located on the JFrame.

I created a little test scenario to show this behaviour:

publicclass Main

{

publicstaticvoid main(String[] args)

{

new TestGUI1();

}

}

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

publicclass TestGUI1implements ActionListener

{

JFrame frame =new JFrame("Test 1");

JPanel panel =new JPanel();

JTextArea textarea =new JTextArea();

JButton button =new JButton("Ok");

public TestGUI1()

{

frame.getContentPane().setLayout(new BorderLayout());

panel.setLayout(new FlowLayout(FlowLayout.RIGHT));

frame.getContentPane().add(panel, BorderLayout.SOUTH);

frame.getContentPane().add(textarea, BorderLayout.CENTER);

panel.add(button);

button.addActionListener(this);

frame.setSize(300,300);

frame.setVisible(true);

}

publicvoid actionPerformed(ActionEvent e)

{

if(e.getSource() == button)

{

TestGUI2 tg2 =new TestGUI2(frame);

tg2.setVisible(true);

}

}

}

import java.awt.event.*;

import java.awt.*;

import javax.swing.*;

publicclass TestGUI2extends JDialogimplements ActionListener

{

JDialog dialog =new JDialog();

JPanel panel =new JPanel();

JTextField textfield =new JTextField(20);

JButton button =new JButton("Close");

public TestGUI2(JFrame frame)

{

super(frame,"Test 2");

dialog.getContentPane().setLayout(new BorderLayout());

panel.setLayout(new FlowLayout(FlowLayout.RIGHT));

dialog.getContentPane().add(panel, BorderLayout.SOUTH);

dialog.getContentPane().add(textfield, BorderLayout.CENTER);

panel.add(button);

button.addActionListener(this);

dialog.pack();

}

publicvoid actionPerformed(ActionEvent e)

{

if(e.getSource() == button)

dialog.dispose();

}

}

So when you click the 'Ok' button on the JFrame, you will see only the title of the JDialog (= Test 2). When I resize it with the mouse, then only a gray area is shown. No components visible.

What could be the reason here?

Thx

[4732 byte] By [Myncaa] at [2007-10-3 8:33:54]
# 1

TestGUI2 extends JDialog, so it is a dialog, and you can call setVisible(true) on it

within TestGUI2 you create another JDialog dialog, and add all to this dialog,

which is not shown. get rid of 'dialog' and your constructor will look like this

public TestGUI2(JFrame frame)

{

super(frame, "Test 2");

getContentPane().setLayout(new BorderLayout());

panel.setLayout(new FlowLayout(FlowLayout.RIGHT));

getContentPane().add(panel, BorderLayout.SOUTH);

getContentPane().add(textfield, BorderLayout.CENTER);

panel.add(button);

button.addActionListener(this);

pack();

}

Michael_Dunna at 2007-7-15 3:41:19 > top of Java-index,Desktop,Core GUI APIs...
# 2
Indeed, it works (and the solution is logical).Sometimes you need a little push in the right direction.Thx
Myncaa at 2007-7-15 3:41:19 > top of Java-index,Desktop,Core GUI APIs...