• Post Reply Bookmark Topic Watch Topic
  • New Topic

Displaying an additional row in a jtable  RSS feed

 
Pete Silk
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a jtable with an abstracttablemodel in a jscrollpane which is in a jpanel.

My problem is that I cannot get the jtable to display a larger number of rows when I add a row in any position. E.g. that if the table has 12 rows and i add an object to the table model at row 7, then the new object is inserted (in the correct place) and displayed fine but what was at the 12th row (and now the 13th) no longer is displayed.

The 13 objects are in the table model (when I delete an object, the 13th row becomes the 12th row and is displayed again).

(Deleting rows has no problems being reflected in the table.)

After adding the new row to the table model,
I have tried a revalidate() on the table and the scrollpane, and tried a repaint() and doLayout() on the scrollpane and nothing happens.

Guess what? It's starting to get really annoying, so if anyone knows any more efficient way of getting more rows to display than deleting and then reinstantiating the scrollpane and table then please help!


regards and thanx in advance
 
Barry Andrews
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe fireTableDataChanged() is what you need.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Pete!

This snippet does what you want.


Notice that if you don't need to scroll horizontally, there is no need for theJScrollPane.
 
Pete Silk
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) I wrote a table model listener:

import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class TechEvalTableModelListener implements TableModelListener
{
private List events = new ArrayList( );
private static Log log = LogFactory.getLog(TechEvalTableModelListener.class);

public void tableChanged(TableModelEvent e) {
log.debug("tableChanged() reached");
this.events.add(e);
}

public int getEventCount( ) {
log.debug("getEventCount() reached");
return this.events.size( );
}

public List getEvents( ) {
log.debug("getEvents() reached");
return this.events;
}
}



2) I added an instance of TechEvalTableModelListener to the tablemodel:
TechEvalTableModelListener tml = new TechEvalTableModelListener();
this.evaluationCriteriaTableModel.addTableModelListener(tml);

3) I added a method to the tablemodel, which calls fireTableRowsInserted()too :
public void addCriterion(int row, Object o){
log.debug("reached addCriterion()");
this.add(row, (WeightedEvaluationCriterion)o);
log.debug("about to call fireTableRowsInserted()");
fireTableRowsInserted( row, row);
}

4) and called this method to add the row
this.evaluationCriteriaTableModel.addCriterion(selectedRow + 1, newCriterion);

And the same problem occurs!

Any ideas as to what I'm doing wrongly regarding the fireTableRowsInserted() etc?

Thanks!
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

And the same problem occurs!


Dou you mean that after adding a new row to a JTable within a JScrollPane, there is no new room added to visualize a new row? That is the standar behaviour of JScrollPane. The view port size does not vary by itself. You need to modify it yourself. What is something that is not normally done because it defeats the purpose of JScrollPane: to show a fixed-size view of a larger component. Look at my post above to see how to tackle with it anyway.
 
Pete Silk
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to both of you for your help so far.

The current stage of the problem is that if e.g. I have a table displaying lets say 12 rows, I can add and delete rows to the table model fine, but the table will only display a maximum of 12 rows.

For example,
I delete 4 rows, then I can add a new row (at any point) in the table and it will be displayed correctly. I can keep adding rows successfully but the table will only display a max of 12!

The problem seems to not be to do with the scroll pane or viewport, as I can replace single-line rows with multi-line rows (i.e. the table is taller) and the rows will be visible, but only up until the table has 12 rows in it...

ANy thoughts?
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When fireTableRowsInserted is called JTable listens to the notification and makes the proper changes to the view representation. It seems that addCriterion is working properly according to you. If 12 rows is the initial room that the JScrollPane allowed, it is not going to increase due to adding more rows. Try the next example and make more room available for the table by dragging the south border of the frame. Then add more rows, you will see that the vertical scroll bar appears when needed. Rows are added but you cannot see them because of JScroolPane. Try uncommenting the sentence in the action listener for a possible work around. Though, again, JScrollPanes are not normally dinamically resized.

 
Pete Silk
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Test34 stuff is good for a JFrame, although pack() and show() are JFrame-specific. My JScrollPane does add a vertical scrollbar when needed if e.g. a row is deleted and a multi-line row is added; nothing I have found (in numerous forums) sorts out the extra row problem of a table with an abstracttablemodel in a scrollpane on a panel.

Anyway:
If I hadn't wasted so much time trying to sort this out I would not quote another forum here, but I found a couple of guys who had been stumped by much the same problem and in the end worked around it by reinstantiating table and scrollpane:
http://forum.java.sun.com/thread.jsp?forum=57&thread=279198
I mention this in the hope that others don't waste half their week in the same way.
Thanks again for your help.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!