如何从嵌套在 JTable 中的 JComboBox 中获取值
How to get value from the JComboBox which nested in the JTable
我在JTable 中放置了一个JComboBox,但现在我无法获取我从JComboBox 中选择的值。
table.getModel().getValueAt(row,column)
的方法不行,我尝试用另一种方法,如下所示,不幸的是,它也不行。
据我所知,如果 table 未对未重新排序的列进行排序或过滤,则 table.getModel().getValueAt(row,column)
和 table.getValueAt(row,column)
都应该有效。
您没有提供完整的代码,所以我们只能猜测是什么导致了问题。也许您实现了一个不完整的 CellEditor
,并且在选择 JComboBox
值之一后它没有设置模型的值。
以下示例正在运行,可能会对您有所帮助。请注意如何使用 DefaultCellEditor
和 JComboBox
在许多情况下就足够了,您不需要为此实现 CellEditor
:
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//
String[] tempColumnNames = new String[] { "#", "Name", "Family", "Age" };
Vector<String> columnNames = new Vector<String>(
Arrays.asList(tempColumnNames));
Vector<Vector<String>> data = new Vector<Vector<String>>();
for (int i = 0; i < 10; i++) {
Vector<String> rowData = new Vector<String>(Arrays.asList((i + 1)
+ "", "Name-" + (i + 1), "Family-" + (i + 1), i + 20 + ""));
data.add(rowData);
}
DefaultTableModel model = new DefaultTableModel(data, columnNames);
final JTable table = new JTable(model);
//
JComboBox<String> ageCombo = new JComboBox<String>(
new DefaultComboBoxModel<String>(new String[] { "20", "21",
"22", "23", "24", "25", "26", "27", "28", "29", "30" }));
table.getColumnModel().getColumn(3)
.setCellEditor(new DefaultCellEditor(ageCombo));
//
frame.getContentPane().add(new JScrollPane(table));
//
JButton showDialogButton = new JButton("Show Selected Age");
showDialogButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "selected age: "
+ table.getValueAt(table.getSelectedRow(), 3));
}
});
frame.getContentPane().add(showDialogButton, BorderLayout.NORTH);
//
frame.setBounds(500, 500, 350, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
希望对您有所帮助。
我在JTable 中放置了一个JComboBox,但现在我无法获取我从JComboBox 中选择的值。
table.getModel().getValueAt(row,column)
的方法不行,我尝试用另一种方法,如下所示,不幸的是,它也不行。
据我所知,如果 table 未对未重新排序的列进行排序或过滤,则 table.getModel().getValueAt(row,column)
和 table.getValueAt(row,column)
都应该有效。
您没有提供完整的代码,所以我们只能猜测是什么导致了问题。也许您实现了一个不完整的 CellEditor
,并且在选择 JComboBox
值之一后它没有设置模型的值。
以下示例正在运行,可能会对您有所帮助。请注意如何使用 DefaultCellEditor
和 JComboBox
在许多情况下就足够了,您不需要为此实现 CellEditor
:
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//
String[] tempColumnNames = new String[] { "#", "Name", "Family", "Age" };
Vector<String> columnNames = new Vector<String>(
Arrays.asList(tempColumnNames));
Vector<Vector<String>> data = new Vector<Vector<String>>();
for (int i = 0; i < 10; i++) {
Vector<String> rowData = new Vector<String>(Arrays.asList((i + 1)
+ "", "Name-" + (i + 1), "Family-" + (i + 1), i + 20 + ""));
data.add(rowData);
}
DefaultTableModel model = new DefaultTableModel(data, columnNames);
final JTable table = new JTable(model);
//
JComboBox<String> ageCombo = new JComboBox<String>(
new DefaultComboBoxModel<String>(new String[] { "20", "21",
"22", "23", "24", "25", "26", "27", "28", "29", "30" }));
table.getColumnModel().getColumn(3)
.setCellEditor(new DefaultCellEditor(ageCombo));
//
frame.getContentPane().add(new JScrollPane(table));
//
JButton showDialogButton = new JButton("Show Selected Age");
showDialogButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "selected age: "
+ table.getValueAt(table.getSelectedRow(), 3));
}
});
frame.getContentPane().add(showDialogButton, BorderLayout.NORTH);
//
frame.setBounds(500, 500, 350, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
希望对您有所帮助。