📄 coloreditor.java
字号:
package JFCBook.Chapter11.jdk13;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
public class ColorEditor extends AbstractCellEditor
implements TableCellEditor, Runnable {
public ColorEditor() {
editorComponent = new JLabel("Editing...");
createDialog();
}
// These methods implement the TableCellEditor interface
public Component getTableCellEditorComponent(JTable table,
Object value,
boolean isSelected,
int row, int column) {
if (value instanceof Color) {
currentColor = (Color)value;
colorChooser.setColor(currentColor);
}
return editorComponent;
}
public Object getCellEditorValue() {
return currentColor;
}
public boolean isCellEditable(EventObject evt) {
if (evt instanceof MouseEvent) {
if (((MouseEvent)evt).getClickCount() >= 2) {
return true;
}
return false;
}
return true; // Key events or editCellAt call
}
public boolean stopCellEditing() {
currentColor = colorChooser.getColor();
dismissDialog();
fireEditingStopped();
return true;
}
public void cancelCellEditing() {
dismissDialog();
fireEditingCanceled();
}
public void addCellEditorListener(CellEditorListener l) {
super.addCellEditorListener(l);
showDialog();
}
// These are methods that implement the internals
// of the editor itself.
protected void createDialog() {
if (dialog == null) {
ActionListener okListener = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
showingDialog = false;
ColorEditor.this.stopCellEditing();
}
};
ActionListener cancelListener = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
showingDialog = false;
ColorEditor.this.cancelCellEditing();
}
};
colorChooser = new JColorChooser();
dialog = JColorChooser.createDialog(null, "Choose a Color",
true, colorChooser,
okListener, cancelListener);
}
}
protected void showDialog() {
if (showingDialog == false) {
showingDialog = true;
SwingUtilities.invokeLater(this);
}
}
protected void dismissDialog() {
if (showingDialog == true) {
showingDialog = false;
dialog.dispose();
}
}
public void run() {
dialog.setVisible(true);
}
protected JLabel editorComponent;
protected Color currentColor;
protected JDialog dialog;
protected JColorChooser colorChooser;
protected boolean showingDialog;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -