Thread View: borland.public.jbuilder.java.swing
2 messages
2 total messages
Started by Alan Shiers
Tue, 02 Nov 2004 19:48
adding components to JPanel in JScrollPane
Author: Alan Shiers
Date: Tue, 02 Nov 2004 19:48
Date: Tue, 02 Nov 2004 19:48
48 lines
2627 bytes
2627 bytes
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
Author: John McGrath [TeamB]
Date: Sat, 06 Nov 2004 08:54
Date: Sat, 06 Nov 2004 08:54
61 lines
3537 bytes
3537 bytes
On 11/2/2004 at 2:48:31 PM, Alan Shiers wrote:
> 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.
> 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();
> ? 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:
http://www.JPMcGrath.net/newsgroups/e-mail.html
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads