🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #890

adding components to JPanel in JScrollPane

#890
From: Alan Shiers
Date: Tue, 02 Nov 2004 19:48
48 lines
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();
}
Technical Details
Message-ID: <4189145f$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: 111d45,bd4e403bad384b6,start
X-Google-Attributes: gid111d45,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Alan Shiers" <ashiers@hfx.eastlink.ca>
Newsgroups: borland.public.jbuilder.java.swing
Subject: adding components to JPanel in JScrollPane
Date: Tue, 2 Nov 2004 15:48:31 -0400
Lines: 50
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1437
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441
NNTP-Posting-Host: 24.222.101.2
Message-ID: <4189145f$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1099502687 24.222.101.2 (3 Nov 2004 09:24:47 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail