I have a piece of JTable code that has a custom editor and cell
renderer, so that it can display checkboxes, Strings and other objects
in the same column. The problem is I can't figure out how to get the
Integer renderer to work. I'd like it to behave so that the numbers
are right justified, and can take in only number characters. Here is
part of the code:
class MultiRenderer extends DefaultTableCellRenderer {
JCheckBox checkBox = new JCheckBox();
public Component getTableCellRendererComponent(
JTable table, Object value,
boolean isSelected, boolean hasFocus,
int row, int column) {
if (value instanceof Boolean) { // Boolean
checkBox.setSelected(((Boolean)value).booleanValue());
checkBox.setHorizontalAlignment(JLabel.CENTER);
return checkBox;
}
if (value instanceof Integer){ // Integer
Integer in = (Integer) value;
return super.getTableCellRendererComponent(
table,in,isSelected,hasFocus,row,column);
}
else{
String str = (value == null) ? "" : value.toString();
return super.getTableCellRendererComponent(
table,str,isSelected,hasFocus,row,column);
}
}
}
class ComboString {
String str;
ComboString(String str) {
this.str = str;
}
public String toString() {
return str;
}
}
class MultiEditor implements TableCellEditor {
private final static int COMBO = 0;
private final static int BOOLEAN = 1;
private final static int STRING = 2;
private final static int INTEGER = 3;
private final static int NUM_EDITOR = 4;
DefaultCellEditor[] cellEditors;
JComboBox comboBox;
int flg;
public MultiEditor() {
cellEditors = new DefaultCellEditor[NUM_EDITOR];
comboBox = new JComboBox();
comboBox.addItem("true");
comboBox.addItem("false");
cellEditors[COMBO] = new DefaultCellEditor(comboBox);
JCheckBox checkBox = new JCheckBox();
checkBox.setOpaque( true );
checkBox.setHorizontalAlignment(JLabel.CENTER);
cellEditors[BOOLEAN] = new DefaultCellEditor(checkBox);
JTextField textField = new JTextField();
cellEditors[STRING] = new DefaultCellEditor(textField);
cellEditors[INTEGER] = new DefaultCellEditor(textField);
flg = NUM_EDITOR; // nobody
}
public Component getTableCellEditorComponent(JTable table, Object
value,
boolean isSelected, int row, int column) {
if (value instanceof ComboString) { //
ComboString
flg = COMBO;
String str = (value == null) ? "" : value.toString();
return cellEditors[COMBO].getTableCellEditorComponent(
table, str, isSelected, row, column);
} else if (value instanceof Boolean) { //
Boolean
flg = BOOLEAN;
return cellEditors[BOOLEAN].getTableCellEditorComponent(
table, value, isSelected, row, column);
} else if (value instanceof String) { //
String
flg = STRING;
return cellEditors[STRING].getTableCellEditorComponent(
table, value, isSelected, row, column);
} else if (value instanceof Integer) { //
Integer
// Assuming that passing Integer object
// will get renderer to work, but doesn't
flg = INTEGER;
return cellEditors[INTEGER].getTableCellEditorComponent(
table, value, isSelected, row, column);
}
return null;
}
public Object getCellEditorValue() {
switch (flg) {
case COMBO:
String str = (String)comboBox.getSelectedItem();
return new ComboString(str);
case BOOLEAN:
case STRING:
return cellEditors[flg].getCellEditorValue();
case INTEGER:
return cellEditors[flg].getCellEditorValue();
default: return null;
}
}
public Component getComponent() {
return cellEditors[flg].getComponent();
}
public boolean stopCellEditing() {
return cellEditors[flg].stopCellEditing();
}
public void cancelCellEditing() {
cellEditors[flg].cancelCellEditing();
}
public boolean isCellEditable(EventObject anEvent) {
return true;//cellEditors[flg].isCellEditable(anEvent);
}
public boolean shouldSelectCell(EventObject anEvent) {
return cellEditors[flg].shouldSelectCell(anEvent);
}
public void addCellEditorListener(CellEditorListener l) {
cellEditors[flg].addCellEditorListener(l);
}
public void removeCellEditorListener(CellEditorListener l) {
cellEditors[flg].removeCellEditorListener(l);
}
public void setClickCountToStart(int n) {
cellEditors[flg].setClickCountToStart(n);
}
public int getClickCountToStart() {
return cellEditors[flg].getClickCountToStart();
}
}
If you want to see more code, just ask. |