java 如何从jtable内的组合框获取值和选定值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11623218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get values and selected values from combobox inside jtable?
提问by Luis Afonso Teixeira
Before making my question I will try and explaing what I need and am trying to do. I have set up a table where there are several columns displaying a combobox as the picture shows:
在提出我的问题之前,我会尝试解释我需要什么和正在尝试做什么。我已经建立了一个表格,其中有几列显示组合框,如图所示:
picture http://www.freeimagehosting.net/newuploads/4ks9s.png
图片 http://www.freeimagehosting.net/newuploads/4ks9s.png
This is supposed to create an "order" for the jobs this meaning, Job 1 will first go to Station 1. If I add Station 4 on the Station 2 Column it would then go to Station 4, and so on. It is intended to create an order for further processing. So, I want to:
这应该为作业创建一个“订单”,这意味着,作业 1 将首先转到站 1。如果我在站 2 列上添加站 4,它将然后转到站 4,依此类推。它旨在为进一步处理创建订单。所以,我想:
- Create the table and display the rendered combobox;
- Make the cells from columns (3-6) non-editable if the previous column has a value of "none" (thus making sure it keeps a correct order);
- Don't display a Station that has already been chosen for that row;
- 创建表格并显示呈现的组合框;
- 如果前一列的值为“none”,则使列 (3-6) 中的单元格不可编辑(从而确保它保持正确的顺序);
- 不要显示已经为该行选择的电台;
But for starters, I am not being able to get the values that are being set when choosing from the comboboxes nor am I able to get thosevales!
但是对于初学者来说,我无法获得从组合框中选择时设置的值,也无法获得那些值!
Heres is my code so far.
到目前为止,这是我的代码。
Creating the combobox:
创建组合框:
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();
Mouse Click event on the table:
桌子上的鼠标点击事件:
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)); }
}
The table construction:
表结构:
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);
}
});
I have looked around and tried to implement a listener for the combobox but failed. If I implement ActionListener on the class it will display a warning saying that:
我环顾四周并尝试为组合框实现侦听器但失败了。如果我在类上实现 ActionListener 它将显示一条警告说:
SimGui is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener
I have alreay an ActionListener implemented for the tables which is working, but I don't know if that might be interfering?
我已经为正在工作的表实现了一个 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());*/
}
};
But bottom line, my question is, how can I correctly work with the combobox inside the table so that I can get the values set and get the item value when selected?
但最重要的是,我的问题是,如何正确使用表格内的组合框,以便在选择时获取设置的值并获取项目值?
回答by MadProgrammer
Take a look at TableModel.setColumnValue(row, col)
看一下TableModel.setColumnValue(row, col)
You might also like to do some reading up on table cell editorsas well, this will help you understand exactly what's going on.
您可能还喜欢阅读表格单元格编辑器,这将帮助您准确了解正在发生的事情。