1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
| import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;
import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
public abstract class TableButton extends AbstractCellEditor implements TableCellRenderer, TableCellEditor {
private static final long serialVersionUID = 1L;
private final JButton buttonRenderer;
private final JButton buttonEditor;
private final ButtonActionListener actionListener;
private Color unselectedBackground;
private Color unselectedForeground;
private transient Object data;
public TableButton() {
buttonRenderer = new JButton();
buttonEditor = new JButton();
actionListener = new ButtonActionListener();
buttonEditor.addActionListener(actionListener);
}
public void setForeground(Color c) {
buttonRenderer.setBackground(c);
buttonRenderer.setForeground(c);
unselectedForeground = c;
}
public void setBackground(Color c) {
buttonRenderer.setBackground(c);
buttonEditor.setBackground(c);
unselectedBackground = c;
}
public void updateUI() {
buttonRenderer.updateUI();
buttonEditor.updateUI();
setForeground(null);
setBackground(null);
}
public Object getCellEditorValue() {
return data;
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
decorateButton(buttonRenderer, table, value, isSelected, hasFocus, row, column);
return buttonRenderer;
}
protected void decorateButton(JButton button, JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
String text = getButtonText(table, value, isSelected, hasFocus, row, column);
if( text==null ) {
button.setText("");
}
else {
button.setText(text);
}
Color fg = null;
Color bg = null;
JTable.DropLocation dropLocation = table.getDropLocation();
if (dropLocation != null
&& !dropLocation.isInsertRow()
&& !dropLocation.isInsertColumn()
&& dropLocation.getRow() == row
&& dropLocation.getColumn() == column) {
fg = UIManager.getColor("Table.dropCellForeground");
bg = UIManager.getColor("Table.dropCellBackground");
isSelected = true;
}
if (isSelected) {
button.setForeground(fg == null ? table.getSelectionForeground()
: fg);
button.setBackground(bg == null ? table.getSelectionBackground()
: bg);
} else {
Color background = unselectedBackground != null
? unselectedBackground
: table.getBackground();
if (background == null || background instanceof javax.swing.plaf.UIResource) {
Color alternateColor = UIManager.getColor("Table.alternateRowColor");
if (alternateColor != null && row % 2 != 0) {
background = alternateColor;
}
}
button.setForeground(unselectedForeground != null
? unselectedForeground
: table.getForeground());
button.setBackground(background);
}
button.setFont(table.getFont());
if (hasFocus) {
if (!isSelected && table.isCellEditable(row, column)) {
Color col;
col = UIManager.getColor("Table.focusCellForeground");
if (col != null) {
button.setForeground(col);
}
col = UIManager.getColor("Table.focusCellBackground");
if (col != null) {
button.setBackground(col);
}
}
}
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
data=value;
decorateButton(buttonEditor, table, value, isSelected, true, row, column);
actionListener.setAction(table, value, row, column);
return buttonEditor;
}
protected abstract String getButtonText(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column);
protected abstract void actionPerformed(JTable table, Object value, int row, int column);
private class ButtonActionListener implements ActionListener, Serializable {
private static final long serialVersionUID = 1L;
private transient JTable table;
private transient Object value;
private transient int row;
private transient int column;
public ButtonActionListener() {
}
public void setAction(JTable table, Object value,
int row, int column) {
this.table=table;
this.value=value;
this.row=row;
this.column=column;
}
public void actionPerformed(ActionEvent e) {
TableButton.this.actionPerformed( table, value, row, column);
stopCellEditing();
}
}
public void install(JTable table, int column) {
table.getColumnModel().getColumn(column).setCellRenderer(this);
table.getColumnModel().getColumn(column).setCellEditor(this);
}
public void install(JTable table, Class<?> columnClass) {
table.setDefaultRenderer(columnClass,this);
table.setDefaultEditor(columnClass, this);
}
} |
Partager