wood burning stoves 2.0
The moose likes Swing / AWT / SWT and the fly likes adding components to JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "adding components to JPanel" Watch "adding components to JPanel" New topic

adding components to JPanel

Alan Shiers
Ranch Hand

Joined: Sep 24, 2003
Posts: 237
Hi there,

I want to know how to make visible components added to a JPanel that happens to be inside a JScrollPane after the JFrame has already been created. My class, that extends JFrame, has the jbInit() method you see below. At the very end of the jbInit() method I set up cardPanel to a GridLayout of 10 rows 1 column. This is so that I can display up to 10 distinct components as I add them dynamically using the displayResults() method. As you can imagine, the new HandStats (which extend JPanel) if they are being added to cardPanel, are not being displayed. I've tried using repaint() methods but they don't do anything. How do I accomplish this? Also, how do I remove the HandStats objects from cardPanel so that I can display another new set of them?


jefff willis
Ranch Hand

Joined: Sep 29, 2004
Posts: 113
Well, I've got a solution of sorts...

Maybe I've just been at work too long today, I'm not too comfortable with this solution. I got this to work by adjusting the viewPortView() of the scrollPane after I added the extra components.

Here is a complete listing. I hope it helps you out.

Just copy and past it.
jefff willis
Ranch Hand

Joined: Sep 29, 2004
Posts: 113
Well, I guess I should tell you how to execute it.

Run it, and it will start with a handful of components on a Panel. In the File Menu up top, there is an "Add" option that will add a handful of additional components to that panel.
[ November 02, 2004: Message edited by: jefff willis ]
I agree. Here's the link: http://aspose.com/file-tools
subject: adding components to JPanel
It's not a secret anymore!