我有一个包含3个JScrollPanes(各包含一个JTable)添加有BoxLayout的一个JPanel,所以我看到在第3个表和我动态加载数据,代码逻辑为3个表和仅几乎相同对于每个表我想有自动滚动到表的底部时,新的行添加到表中的列名和一些细胞呈现不同的是,前2代表工作完善和滚动进入表底,但最后这个表的滚动条做奇怪的事情!我对3张桌子使用完全相同的滚动方法,但前两个作品不起作用!JTable的自动滚动到下
任何想法?
我去掉了一些列添加代码为clearity但这是想法;
private JScrollPane fillThirdTable(ArrayList<DisplayVariable> displayList) {
DefaultTableModel model = new DefaultTableModel();
ToolTipTable answer = new ToolTipTable(model);
answer.setRowHeight(60);
answer.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
answer.setSize(1300, 400);
DefaultTableCellRenderer dtcr = new DefaultTableCellRenderer();
dtcr.setHorizontalAlignment(SwingConstants.CENTER);
answer.getColumn("Display Variable ID").setCellRenderer(dtcr);
JScrollPane scrollPane = null;
for (DisplayVariable var : displayList) {
model.addRow(new Object[] { id, shown, name, value });
answer.setFillsViewportHeight(true);
}
TableColumn c= answer.getColumnModel().getColumn(3);
c.setCellRenderer(new MultiLineCellRenderer());
TableColumn c2= answer.getColumnModel().getColumn(2);
c2.setCellRenderer(new MultiLineCellRenderer());
scrollPane = new JScrollPane(answer);
scrollPane.setSize(1300, 400);
//here I call the method
scrollToVisible(answer, (displayList.size()-1), 1);
return scrollPane;
}
这是自动滚动的方法;
public void scrollToVisible(JTable table, int rowIndex, int vColIndex) {
if (!(table.getParent() instanceof JViewport)) {
return;
}
JViewport viewport = (JViewport)table.getParent();
// This rectangle is relative to the table where the
// northwest corner of cell (0,0) is always (0,0).
Rectangle rect = table.getCellRect(rowIndex, vColIndex, true);
// The location of the viewport relative to the table
Point pt = viewport.getViewPosition();
// Translate the cell location so that it is relative
// to the view, assuming the northwest corner of the
// view is (0,0)
rect.setLocation(rect.x-pt.x, rect.y-pt.y);
// Scroll the area into view
viewport.scrollRectToVisible(rect);
}
猜测(通过去除效果的setSize),但使用空布局的另一个问题?如果是这样的话:dontdontdontdont永远不会null layoutManager。真正做到:从不 – kleopatra 2011-05-11 08:34:56