我有一个带有行分拣机的表 - >我附加了侦听器的setAutoCreateRowSorter(true);
和ListSelectionModel。如果我对它进行排序(通过单击标题)。
tableProducts=new JTable(table_modelProducts);
tableProducts.setAutoCreateRowSorter(true);
cellSelectionModelProducts = tableProducts.getSelectionModel();
cellSelectionModelProducts.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
问题是,如果对它进行排序然后单击一个单元格,它将返回位于该位置的原始值(表排序之前的那个):
if(arg0.getValueIsAdjusting()){
System.out.println(String.valueOf(tableProducts.getModel().getValueAt(tableProducts.getSelectedRow(), 1)));
}
答案 0 :(得分:1)
如果您阅读java代码,则getSelectedRow()在选择模型上,而getModel()返回数据模型。数据模型和选择模型是2种不同的模型,这就是为什么您无法获得正确的索引的原因。
正确的解决方案是调用JTable.getRowSorter()。convertRowIndexToModel()来获取数据模型索引。分拣机是将基础数据模型混合到视图模型中的分拣机。逻辑思考,它必须是维护2个模型之间映射的那个。所以你的代码应该是
System.out.println(String.valueOf(tableProducts.getModel()
.getValueAt(tableProducts.getRowSorter().convertRowIndexToModel(
tableProducts.getSelectedRow()), 1)));
以Java Tutorial中的JTable示例为基础,参见 System.out.println(" Right Value =")的行,如何调用上述方法,完整代码在这里。它是可执行的,您可以通过启动应用程序来测试它,单击姓氏列标题,然后单击每个姓氏,观察控制台输出。
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
/*
* SimpleTableDemo.java requires no other files.
*/
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class SimpleTableDemo extends JPanel {
private boolean DEBUG = false;
public SimpleTableDemo() {
super(new GridLayout(1,0));
String[] columnNames = {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian"};
Object[][] data = {
{"Kathy", "Smith",
"Snowboarding", new Integer(5), new Boolean(false)},
{"John", "Doe",
"Rowing", new Integer(3), new Boolean(true)},
{"Sue", "Black",
"Knitting", new Integer(2), new Boolean(false)},
{"Jane", "White",
"Speed reading", new Integer(20), new Boolean(true)},
{"Joe", "Brown",
"Pool", new Integer(10), new Boolean(false)}
};
final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setAutoCreateRowSorter(true);
table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if(e.getValueIsAdjusting()) {
System.out.println("Wrong Value = " + String.valueOf(table.getModel().getValueAt(table.getSelectedRow(), 1)));
System.out.println("Right Value = " + String.valueOf(table.getModel().getValueAt(table.getRowSorter().convertRowIndexToModel(table.getSelectedRow()), 1)));
}
}
});
table.setFillsViewportHeight(true);
if (DEBUG) {
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
printDebugData(table);
}
});
}
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//Add the scroll pane to this panel.
add(scrollPane);
}
private void printDebugData(JTable table) {
int numRows = table.getRowCount();
int numCols = table.getColumnCount();
javax.swing.table.TableModel model = table.getModel();
System.out.println("Value of data: ");
for (int i=0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j=0; j < numCols; j++) {
System.out.print(" " + model.getValueAt(i, j));
}
System.out.println();
}
System.out.println("--------------------------");
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("SimpleTableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
SimpleTableDemo newContentPane = new SimpleTableDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}