我有一个JTable
可按列标题排序的:
我现在只想显示表中的前 X 行。已经了解表格排序不在tableModel中。我认为将 JTable 数据复制到模型然后只检索第一个 X 元素是个好主意。但它似乎失败了。知道如何解决这个任务吗?
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
public class SortJTable {
public static void main(String[] args) {
final String[] columns = getTableColumns();
final Object[][] tableData = getTableValues();
TableModel model = new DefaultTableModel(tableData, columns);
final JTable table = new JTable(model);
table.setAutoCreateRowSorter(true); // Make it possible to column-sort
final JComboBox<String> box = new JComboBox<String>();
box.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TRY TO SORT THE MODEL AS THE TABLE
for(int i=0; i<10; i++)
table.convertColumnIndexToModel(i);
// IF MODEL IS SORTED AS TABLE: TAKE WANTED ELEMENTS
Object[][] newModel;
if(box.getSelectedIndex() == 0){
System.out.println("SHOW ALL");
}
else if(box.getSelectedIndex() == 1){
System.out.println("SHOW 5");
// CREATE NEW MODEL WITH ONLY 5 FIRST ELEMENTS FROM THE SORTED MODEL
newModel = new Object[5][10];
for(int col=0; col<tableData[0].length; col++){
for(int row=0; row<5; row++){
newModel[row][col] = tableData[row][col];
TableModel model = new DefaultTableModel(tableData, columns);
table.setModel(model);
}
}
}
else{
System.out.println("SHOW 10");
}
}
});
box.addItem("Show all rows");
box.addItem("Show 5 first rows");
box.addItem("Show 10 first rows");
JPanel content = new JPanel();
content.add(new JScrollPane(table));
content.add(box);
JFrame frame = new JFrame();
frame.add(content);
frame.pack();
frame.setVisible(true);
}
private static String[] getTableColumns(){
String[] columns = new String[10];
for(int i=0; i<10; i++)
columns[i] = "col"+i;
return columns;
}
private static Object[][] getTableValues(){
Object[][] tableData = new Object[10][10];
for(int i=0; i<tableData.length; i++){
for(int j=0; j<tableData[0].length; j++){
tableData[i][j] = i+""+j;
}
}
return tableData;
}
}