JTable 排序器不工作
JTable sorter not working
我有一个非常简单的 JTable,我想按第 0 列升序排序。代码非常简单。但它无法正确排序行。我不知道出了什么问题。以下是我的代码:
package test;
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 TableSorter {
public static void main(String[] args) {
new TableSorter();
}
public TableSorter() {
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[] {"X", "Y", }, 0);
model.addRow(new Object[]{5, 8});
model.addRow(new Object[]{10, 5});
model.addRow(new Object[]{50, 60});
model.addRow(new Object[]{100, 60});
JTable table = new JTable(model);
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(table.getModel());
table.setRowSorter(sorter);
List<RowSorter.SortKey> sortKeys = new ArrayList<>();
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);
}
});
}
}
Here is the result when I run the program
结果:
X y
10 5
100 60
5 8
50 60
如有任何帮助,我们将不胜感激。结果附上。
提前致谢。
来自 DefaultTableModel 的文档:
Warning: DefaultTableModel returns a column class of Object. When
DefaultTableModel is used with a TableRowSorter this will result in
extensive use of toString, which for non-String data types is
expensive. If you use DefaultTableModel with a TableRowSorter you are
strongly encouraged to override getColumnClass to return the
appropriate type.
您需要重写 table 模型的 getColumnClass,例如:
DefaultTableModel model = new DefaultTableModel(new String[] {"X", "Y", }, 0)
{
@Override
public Class<?> getColumnClass(int column)
{
Class<?> returnValue;
if ((column >= 0) && (column < getColumnCount()))
{
returnValue = getValueAt(0, column).getClass();
}
else
{
returnValue = Object.class;
}
return returnValue;
};
};
在您的情况下,它比较 Integer.toString()
整数,因此您看到的是错误的顺序。
通过将 getColumnClass()
覆盖为 return Integer
类型,您将根据整数的值进行比较。
我有一个非常简单的 JTable,我想按第 0 列升序排序。代码非常简单。但它无法正确排序行。我不知道出了什么问题。以下是我的代码:
package test;
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 TableSorter {
public static void main(String[] args) {
new TableSorter();
}
public TableSorter() {
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[] {"X", "Y", }, 0);
model.addRow(new Object[]{5, 8});
model.addRow(new Object[]{10, 5});
model.addRow(new Object[]{50, 60});
model.addRow(new Object[]{100, 60});
JTable table = new JTable(model);
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(table.getModel());
table.setRowSorter(sorter);
List<RowSorter.SortKey> sortKeys = new ArrayList<>();
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);
}
});
}
}
Here is the result when I run the program
结果:
X y
10 5
100 60
5 8
50 60
如有任何帮助,我们将不胜感激。结果附上。 提前致谢。
来自 DefaultTableModel 的文档:
Warning: DefaultTableModel returns a column class of Object. When DefaultTableModel is used with a TableRowSorter this will result in extensive use of toString, which for non-String data types is expensive. If you use DefaultTableModel with a TableRowSorter you are strongly encouraged to override getColumnClass to return the appropriate type.
您需要重写 table 模型的 getColumnClass,例如:
DefaultTableModel model = new DefaultTableModel(new String[] {"X", "Y", }, 0)
{
@Override
public Class<?> getColumnClass(int column)
{
Class<?> returnValue;
if ((column >= 0) && (column < getColumnCount()))
{
returnValue = getValueAt(0, column).getClass();
}
else
{
returnValue = Object.class;
}
return returnValue;
};
};
在您的情况下,它比较 Integer.toString()
整数,因此您看到的是错误的顺序。
通过将 getColumnClass()
覆盖为 return Integer
类型,您将根据整数的值进行比较。