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

