Swing problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Leo P.

    Swing problem

    I haven't used Swing too much, but I've recently run into a problem
    that I'm not sure how to fix. When I remove all components from a
    JPanel and then add new components to it, the panel displays the new
    components if they are AWT components, but not if they're Swing
    components. Example

    import java.awt.*;
    import javax.swing.*;
    public class test extends JFrame
    {
    public static void main(String[] args)
    {
    new test();
    }
    public test()
    {
    JPanel panel = new JPanel();
    panel.add(new Button("a"));
    panel.add(new Button("b"));
    getContentPane( ).add(panel);
    pack();
    show();
    try{Thread.slee p(3000);}catch( Exception ex){}
    panel.removeAll ();
    panel.add(new Button("c"));
    panel.add(new Button("d"));
    panel.repaint() ;
    }
    }

    This works fine. But if I change Button to JButton everywhere, then
    the panel goes blank after 3 seconds. Can anyone tell me why this
    happens?

    Thanks in advance
  • Raymond DeCampo

    #2
    Re: Swing problem

    Leo P. wrote:
    [snip][color=blue]
    > panel.repaint() ;
    > }
    > }
    >
    > This works fine. But if I change Button to JButton everywhere, then
    > the panel goes blank after 3 seconds. Can anyone tell me why this
    > happens?
    >[/color]

    Leo,

    Change the call to repaint() to revalidate(). Use revalidate() whenever
    a component's lay out needs to be redone.

    HTH,
    Ray

    --
    XML is the programmer's duct tape.

    Comment

    • Leo P.

      #3
      Re: Swing problem

      That works great. Thanks for answering my question even though I
      accidentally posted it into someone else's thread

      Raymond DeCampo <rdecampo@spam. twcny.spam.rr.s pam.com.spam> wrote in message news:<rcFIc.378 09$yd5.4652@twi ster.nyroc.rr.c om>...[color=blue]
      > Leo P. wrote:
      > [snip][color=green]
      > > panel.repaint() ;
      > > }
      > > }
      > >
      > > This works fine. But if I change Button to JButton everywhere, then
      > > the panel goes blank after 3 seconds. Can anyone tell me why this
      > > happens?
      > >[/color]
      >
      > Leo,
      >
      > Change the call to repaint() to revalidate(). Use revalidate() whenever
      > a component's lay out needs to be redone.
      >
      > HTH,
      > Ray[/color]

      Comment

      Working...