Java Reference
In-Depth Information
public void setValueAt(Object value, int row, int column) {
rowData[row][column]=value;
}
public boolean isCellEditable(int row, int column) {
return (column != 0);
}
}
Creating a Complex Cell Editor
Although the previous example demonstrates how to provide a fixed set of choices to the user
in a combo box TableCellEditor , offering the JColorChooser as an option seems to be a better
choice (at least, in the case of color selection). When defining your own TableCellEditor , you
must implement the single TableCellEditor method to get the appropriate component. You
must also implement the seven methods of the CellEditor because they manage and notify a
list of CellEditorListener objects, as well as control when a cell is editable. Starting with an
AbstractCellEditor subclass makes defining your own TableCellEditor much simpler.
By extending the AbstractCellEditor class, only the getCellEditorValue() method from
the CellEditor methods requires customization for the editor. Doing that and providing a
JButton that pops up a JColorChooser when clicked provides the entire editor component.
Listing 18-17 shows the code for this custom editor.
Listing 18-17. JColorChooser As Table Cell Editor
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class ColorChooserEditor extends AbstractCellEditor
implements TableCellEditor {
private JButton delegate = new JButton();
Color savedColor;
public ColorChooserEditor() {
ActionListener actionListener = new ActionListener() {
public void actionPerformed (ActionEvent actionEvent) {
Color color = JColorChooser.showDialog(
delegate, "Color Chooser", savedColor);
ColorChooserEditor.this.changeColor(color);
}
};
delegate.addActionListener(actionListener);
}
Search WWH ::




Custom Search