【发布时间】:2015-01-13 15:14:36
【问题描述】:
我遇到了问题。
我有一个 JTable,第一列包含描述符,然后是不同类型的 第二列中的 JComponents(JTextField 为标准,JCheckBox 为布尔值,JCombobox 为整数) 为此,我创建了自己的 Cellrenderer,但是当我尝试更改(编辑) f.e. 的值时。 JCheckBox,CheckBox消失,有一个正常的TextField。
所以我尝试编写自己的 CellEditor,但它并没有真正起作用。 请大家帮帮我好吗?
谢谢 开发者_X
String[] columnNames =
{"", ""};
Object[][] data =
{
{ "A", ""},
{ "B", ""},
{ "C", new Integer(0) },
{ "D", new Boolean(true) },
{ "E", "A.B.C" },
{ "F", "Rhababer" }
};
JTable table = new JTable(data, columnNames)
{
private static final long serialVersionUID = 1L;
@Override
public boolean isCellEditable(int row, int column)
{
if(column==0)
return false;
else
return true;
}
};
table.setRowSelectionAllowed(false);
table.setColumnSelectionAllowed(false);
table.setCellSelectionEnabled(false);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
table.setDefaultRenderer( Object.class, new FormalDataCellRenderer(table.getDefaultRenderer(Object.class)));
table.setDefaultRenderer( Boolean.class, new FormalDataCellRenderer(table.getDefaultRenderer(Boolean.class)));
table.setDefaultRenderer( Integer.class, new FormalDataCellRenderer(table.getDefaultRenderer(Integer.class)));
table.setDefaultEditor(Object.class, new FormalDataCellEditor(table.getCellEditor()));
//FormalDataCellRenderer
public class FormalDataCellRenderer implements TableCellRenderer
{
private TableCellRenderer normal;
public FormalDataCellRenderer(TableCellRenderer cellRenderer)
{
super();
this.normal = cellRenderer;
}
public Component getTableCellRendererComponent(JTable table,Object value, boolean isSelected, boolean hasFocus, int row,int column)
{
if(column==1)
{
if(value instanceof Boolean)
{
return new JCheckBox("",value.equals(new Boolean(true)));
}
else if(value instanceof Integer)
{
JComboBox<String> typeSelection = new JComboBox<String>();
typeSelection.addItem(sm.getString(StringConstants.PROFILE_GENDER_MALE));
typeSelection.addItem(sm.getString(StringConstants.PROFILE_GENDER_FEMALE));
typeSelection.setSelectedIndex((Integer)(value));
return typeSelection;
}
}
return normal.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
}
}
// 形式数据单元编辑器
public class FormalDataCellEditor extends DefaultCellEditor
{
public FormalDataCellEditor(TableCellEditor editor)
{
super(new JCheckBox()); //? What shall I do?
}
@Override
public Component getTableCellEditorComponent(JTable table,Object value, boolean isSelected, int row, int column)
{
if(column==1)
{
System.out.println(value.toString());
if(value instanceof Boolean)
{
if(value.equals(new Boolean(true)))
{
value = new Boolean(false);
}
else
{
value = new Boolean(true);
}
return new JCheckBox("",value.equals(new Boolean(true)));
}
else if(value instanceof Integer)
{
JComboBox<String> typeSelection = new JComboBox<String>();
typeSelection.addItem(sm.getString(StringConstants.PROFILE_GENDER_MALE));
typeSelection.addItem(sm.getString(StringConstants.PROFILE_GENDER_FEMALE));
int selected = (Integer)(value);
if(selected<typeSelection.getItemCount()-1)
selected++;
else
selected = 0;
typeSelection.setSelectedIndex(selected);
value = new Integer(selected);
return typeSelection;
}
}
return super.getTableCellEditorComponent(table, value, isSelected,row, column);
}
}
【问题讨论】:
-
请编辑您的问题,在complete example 中包含您描述的问题。
标签: swing jtable jcombobox tablecellrenderer tablecelleditor