The moose likes Swing / AWT / SWT / JFace and the fly likes JTable in a JViewport Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT / JFace
Reply Bookmark "JTable in a JViewport" Watch "JTable in a JViewport" New topic
Author

JTable in a JViewport

Thomas Suer
Ranch Hand

Joined: Sep 03, 2001
Posts: 50
Hi everybody!
I have a problem when inserting a new row into the table's model. I want the table to insert new rows at the table's top and display the table's end. This works fine when starting the gui in VisualAge for Java, but when starting the gui from the console (Java 2 Runtime Environment, build 1.3.0_02, mixed mode) the table flickers when updating the content.
This is how I do the inserting and jumping mechanism (the whole code snippet is triggered by a timer event):
// get the actual data:
Vector tableData = getMyTableModel().getDataVector();
// new data:
MyData data = getNewData();
// insert a new row:
switch (mInsertPosition)
{
case INSERT_AT_START:
tableData.insertElementAt(data, 0);
getMyTableModel().fireTableRowsInserted(0, 0);
break;
case INSERT_AT_END:
default:
getMyTableModel().fireTableRowsInserted(tableData.size() - 1, tableData.size() - 1);
tableData.addElement(data);
}
// jump to the desired position:
switch (mJumpMode)
{
case JUMP_TO_START:
getScrollPane().getViewport().setViewPosition(new Point(0, 0));
break;
case JUMP_TO_END:
JViewport vp = getScrollPane().getViewport();
int vpos = 0;
// height of a single row
int rHeight = getTable().getRowHeight() + getTable().getIntercellSpacing().height;
// total height of the table
vpos = rHeight * getTable().getRowCount();
// subtract the visible height to get the upper left position
vpos = vpos - vp.getExtentSize().height;
if (vpos < 0) vpos = 0;
vp.setView(getTable());
// set the new position
vp.setViewPosition(new java.awt.Point(vp.getViewPosition().x, vpos));
break;
case DONT_JUMP:
default:
if (mInsertPosition == INSERT_AT_START)
{
Point viewPosition = getScrollPane().getViewport().getViewPosition();
// add one row height to the actual position!!!
int rowHeight = getTable().getRowHeight() + getTable().getIntercellSpacing().height;
viewPosition.y += rowHeight;
getScrollPane().getViewport().setViewPosition(viewPosition);
}
else // INSERT_AT_END
{
// do nothing...
}
}

What am I doing wrong? Any help would be appreciated.
Thanks in advance
Thomas
Thomas Suer
Ranch Hand

Joined: Sep 03, 2001
Posts: 50
Hi!
I have found the solution by my own. You just have to set the backing store enabled property of the scroll pane's viewport to true:
myScrollPane.getViewport().setBackingStoreEnabled(true);
Tom
 
IntelliJ Java IDE
 
subject: JTable in a JViewport
 
Threads others viewed
TableModel-unable to locate error
How to find visible row count in JTable?
Jtable Back ground
Need Help in Modification MultiRowHeaderExample
Need Help in Modification MultiRowHeaderExample
MyEclipse, The Clear Choice