Custom Cell Renderers and Custom Cell Editors in Swing - Part II
RadioColumnEditor:
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EventObject;
import javax.swing.JRadioButton;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.EventListenerList;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableModel;
public class RadioColumnCellEditor implements
ActionListener, TableCellEditor {
Color currentColor;
JRadioButton button;
boolean state;
public RadioColumnCellEditor() {
super();
button = new JRadioButton();
button.setOpaque(false);
button.setBackground(Color.WHITE);
button.addActionListener(this);
}
protected EventListenerList listenerList =
new EventListenerList();
protected Object value;
protected ChangeEvent changeEvent = null;
protected int clickCountToStart = 1;
public void setCellEditorValue(Object value) {
this.value = value;
}
/**
* Add a listener to the list that's notified when
* the editor starts, stops, or cancels editing.
*/
public void addCellEditorListener(
CellEditorListener l) {
listenerList.add(CellEditorListener.class, l);
}
/**
* Remove a listener from the list that's notified
*/
public void removeCellEditorListener(
CellEditorListener l) {
listenerList.remove(CellEditorListener.class,
l);
}
protected void fireEditingStopped() {
Object[] listeners =
listenerList.getListenerList();
for (int i = listeners.length - 2; i >= 0; i -=
2) {
if (listeners[i] == CellEditorListener.class) {
if (changeEvent == null)
changeEvent =
new ChangeEvent(this);
((CellEditorListener) listeners[i + 1])
.editingStopped(changeEvent);
}
}
}
protected void fireEditingCanceled() {
Object[] listeners =
listenerList.getListenerList();
for (int i = listeners.length - 2; i >= 0; i -=
2) {
if (listeners[i] == CellEditorListener.class) {
if (changeEvent == null)
changeEvent =
new ChangeEvent(this);
((CellEditorListener) listeners[i + 1])
.editingCanceled(changeEvent);
}
}
}
public void actionPerformed(ActionEvent event) {
fireEditingStopped();
}
// Implement the one CellEditor method that
// AbstractCellEditor doesn't.
public Object getCellEditorValue() {
return new Boolean(button.isSelected());
}
// Implement the one method defined by
// TableCellEditor.
public Component getTableCellEditorComponent(
JTable table, Object value,
boolean isSelected, int row, int column) {
if (!value.getClass().getName().equals(
Boolean.class.getName())) {
return null;
}
button.setSelected(value != null
&& value.getClass().getName().equals(
Boolean.class.getName())
&& ((Boolean) value).booleanValue());
TableModel model = table.getModel();
// Make all the radioButtons, except the
// selected
// TRUE
for (int i = 0; i < model.getRowCount(); i++) {
if (model.getValueAt(i, column) != null
&& model
.getValueAt(i, column)
.getClass()
.getName()
.equals(
Boolean.class
.getName())) {
model.setValueAt(Boolean.FALSE, i,
column);
}
}
model.setValueAt(Boolean.TRUE, row, column);
button
.setHorizontalAlignment(
SwingConstants.CENTER);
return button;
}
public boolean stopCellEditing() {
fireEditingStopped();
return true;
}
public void cancelCellEditing() {
fireEditingCanceled();
}
public boolean shouldSelectCell(EventObject eo) {
return true;
}
public boolean isCellEditable(EventObject eo) {
return true;
}
}
No comments:
Post a Comment