Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

JButton is not visible

167 views
Skip to first unread message

Matt Falkenhagen

unread,
Feb 12, 1999, 3:00:00 AM2/12/99
to
Hello,

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

Steve Bennetch

unread,
Feb 13, 1999, 3:00:00 AM2/13/99
to

Matt Falkenhagen wrote in message <36C515FB...@netscape.net>...

>Hello,
>
>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?
>
>

>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

slo...@club-internet.fr

unread,
Feb 13, 1999, 3:00:00 AM2/13/99
to
Steve Bennetch wrote:
>
> Matt Falkenhagen wrote in message <36C515FB...@netscape.net>...
> >Hello,
> >
> >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?


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!"

0 new messages