gpt4 book ai didi

java - 使用 setAutoCreateRowSorter(true) 时获取列排序顺序?

转载 作者:行者123 更新时间:2023-11-30 04:48:07 29 4
gpt4 key购买 nike

我有一个 JTable,其中设置了 table.setAutoCreateRowSorter(true);。我的主要目标是无论任何列排序顺序如何,都有一行始终位于顶部。我做的第一件事是修改单元格数据类型的 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);
}
});
}
}

最佳答案

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 编辑问题时,准确地演示了您的问题,然后我会查看它

4) 没有比 Darryl's Multisort Table Header Cell Renderer 更好的了。

关于java - 使用 setAutoCreateRowSorter(true) 时获取列排序顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10467445/

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