Board index » jbuilder » adding components to JPanel in JScrollPane

adding components to JPanel in JScrollPane


2004-11-03 03:48:31 AM
jbuilder11
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?
Alan
*************************************************
ArrayList players = new ArrayList();
JPanel cardPanel = new JPanel();
private void jbInit() throws Exception
{
this.setSize(new Dimension(600, 300));
this.setTitle("Card Games");
contentPane = (JPanel) this.getContentPane();
...
...
cardPanel.setLayout(new GridLayout(10,1));
scrollpane.getViewport().add(cardPanel);
contentPane.add(scrollpane, BorderLayout.CENTER);
}
private void displayResults()
{
for(int i = 0; i < players.size(); i++)
{
HandStats stats = new HandStats("Player" + (i + 1) + "\nScore: " +
((Player)players.get(i)).getHandScore());
Card[] cards = ((Player)players.get(i)).getHand();
for(int j = 0; j < cards.length; j++)
stats.addComponent(cards[j]);
cardPanel.add(stats);
}
scrollpane.repaint(); //doesn't do anything
this.repaint(); //doesn't do anything either
//enableMenus();
}
 
 

Re:adding components to JPanel in JScrollPane

On 11/2/2004 at 2:48:31 PM, Alan Shiers wrote:
Quote
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.
If you want to display *up to* 10 rows, try using GridLayout(0,1) instead
of GridLayout(10,1). In GridLayout, either the rows or the cols argument
may be 0 (but not both). This means that it will grow in that dimension
as needed. When you specify (10,1), it will always make space for 10
rows, which will give you white space at the bottom if the actual count is
less than 10.
Quote
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.
I do not think the problem is that the components are not being repainted,
but rather that they are not being laid out. They are being painted, but
they have a size of [0,0], so you do not see anything.
There are two easy ways to recognize this problem. One that will work
most of the time is to resize your frame. Depending on the component
hierarchy, that will often force all components to be laid out, so they
will magically appear when you do this.
A more reliable method (but not quite as easy) is to take advantage of a
feature that it hidden in the Window class. When you press Ctrl-Shift-F1
in a Window (which includes Frame and Dialog), the Window will do a list()
on itself to System.out, displaying the entire UI hierarchy. You can see
the size and location of each component, as well as many other attributes.
The solution is to request that the container that you have modified lay
itself out again. The best way to do that is to use the revalidate()
method, which marks the component's layout as being invalid, and then
posts a request to the event queue to revalidate it. This has an
advantage over calling validate() directly, in that it has the effect of
coalescing multiple revalidate() calls.
So instead of the repaint() calls that you tried, use:
cardPanel.revalidate();
Quote
? Also, how do I remove the HandStats objects from cardPanel so
that I can display another new set of them?
cardPanel.remove( stats );
But you probably want to remove them all in one show, so you can use:
cardPanel.removeAll();
--
Regards,
John McGrath [TeamB]
---------------------------------------------------
Before sending me e-mail, please read:
www.JPMcGrath.net/newsgroups/e-mail.html