By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,557 Members | 1,184 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,557 IT Pros & Developers. It's quick & easy.

Swing problem

P: n/a
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.sleep(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
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Leo P. wrote:
[snip]
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?


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.
Jul 17 '05 #2

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

Raymond DeCampo <rd******@spam.twcny.spam.rr.spam.com.spam> wrote in message news:<rc******************@twister.nyroc.rr.com>.. .
Leo P. wrote:
[snip]
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?


Leo,

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

HTH,
Ray

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.