gpt4 book ai didi

java - 如何从jtable内的组合框中获取值和选定的值?

转载 作者:行者123 更新时间:2023-12-02 00:17:37 25 4
gpt4 key购买 nike

在提出问题之前,我会尝试解释我需要什么和正在尝试做什么。我设置了一个表格,其中有几列显示组合框,如图所示:

picture http://www.freeimagehosting.net/newuploads/4ks9s.png

这应该为作业创建一个“顺序”,这意味着,作业 1 将首先转到工作站 1。如果我在工作站 2 列上添加工作站 4,那么它将转到工作站 4,依此类推。其目的是创建进一步处理的订单。所以,我想:

  • 创建表格并显示渲染的组合框;
  • 如果前一列的值为“none”,则使第 (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)); }
}

表结构:

jTable2.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null},
{null, null, null, null, null, null, null}
},
new String [] {
"Job Type", "Parts", "Station 1", "Station 2", "Station 3", "Station 4", "Station 5"
}
) {
Class[] types = new Class [] {
java.lang.Integer.class, java.lang.Integer.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class
};
boolean[] canEdit = new boolean [] {
false, true, true, true, true, true, true
};

public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}

public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
jTable2.getTableHeader().setReorderingAllowed(false);
for (int x = 2;x<7;x++) {
jTable2.getColumnModel().getColumn(x).setCellEditor(new DefaultCellEditor(stationscombo));
}
jTable2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jTable2MouseClicked(evt);
}
});

我环顾四周并尝试为组合框实现监听器,但失败了。如果我在类上实现 ActionListener,它将显示一条警告:

SimGui is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener

我已经为正在工作的表实现了一个 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());*/
}
};

但最重要的是,我的问题是,如何正确使用表内的组合框,以便我可以获得设置的值并在选择时获取项目值?

最佳答案

看看TableModel.setColumnValue(row, col)

您可能还想阅读 table cell editors同样,这将帮助您准确了解正在发生的事情。

关于java - 如何从jtable内的组合框中获取值和选定的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11623218/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com