这是我的问题:我目前有一个包含任何地方的JTable 5,000到超过200,000行。你知道我要去哪里。 数据已经加载到内存中,这不是问题,但是如何 我可以创建一个高效的JTable,以便它只加载行 是可见的,任何事件只作用于那些行 在视口中可见?显然滚动几乎是不可能的 这么多的数据,因为它需要永远的系统重新绘制和解雇 事件
基本上我认为一种解决方案是确定哪些行 视口,然后创建一个包含这些行的新模型?
答案 0 :(得分:6)
您可以使用FixedRowsTable
类型设计,此处显示200,000行。虽然你想添加额外的按钮<< >>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
class FixedRowsTable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
String[] columns = {"1","2","3","4","5","6","7"};
Integer[][] data = new Integer[200000][columns.length];
for (int xx=0; xx<data.length; xx++) {
for (int yy=0; yy<data[0].length; yy++) {
data[xx][yy] = new Integer((xx+1)*(yy+1));
}
}
final int rows = 11;
JPanel gui = new JPanel(new BorderLayout(3,3));
final JTable table = new JTable(
new DefaultTableModel(data, columns));
final JScrollPane scrollPane = new JScrollPane(
table,
JScrollPane.VERTICAL_SCROLLBAR_NEVER,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
Dimension d = table.getPreferredSize();
scrollPane.setPreferredSize(
new Dimension(d.width,table.getRowHeight()*rows));
JPanel navigation = new JPanel(
new FlowLayout(FlowLayout.CENTER));
JButton next = new JButton(">");
next.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent ae) {
int height = table.getRowHeight()*(rows-1);
JScrollBar bar = scrollPane.getVerticalScrollBar();
bar.setValue( bar.getValue()+height );
}
} );
JButton previous = new JButton("<");
previous.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent ae) {
int height = table.getRowHeight()*(rows-1);
JScrollBar bar = scrollPane.getVerticalScrollBar();
bar.setValue( bar.getValue()-height );
}
} );
navigation.add(previous);
navigation.add(next);
gui.add(scrollPane, BorderLayout.CENTER);
gui.add(navigation, BorderLayout.SOUTH);
JOptionPane.showMessageDialog(null, gui);
}
});
}
}