Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Dynamically changing contents of a scrollpane, doesn't display?  RSS feed

 
Joe Vahabzadeh
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All,

I've run into something of a problem.

I've got a GUI using the BorderLayout, with the top part showing several buttons, the bottom also showing several buttons, and the center section with a JScrollPane which starts out empty but will be updated depending on other things that are going on.

Here's the relevant code:



However, all I get in the middle section is a blank area.

If I delete the last two lines of code, and change the 5th line to read as follows:


then everything works. However, this isn't suitable for my needs.

What am I doing wrong? Why can't I add a table to the JScrollPane after the display has already been set up and shown?

Thanks in advance.
 
Johan Sun
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
don't use
scrollPane.add(table);

use
scrollPane.setViewportView(table)
instead, which is same as
scrollPane.getViewport().add(table)

u can add things to JScrollPane, but it's its viewport that's displayed.

//-----------
import javax.swing.*;
public class AdminTab //extends JPanel //implements ActionListener
{

public static void main(String[] args) {
JScrollPane scrollPane = new JScrollPane();
JFrame frame = new JFrame();
frame.add(scrollPane);

*OK scrollPane.setViewportView(new JLabel("haha"));
*NO scrollPane.add(new JLabel("haha"));

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
//-----------------------
 
Joe Vahabzadeh
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!