For some reason, the button in the following program does not show up
until the window is resized. I thought calling setVisible() would make
it appear, but it does not work. Any ideas?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestButton {
static JFrame jframe = new JFrame("Testing...");
public static void main(String[] args) {
setupFrame();
JButton jb = new JButton("can you see this?");
jframe.getContentPane().add( jb );
jb.setVisible(true);
}
public static void setupFrame() {
jframe.setSize(400,100);
jframe.setVisible(true);
jframe.getContentPane().setLayout( new FlowLayout() );
}
}
Thanks,
Matt Falkenhagen
mfalke...@netscape.net
>public class TestButton {
> static JFrame jframe = new JFrame("Testing...");
>
> public static void main(String[] args) {
> setupFrame();
> JButton jb = new JButton("can you see this?");
> jframe.getContentPane().add( jb );
> jb.setVisible(true);
> }
>
Add the following line at the end of
your main method:
jframe.getContentPane().doLayout();
This works, but normally you wouldn't
have to call this method explicitly, as it
should be called for you internally.
Steve
You should pack() and show() your JFrame at the end of your main.
If you add components, use validate() then repaint(), to indicate to
your frame that its graphic state has changed. When you resize the
frame, a paint() method is invoked by the system.
Sun has a very good article on the paint mechanism in the swing
connection.
--
//
// St LOUBRY
// slo...@club-internet.fr
// "live for each moment!"