Java 如何从jtable内的combobox中获取值和选定值?
在提出我的问题之前,我会尝试解释我需要和正在尝试做的事情。我设置了一个表,其中有几列显示一个组合框,如图所示: 这将为作业创建一个“订单”。这意味着,作业1将首先进入工位1。如果我在Station 2列中添加Station 4,它将转到Station 4,依此类推。其目的是创建一个订单以便进一步处理。因此,我想:Java 如何从jtable内的combobox中获取值和选定值?,java,swing,combobox,jtable,jcombobox,Java,Swing,Combobox,Jtable,Jcombobox,在提出我的问题之前,我会尝试解释我需要和正在尝试做的事情。我设置了一个表,其中有几列显示一个组合框,如图所示: 这将为作业创建一个“订单”。这意味着,作业1将首先进入工位1。如果我在Station 2列中添加Station 4,它将转到Station 4,依此类推。其目的是创建一个订单以便进一步处理。因此,我想: 创建表格并显示呈现的组合框 如果前一列的值为“无”,则使列(3-6)中的单元格不可编辑(从而确保其顺序正确) 不显示已为该行选择的桩号 但对于初学者来说,我无法获得从组合框中选择
- 创建表格并显示呈现的组合框李>
- 如果前一列的值为“无”,则使列(3-6)中的单元格不可编辑(从而确保其顺序正确)李>
- 不显示已为该行选择的桩号李>
public class SimGui extends JFrame {
//implements ActionListener {
String Stations[] = new String[] {"Station 1","Station 2","Station 3","Station 4","Station 5","None"};
JComboBox stationscombo = new JComboBox(Stations);
Object obj = stationscombo.getSelectedItem();
表上的鼠标单击事件:
private void jTable2MouseClicked(java.awt.event.MouseEvent evt) {
//Object event = evt.getSource();
obj = stationscombo.getSelectedItem();
System.out.println("Item: " + obj);
//ListSelectionModel selectionModel = jTable2.getSelectionModel();
int tb1columns = jTable2.getColumnCount();
int selectionrow= jTable2.getSelectedRow();
int selectioncolumn = jTable2.getSelectedColumn();
if (selectioncolumn > 1) {
for (int i=2;i<tb1columns;i++) {
System.out.println(jTable2.getValueAt(selectionrow,selectioncolumn));
/*if (jTable2.getValueAt(selectionrow, i) != "None") {
stationscombo.removeItem(jTable2.getValueAt(selectionrow, i));
}*/
}
}
else { System.out.println(jTable2.getValueAt(selectionrow,selectioncolumn)); }
}
我已经为正在工作的表实现了ActionListener,但我不知道这是否会造成干扰
Action action = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(e.getSource());
//TableCellListener tcl = (TableCellListener)e.getSource();
//JComboBox cb = (JComboBox)e.getSource();
//String newSelection = (String)cb.getSelectedItem();
/*JComboBox cb = (JComboBox)e.getSource();
String teste = (String)cb.getSelectedItem();
System.out.println("Item: " + teste);*/
//TableCellListener tcl1 = new TableCellListener(jTable1, action);
/*if (tcl.getColumn() == 3) {
if (tcl.getNewValue() == true) {
int x = tcl.getColumn();
table1model.setColumnEditable(x, true);
}
else {
table1model.setColumnEditable(tcl.getColumn(), false);
}
/*boolean canEdit[] = {
false, true, true, true, true, true
};
//System.out.println(isCellEditable(tcl1.getRow(),tcl1.getColumn()));
}*/
/*System.out.println(newSelection);
System.out.println("Row : " + tcl.getRow());
System.out.println("Column: " + tcl.getColumn());
System.out.println("Old : " + tcl.getOldValue());
System.out.println("New : " + tcl.getNewValue());*/
}
};
但归根结底,我的问题是,如何正确地使用表中的组合框,以便在选中时设置值并获取项目值?请看
你可能还想做一些阅读,这将帮助你准确地理解发生了什么。看看
您可能还想做一些阅读,这将有助于您准确了解正在发生的事情。要更快获得更好的帮助,请发布一个。要更快获得更好的帮助,请发布一个。
SimGui is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener
Action action = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(e.getSource());
//TableCellListener tcl = (TableCellListener)e.getSource();
//JComboBox cb = (JComboBox)e.getSource();
//String newSelection = (String)cb.getSelectedItem();
/*JComboBox cb = (JComboBox)e.getSource();
String teste = (String)cb.getSelectedItem();
System.out.println("Item: " + teste);*/
//TableCellListener tcl1 = new TableCellListener(jTable1, action);
/*if (tcl.getColumn() == 3) {
if (tcl.getNewValue() == true) {
int x = tcl.getColumn();
table1model.setColumnEditable(x, true);
}
else {
table1model.setColumnEditable(tcl.getColumn(), false);
}
/*boolean canEdit[] = {
false, true, true, true, true, true
};
//System.out.println(isCellEditable(tcl1.getRow(),tcl1.getColumn()));
}*/
/*System.out.println(newSelection);
System.out.println("Row : " + tcl.getRow());
System.out.println("Column: " + tcl.getColumn());
System.out.println("Old : " + tcl.getOldValue());
System.out.println("New : " + tcl.getNewValue());*/
}
};