我有一个JTable
,其一列单元格是JComboBox
。
但是,当单击表 JComboBox
单元格时尝试获取行数时,我发现行索引总是返回错误值(始终是最后一次单击的行索引)。
public class TableComboBoxTest extends JFrame {
private JTable table;
private DefaultTableModel tableModel;
private Object[][] tableCells;
private final String[] TABLE_COLUMNS = {"No.1"};
private final String[] YES_NO_SELECTION = {"Yes", "No"};
public TableComboBoxTest() {
Container pane = getContentPane();
pane.setLayout(new BorderLayout());
tableModel = new DefaultTableModel(tableCells, TABLE_COLUMNS);
table = new JTable(tableModel);
DefaultCellEditor cellEditor = null;
JComboBox selA = new JComboBox(YES_NO_SELECTION);
cellEditor = new DefaultCellEditor(selA);
cellEditor.setClickCountToStart(1);
table.getColumn(TABLE_COLUMNS[0]).setCellEditor(cellEditor);
JScrollPane jsp = new JScrollPane();
jsp.getViewport().add(table, null);
pane.add(jsp, BorderLayout.CENTER);
TableCellEditor tce = null;
addRow("Yes");
outputDefaultSelection(0, 0);
addRow("No");
outputDefaultSelection(1, 0);
System.out.println("");
selA.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
JComboBox cb = (JComboBox) e.getSource();
String sel = (String) cb.getSelectedItem();
int rowIndex = table.getSelectedRow();
rowIndex = table.convertRowIndexToModel(rowIndex);
if (rowIndex == -1) {
return;
}
outputDefaultSelection(rowIndex, 0);
System.out.println("Select: " + sel + " at " + rowIndex);
}
}
});
}
private void addRow(String v1) {
Vector<String> vec = new Vector<String>();
vec.add(v1);
tableModel.addRow(vec);
tableModel.fireTableDataChanged();
}
private void outputDefaultSelection(int row, int col) {
TableCellEditor tce = table.getCellEditor(row, col);
System.out.println("Default " + row + "-" + col + " Selection: " + tce.getCellEditorValue());
System.out.println("Default " + row + "-" + col + " Value: " + table.getModel().getValueAt(row, col));
}
public static void main(String[] args) {
TableComboBoxTest stt = new TableComboBoxTest();
stt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
stt.setSize(200, 100);
stt.setVisible(true);
}
}
Default 0-0 Selection: Yes Default 0-0 Value: Yes Default 1-0 Selection: Yes Default 1-0 Value: No*
当点击第一行并选择"is"时,不会触发更改事件。 当点击第二行时,改变事件触发!并且行号错误:0
Default 0-0 Selection: No Default 0-0 Value: Yes Select: No at 0*
当继续点击第一行时,改变事件触发!并且行号错误:1
Default 1-0 Selection: Yes Default 1-0 Value: No Select: Yes at 1
如何获得正确的点击手机号码?
对于 itemStateChanged 过程,我还发现如果单元格设置值与默认列值相同("is"),则单击它时不会触发事件。但如果单元格设置值为“否”,单击它会引发更改事件。这意味着模型数据与默认选择的数据不同。如何使它们保持一致?
谢谢~
最佳答案
That means model data is different with default selected data. How to make them consistent?
这仅意味着模型尚未使用组合框中新选择的值进行更新。
这可以通过使用以下内容来证明:
final String sel = (String) cb.getSelectedItem();
final int rowIndex = table.convertRowIndexToModel(table.getSelectedRow());
if (rowIndex == -1) {
return;
}
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
outputDefaultSelection(rowIndex, 0);
System.out.println("Select: " + sel + " at " + rowIndex);
}
});
现在,显示代码将被添加到事件调度线程的末尾,这意味着它将在所有其他事件执行完成后执行,因此 TableModel 现在将被更新。
但是,这并不是最好的解决方案。如果您想知道单元格中的数据何时发生更改,请将 TableModelListener
添加到 TableModel。 不要使用 ItemListener。
关于java - JTable 始终无法获取内部 ComboBox 的正确 rowIndex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18975507/