i got a Problem.
I have a JTable, containing descriptors in the first column, and then different types of JComponents in the second column (JTextField as standard, JCheckBox for Boolean and JCombobox for Integers) I created my own Cellrenderer, to do so, but when i try to change the value of (to edit) f.e. the JCheckBox, the CheckBox dissapears, and there is a normal TextField.
So I tried to write my own CellEditor, but it doesnt really work. Might you guys help me please?
Thanks Developer_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);
}
}
// FormalDataCellEditor
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);
}
}
So this is my complete example.