为了使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;
}
}
我自定义了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;
}
}
这就导致我在取该单元格的值getCellEditorValue()的时候box没有动态更新;box永远是最后操作的那个。
但是怎样才能根据行、列得到单元格对应的JComboBox?????
// 定义渲染类
TSCellEditor tsCellEditor = new TSCellEditor();
tbParam.getColumn("列b").setCellEditor(tsCellEditor);2.JTable数据绑定
我给“列b”绑定的值是一个Vector ;由于每行记录的中该列对应的Vector都不同;
所以JTable显示的时候,每行的JComboBox的值的集合都不同;3.JComboBox的取值
JTable1.getCellEditor(rowid,“列b”的对应列号).getCellEditorValue().toString()结果每行记录的下拉列表框取值都是一样的 :(大家帮我看看怎么解决这个问题啊!!!!!!!万分感谢!!! :D