【发布时间】:2018-08-10 09:29:47
【问题描述】:
我试图在单击组合框时获取当前行数据。 我的问题是,如果我尝试在单击组合框时获取详细信息,则检索到的数据是错误的。
这是在集合中填充无效数据。请按照下面提到的确切步骤进行复制。
请运行代码以复制问题,因为它仅在初始选择期间有效,但在之后无效。
注意:请仅在第二列上直接点击
Step 1: Click on Second Column of Row 1
Step 2: Select- Item 1
Step 3: Click on Second Column of Row 2
Step 4: Select- Item 2
Step 5: Click on Second Column of Row 3
Step 6: Select- Item 3
WORKS Fine till here :)
Step 7 : Click on Second column of Row 1 and do not change an selection leave it as it is (Just click on the combobox twice)
Step 8 : Click on Second column of Row 2, DO NO CHANGES
Step 9 : Click on Second column of Row 3, DO NO CHANGES
Step 10: NOW randomly click on any of the second columns of rows(1,2,3) and see the output datamap. It really wierd why the event is
下面是示例代码:
import java.awt.Cursor;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Vector;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
public class TestJCombo extends JFrame {
public TestJCombo() {
initialize();
}
JTable jTable;
JComboBox comboBox;
Map<Integer, String> dataMap;
private void initialize() {
setSize(300, 300);
setLayout(new FlowLayout(FlowLayout.LEFT));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JTextField field = new JTextField();
field.setSize(50000, 25);
field.setText(" ");
jTable = new JTable();
comboBox = new JComboBox();
comboBox.setEditable(true);
comboBox.addItem("item 1");
comboBox.addItem("item 2");
comboBox.addItem("item 3");
comboBox.setEditable(false);
dataMap = new LinkedHashMap();
comboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
Object selected = comboBox.getSelectedItem();
int selectedRow = jTable.getSelectedRow();
selectedRow = jTable.convertRowIndexToModel(selectedRow);
if (selectedRow != -1) {
String user = (String) jTable.getValueAt(selectedRow, 0);
String data = "Row: " + (selectedRow + 1) + " :::: " + user + " , "
+ comboBox.getSelectedItem();
dataMap.put(selectedRow + 1, "[" + user + " - " + comboBox.getSelectedItem() + "]");
if (selected != null) {
field.setText(data);
}
System.out.println("Current data map:: " + dataMap);
}
}
}
});
jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jTable.setRowHeight(30);
jTable.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
DefaultTableModel myTableMdl = new DefaultTableModel();
myTableMdl.addColumn("User");
myTableMdl.addColumn("Role");
jTable.setModel(myTableMdl);
jTable.getColumn("Role").setCellEditor(new DefaultCellEditor(comboBox));
Vector tableData;
for (int i = 1; i <= 7; i++) {
tableData = new Vector();
tableData.add("User " + i);
myTableMdl.addRow(tableData);
}
getContentPane().add(jTable);
getContentPane().add(field);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TestJCombo().setVisible(true);
}
});
}
}
【问题讨论】:
-
JTable.getValueAt方法采用视图索引,而不是模型索引。在您使用此方法之前,您正在将视图索引转换为模型索引,并在该调用中使用模型索引。
标签: java swing jtable jcombobox