nanode
01-08-2001, 02:48 PM
I have a TableCellEditor sublcass that I want to implement at a specific cell on a JTable.
I understand how to do this for an entire column, but it must only be for a specific cell.
Here's my class:
package screenpainter.util;
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);
}
public Object getCellEditorValue() {
return savedColor;
}
private void changeColor(Color color) {
if(color != null) {
savedColor = color;
delegate.setBackground(color);
}
}
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
changeColor((Color)value);
return delegate;
}
}
my only though...
This class implements the TableCellEditor method: getTableCellEditorComponent(...) which takes column and row as args. Is there some one upon instantiation, that I can say "you are editing cell 2,3", then myJTable.setCellEditor(theAboveClass); ?
I understand how to do this for an entire column, but it must only be for a specific cell.
Here's my class:
package screenpainter.util;
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);
}
public Object getCellEditorValue() {
return savedColor;
}
private void changeColor(Color color) {
if(color != null) {
savedColor = color;
delegate.setBackground(color);
}
}
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
changeColor((Color)value);
return delegate;
}
}
my only though...
This class implements the TableCellEditor method: getTableCellEditorComponent(...) which takes column and row as args. Is there some one upon instantiation, that I can say "you are editing cell 2,3", then myJTable.setCellEditor(theAboveClass); ?