gpt4 book ai didi

java - 如何以最短的方式对 JTable 进行排序?

转载 作者:搜寻专家 更新时间:2023-10-30 21:44:29 26 4
gpt4 key购买 nike

我正在搜索 JTable 中的排序,我引用了很多文章,但无法找到对表格进行排序的最简单方法。我也知道 TableRowSorter 可能会有所帮助,但不知道如何做。作为 JTable 的新手,我在这里创建了一个新问题。

我的表结构是这样的

| People  | Place |   Organisation    | Event    | Mentions |
_____________________________________________________________
| Ramanuj | India | Tata Consultancy | Party'14 | 500000 |
| Prankster | USA | Microsoft Pvt Ltd | Party'14 | 900000 |

我在这里想要的是根据第 4 列(“提及”)对我的表进行降序排序。如果计数(提及)相同,则应按第一列(“人物”)升序排序

CSVReader reader = new CSVReader(new FileReader(file)); 

List<String[]> myEntries = reader.readAll();
String[][] rowData = myEntries.toArray(new String[0][]);

String[] columnNames = { "People", "Place", "Organisation", "Event", "Mentions" };

DefaultTableModel tableModel = new DefaultTableModel(rowData, columnNames);

最佳答案

根据 How to Use Tables: Sorting and Filtering

JTable table = new JTable(tableModel);
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(table.getModel());
table.setRowSorter(sorter);

List<RowSorter.SortKey> sortKeys = new ArrayList<>(25);
sortKeys.add(new RowSorter.SortKey(4, SortOrder.ASCENDING));
sortKeys.add(new RowSorter.SortKey(0, SortOrder.ASCENDING));
sorter.setSortKeys(sortKeys);

已更新

Are you sure it will sort?

...是的

Table

import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.RowSorter;
import javax.swing.SortOrder;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

public class Test {

public static void main(String[] args) {
new Test();
}

public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

DefaultTableModel model = new DefaultTableModel(new String[]{"People", "Place", "Organisation", "Event", "Mentions"}, 0);
model.addRow(new Object[]{"Prankster", "USA", "Microsoft Pvt Ltd", "Party'14", 900000});
model.addRow(new Object[]{"Ramanuj", "India", "Tata Consultancy", "Party'14", 500000});
model.addRow(new Object[]{"Banana", "India", "Tata Consultancy", "Party'14", 500000});

JTable table = new JTable(model);
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(table.getModel());
table.setRowSorter(sorter);

List<RowSorter.SortKey> sortKeys = new ArrayList<>(25);
sortKeys.add(new RowSorter.SortKey(4, SortOrder.ASCENDING));
sortKeys.add(new RowSorter.SortKey(0, SortOrder.ASCENDING));
sorter.setSortKeys(sortKeys);

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

}

已更新

您可以直接从 View 中提取数据...

 for (int row = 0; row < table.getRowCount(); row++) {
String people = table.getValueAt(row, 0).toString();
String place = table.getValueAt(row, 1).toString();
String organisation = table.getValueAt(row, 2).toString();
String event = table.getValueAt(row, 3).toString();
int mentions = (int)table.getValueAt(row, 4);
//...
}

这将为您提供“查看”(或排序)顺序的数据...

关于java - 如何以最短的方式对 JTable 进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28823670/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com