• Post Reply Bookmark Topic Watch Topic
  • New Topic

JScrollPane with fixed first row  RSS feed

 
Andrew Keidel
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'd like to make a table with the following layout:
Name Phone Address
andy 5551212 432 Johnson St
john 5551199 324 Hewett Rd
...
I want the first row (with column names) to be fixed in place while the user scrolls down the list of people.
Two requirements are (1) I need to be able to put images and jbuttons and jlabels inside the cells and (2) I need the header column to line up with the other columns regardless of whether there is a scroll bar. The second requirement means I can't simply make the header row a separate panel, because then the placement of its columns will be fixed, while column sizes for the cells below will change depending on whether a jscrollpane shows up. I only want the jscrollpane to show up if there are more data than can be displayed.
Thanks for any help,
Andy
Btw, the following link has helpful code, but I'm not sure if I want to use a JTable here or something else, like a JPanel/JScrollPane using TableLayout.
http://developer.java.sun.com/developer/qow/archive/51/
[ May 01, 2002: Message edited by: Andrew Keidel ]
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like you need to go through a few tutorials on what JTable does... ( here is one from Sun ) There are already built in ways to do most of what you want to do...

To put images, buttons, etc. inside a table cell you'll need to implement a custom cell renderer by extending DefaultTableCellRenderer or by implementing TableCellRenderer. If your table is going to be editable, you'll also need to make classes that extend DefaultCellEditor or TableCellEditor. These are then set for specified classes of JTable data by calling setDefaultRenderer( class, renderer ), or setDefaultEditor( class, editor ) on your JTable.

Column headers are automatically added to your JTable when you put your JTable in a JScrollPane... they stay fixed while your table data scrolls in the scroll pane. Unless you're forcing your JScrollPane to always show scroll bars, they shouldn't show up unless the table is larger than the viewable area of the JScrollPane.

-Nate
 
Andrew Keidel
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Nate!
Before seeing your reply, I ended up taking a non-JTable approach. (Btw, I have used JTables before, although I'm far from an expert.)
I managed to implement the specs successfully using the TableLayout. The JScrollPane.setColumnHeaderView method enabled me to solve the header problem.
My question now is this: What are the advantages of using a JTable versus using the TableLayout approach? Given that I want each cell to be its own panel, the table data is fixed and uneditable (though it is clickable), it seems to me that the TableLayout is better suited, because a JTable has a lot of table-management overhead that is not needed for a static table. Further, TableLayout is specifically tailored for GUI components, while JTable is more generalized.
Am I way off here? Please let me know. I appreciate your input. Also, please see the outline of my code, given below:
JPanel tableHeaderPane = new JPanel(false);
tableHeaderPane.setBackground(QUESTION_INFO_PANEL_COLOR);
tableHeaderPane.setLayout(new TableLayout(borderTableSize));
JPanel tableDataPane = new JPanel(false);
tableDataPane.setLayout(new TableLayout(dataTableSize));
JScrollPane myScrollPane = new JScrollPane(tableDataPane);
...
myScrollPane.setColumnHeaderView(tableHeaderPane);
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like TableLayout was the perfect solution to your problems... There is no reason you have to use a JTable if TableLayout provides the functionality you need! JTable has a lot of built in functionality, but if you don't need it, don't use it!

-Nate
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!