【发布时间】:2012-10-16 10:57:30
【问题描述】:
我正在尝试创建一个当时只显示一行的 JTable。当您按下 Next/Previous 时,表格将显示下一行或上一行,具体取决于按下的按钮。如果没有 Next/Previous,则 JTable 将显示第一个/最后一个元素。
一次只显示一行的代码:
tableModel = new DefaultTableModel(data, columnNames);
tableModel.setRowCount(0); //If I remove this, the things I add ends up att the 2nd line.
tableModel.addTableModelListener(resultTable);
resultTable = new JTable(tableModel);
scroll = new JScrollPane(resultTable, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
final int rows = 1;
Dimension d = resultTable.getPreferredSize();
resultTable.setPreferredSize(new Dimension(d.width,resultTable.getRowHeight()*rows));
下一个按钮的代码(不工作,怎么办?显示与以前相同的元素):
int height = resultTable.getRowHeight()*(rows-1);
JScrollBar bar = scroll.getVerticalScrollBar();
bar.setValue( bar.getValue()+height);
Previous-button 的代码(不工作,同上,什么都没有发生/改变。)
int height = resultTable.getRowHeight()*(rows-1);
JScrollBar bar = scroll.getVerticalScrollBar();
bar.setValue(bar.getValue()-height);
做什么?一些帮助将不胜感激。一直在努力解决这个问题。
编辑:尝试遵循此处的代码:JTable row limitation 但无法使其工作。虽然我只想显示 1 行,而不是 10。
【问题讨论】:
-
你确定要在这儿放一张桌子吗?
-
表与否并不重要。似乎是一种简单的方法。有什么更好的方法?
-
你改变了tableModel吗?这里的jtable只是代表模型数据。
-
不,没有更改 tableModel。