This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JTable's cells getting bunched up when I scroll  RSS feed

 
Chris Garrison
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What can I do to prevent the cell rows sizing strangely as I scroll up and down.
They get all out of whack.
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is not a normal behaviour. could you paste the code where your building up the table.
it seems like your table is loading a lot while scrolling.
chantal
 
Chris Garrison
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You bet!! Here's the code to set up table initially.
///////////////////////
/// Sub panel 1 for lower center portion
/// i.e lab results
///
JPanel tpanel1 = new JPanel();
tpanel1.setLayout(new GridLayout(1,1));// IMPORTANT SO TABLE FILLS UP ENTIRE PANEL!!!
tpanel1.setForeground(nb);
tpanel1.setBackground(tan);

headings = new String [] {"Profile", "Specimen", "Result", "Norm Range High", "Norm Range Low", "UoM", "Entry Date", "Entry Time"};
datat = new String [150][8]; // datat is array that holds results from JDBC/SQL query
table1 = new JTable(datat, headings);
tpanel1.add(table1);
tpanel1.add(getContentPane().add(new JScrollPane(table1)));

Here's the code where I populate the table with results from query.
// load data array into retdata
for (int z = 0; z < count; z++){
for(int y = 0; y <=7; y++){

retdata[z][y] = data[z][y];
ArchGui.table1.setValueAt(data[z][y], z, y);
//System.out.println("retdata [" + z + "][" + y + "] = " + retdata[z][y]);
}
}
I think the problem must be in the initial phase. If I scroll up and down on the blank table (i.e. before I've retrieved the data) the cells still get screwed up. It may be related to the array. What I'd ultimatly like to do is for the table to be only as long as the results ( if I get 30 results from the query, I want the table 30 rows long)but so far I haven't been able to figure it out. I just hard code it to 150.
I removed the 2nd line (where I set the panel1 to have a Grid layout) and it corrected the problem, but I need the table to expand to fit the screen. Any ideas?
Thanks so much for your assistance.
[ July 12, 2002: Message edited by: Chris Garrison ]
[ July 12, 2002: Message edited by: Chris Garrison ]
 
Chris Garrison
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured it out - or at least I made changes so it would happen.
The problem was related to the Grid layout. It would automatically expand the table (inside the scroll pane) to fit the panel.
I added the following:
Dimension d = new Dimension(800, 250);
table1.setPreferredScrollableViewportSize(d);
This expands the table to the correct size. Though it does not re-size as I would like it to (if the Grid layout would work) I'm OK with the way it's working.
Is it supposed to do that? Could it by my OS environment??? who knows....
Thanks again for your response. I wanted to follow up and post the solution.
[ July 12, 2002: Message edited by: Chris Garrison ]
 
Chris Garrison
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Crud!
It's doing it again.
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you don't have to call setValue by yourself. use a TableModel and it will handle all this for you.
if you don't need special behaviour in your table you could just use DefaultTableModel.
If you want to implement special behaviour like which cells are editable and which are not, or how to add a new row etc.:
Either you write a class that implements the TableModel interface, or you extend AbstractTableModel or DefaultTalbleModel. AbstractTableModel implements the listener methods so I recommend to use at least that one.
see the swing tutorial for detailed information.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!