急~~~~!!取JTable中的JComboBox值问题!!!请大侠指点!
为了使JTable中的某列是JComboBox形式,但是每行的JComboBox的取值都是不同的。
我自定义了TableCellEditor,已经实现了。
但是??我遍历表中的所有Row,发现每行取出的JComboBox选值都是一样的!!!
请大家帮我看看代码:
package tb.test;
import javax.swing.AbstractCellEditor;
import javax.swing.table.TableCellEditor;
import javax.swing.JComboBox;
import javax.swing.JTable;
import java.awt.Component;
import java.util.Vector;
import javax.swing.JCheckBox;
import java.util.EventObject;
import sun.jdbc.rowset.*;
import java.sql.*;
public class MyCellEditor extends AbstractCellEditor implements TableCellEditor {
JCheckBox check;
Object obj = null;
JComboBox box;
JTable tb;
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int rowIndex, int vColIndex) {
if(value instanceof Vector)
{
obj = value;
Vector vv = (Vector) value;
box = new JComboBox(vv);
box.setFont(new java.awt.Font("宋体", 0, 12));
if (isSelected)
{
box.setForeground(table.getSelectionForeground());
box.setBackground(table.getSelectionBackground());
} else
{
box.setForeground(table.getForeground());
box.setBackground(table.getBackground());
}
box.setSelectedItem(value);
box.repaint();
return box;
}
return check;
}
//取单元值
public Object getCellEditorValue() {
//如果是单选框
if(obj instanceof JCheckBox)
{
if (check.isSelected())
return Boolean.TRUE;
else
return Boolean.FALSE;
}
if(obj instanceof Vector)
{
return box.getSelectedItem().toString();
}
return null;
}
public boolean isCellEditable(EventObject evt)
{
return true;
}
public boolean stopCellEditing() {
return true;
}
}