我有JTable
我已设置table.setAutoCreateRowSorter(true);
。我的主要目标是让一行始终位于顶部,而不管任何列排序顺序。我做的第一件事就是为我的cell数据类型修改compareTo
方法,这样它就能理解一个标志,意味着“少于其他任何东西”,因此总是将该项排序到顶部。不幸的是,这使得给定行始终位于顶部或底部,具体取决于列的排序顺序。因此,当排序顺序降序时,我需要设置单元格,使其有效值更大比其他任何东西都要高。唯一的问题是我无法弄清楚如何获得列排序顺序。
在this page上,一个建议的答案是
if (myTable.getRowSorter().getSortKeys().get(column).getSortOrder == SortOrder.ASCENDING) {
...
}
不幸的是,getSortKeys()始终会返回一个空列表。所以我回到了原点。我还考虑添加一个RowSorterListener,但这似乎是更改分拣机时的监听器,而不是更改排序顺序时的监听器。
有任何帮助吗?谢谢!
编辑:这是代码,演示了我的部分问题。请注意System.out.println,其结果始终为零。我想得到列的行排序顺序,但似乎没有。
package tablecolumn;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.RowSorter;
import javax.swing.SortOrder;
import javax.swing.table.AbstractTableModel;
public class Tablecolumn extends JFrame {
JScrollPane scroll;
JTable table;
MyTableModel model;
static String[] columnnames = new String[] {"A", "B", "C"};
public class MyTableModel extends AbstractTableModel {
public List<List<String>> entries;
MyTableModel() {
entries = new ArrayList<List<String>>();
for (int i=0; i<5; i++) {
List<String> cols = new ArrayList<String>();
cols.add("X" + i);
cols.add("Y" + i);
cols.add("Z" + i);
entries.add(cols);
}
}
@Override
public int getRowCount() {
return entries.size();
}
@Override
public int getColumnCount() {
return columnnames.length;
}
@Override
public Object getValueAt(int r, int c) {
List<? extends RowSorter.SortKey> rsk = table.getRowSorter().getSortKeys();
System.out.println(rsk.size());
return entries.get(r).get(c);
}
@Override
public Class getColumnClass(int c) {
return String.class;
}
@Override
public String getColumnName(int c) { return columnnames[c]; }
@Override
public boolean isCellEditable(int row, int col) { return false; }
}
public Tablecolumn() {
scroll = new JScrollPane();
model = new MyTableModel();
table = new JTable();
table.setModel(model);
table.setAutoCreateRowSorter(true);
table.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
table.getTableHeader().setReorderingAllowed(false);
table.setColumnSelectionAllowed(false);
table.setRowSelectionAllowed(true);
scroll.setViewportView(table);
scroll.setMinimumSize(new Dimension(200, 200));
scroll.setPreferredSize(new Dimension(800, 600));
GroupLayout layout2 = new GroupLayout(this.getContentPane());
this.setLayout(layout2);
layout2.setVerticalGroup(
layout2.createSequentialGroup()
.addComponent(scroll)
);
layout2.setHorizontalGroup(
layout2.createParallelGroup()
.addComponent(scroll)
);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
pack();
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Tablecolumn().setVisible(true);
}
});
}
}
答案 0 :(得分:2)
1)您的问题简单无法回答,因为您无视重复请求发布SSCCE
2)例如
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.RowSorter.SortKey;
class SelectedColumnTest {
private JTableHeader header;
private Object selectedColumn = null;
private String[] columnNames = {"String", "Integer", "Boolean"};
private Object[][] data = {{"aaa", 12, true}, {"bbb", 5, false}, {"CCC", 92, true}};
private TableModel model = new DefaultTableModel(data, columnNames) {
private static final long serialVersionUID = 1L;
@Override
public Class<?> getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
};
private JTable table = new JTable(model);
public JComponent makeUI() {
table.setRowSorter(new TableRowSorter<TableModel>(model) {
@Override
public void toggleSortOrder(int column) {
}
});
header = table.getTableHeader();
header.getActionMap().put("toggleSortOrder2", new AbstractAction() {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
e.setSource(table);
table.getActionMap().get("focusHeader").actionPerformed(e);
if (selectedColumn != null) {
int i = header.getColumnModel().getColumnIndex(selectedColumn);
SortOrder order = SortOrder.ASCENDING;
List<SortKey> keys = new ArrayList<SortKey>(table.getRowSorter().getSortKeys());
if (!keys.isEmpty()) {
SortKey sortKey = keys.get(0);
if (sortKey.getColumn() == i && sortKey.getSortOrder() == SortOrder.ASCENDING) {
order = SortOrder.DESCENDING;
}
}
table.getRowSorter().setSortKeys(Arrays.asList(new SortKey(i, order)));
}
header.repaint();
}
});
InputMap im = header.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_F8, 0), "toggleSortOrder2");
header.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
JTableHeader h = (JTableHeader) e.getSource();
int i = h.columnAtPoint(e.getPoint());
Object o = h.getColumnModel().getColumn(i).getHeaderValue();
if (i < 0 || o == selectedColumn) {
selectedColumn = null;
return;
}
selectedColumn = o;
h.requestFocusInWindow();
}
});
final TableCellRenderer hr = table.getTableHeader().getDefaultRenderer();
header.setDefaultRenderer(new TableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable t, Object v,
boolean isS, boolean hasF, int row, int col) {
return (v == selectedColumn) ? hr.getTableCellRendererComponent(
t, v, true, true, row, col) : hr.getTableCellRendererComponent(
t, v, false, false, row, col);
}
});
header.setToolTipText("Sorting by F8");
JPanel p = new JPanel(new BorderLayout());
p.add(new JScrollPane(table));
return p;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new SelectedColumnTest().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
3)在您使用SSCCE编辑问题的那一刻,确切地证明了您的问题,然后我会看看它
答案 1 :(得分:0)
我认为问题在于,如果您使用table.setAutoCreateRowSorter(true);
,则会出现使myTable.getRowSorter().getSortKeys()
始终返回空列表的错误。另一方面,如果您添加自己的行分类器,那么它可以正常工作。
我需要添加此代码:
RowSorter<MyTableModel> sorter =
new TableRowSorter<MyTableModel>(model);
table.setRowSorter(sorter);
现在,getSortKeys()
将返回列列表及其排序顺序。该列表的长度可变,具体取决于您点击了哪些列,因此您必须遍历列表,查看myTable.getRowSorter().getSortKeys().get(index).getColumn()
和myTable.getRowSorter().getSortKeys().get(index).getSortOrder()
,其中后者包含值{ {1}}或SortOrder.ASCENDING
。