在提出我的问题之前,我将尝试解释我需要和正在尝试做的事情。我已经设置了一个表格,其中有几列显示一个组合框,如图所示:
图片 http://www.freeimagehosting.net/newuploads/4ks9s.png
这应该为作业创建一个“订单”,这意味着,作业 1 将首先到站 1。如果我在站 2 列中添加站 4,那么它将到站 4,依此类推。它旨在创建订单以进行进一步处理。所以,我想:
- 创建表格并显示渲染的组合框;
- 如果前一列的值为“无”,则使列 (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());*/
}
};
但最重要的是,我的问题是,如何正确使用表格内的组合框,以便我可以设置值并在选择时获取项目值?