1 package net.sf.openrocket.gui.preset;
3 import javax.swing.AbstractCellEditor;
4 import javax.swing.Action;
5 import javax.swing.Icon;
6 import javax.swing.JButton;
7 import javax.swing.JTable;
8 import javax.swing.UIManager;
9 import javax.swing.border.Border;
10 import javax.swing.border.LineBorder;
11 import javax.swing.table.TableCellEditor;
12 import javax.swing.table.TableCellRenderer;
13 import javax.swing.table.TableColumnModel;
14 import java.awt.Color;
15 import java.awt.Component;
16 import java.awt.event.ActionEvent;
17 import java.awt.event.ActionListener;
18 import java.awt.event.MouseEvent;
19 import java.awt.event.MouseListener;
22 * The ButtonColumn class provides a renderer and an editor that looks like a
23 * JButton. The renderer and editor will then be used for a specified column
24 * in the table. The TableModel will contain the String to be displayed on
27 * The button can be invoked by a mouse click or by pressing the space bar
28 * when the cell has focus. Optionaly a mnemonic can be set to invoke the
29 * button. When the button is invoked the provided Action is invoked. The
30 * source of the Action will be the table. The action command will contain
31 * the model row number of the button that was clicked.
33 * Credits: A post by Rob Camick http://tips4java.wordpress.com/2009/07/12/table-button-column/
35 public class ButtonColumn extends AbstractCellEditor
36 implements TableCellRenderer, TableCellEditor, ActionListener, MouseListener
39 private Action action;
41 private Border originalBorder;
42 private Border focusBorder;
44 private JButton renderButton;
45 private JButton editButton;
46 private Object editorValue;
47 private boolean isButtonColumnEditor;
50 * Create the ButtonColumn to be used as a renderer and editor. The
51 * renderer and editor will automatically be installed on the TableColumn
52 * of the specified column.
54 * @param table the table containing the button renderer/editor
55 * @param action the Action to be invoked when the button is invoked
56 * @param column the column to which the button renderer/editor is added
58 public ButtonColumn(JTable table, Action action, int column)
63 renderButton = new JButton();
64 editButton = new JButton();
65 editButton.setFocusPainted( false );
66 editButton.addActionListener( this );
67 originalBorder = editButton.getBorder();
68 setFocusBorder( new LineBorder(Color.BLUE) );
70 TableColumnModel columnModel = table.getColumnModel();
71 columnModel.getColumn(column).setCellRenderer( this );
72 columnModel.getColumn(column).setCellEditor( this );
73 table.addMouseListener( this );
78 * Get foreground color of the button when the cell has focus
80 * @return the foreground color
82 public Border getFocusBorder()
88 * The foreground color of the button when the cell has focus
90 * @param focusBorder the foreground color
92 public void setFocusBorder(Border focusBorder)
94 this.focusBorder = focusBorder;
95 editButton.setBorder( focusBorder );
98 public int getMnemonic()
104 * The mnemonic to activate the button when the cell has focus
106 * @param mnemonic the mnemonic
108 public void setMnemonic(int mnemonic)
110 this.mnemonic = mnemonic;
111 renderButton.setMnemonic(mnemonic);
112 editButton.setMnemonic(mnemonic);
116 public Component getTableCellEditorComponent(
117 JTable table, Object value, boolean isSelected, int row, int column)
121 editButton.setText( "" );
122 editButton.setIcon( null );
124 else if (value instanceof Icon)
126 editButton.setText( "" );
127 editButton.setIcon( (Icon)value );
131 editButton.setText( value.toString() );
132 editButton.setIcon( null );
135 this.editorValue = value;
140 public Object getCellEditorValue()
146 // Implement TableCellRenderer interface
148 public Component getTableCellRendererComponent(
149 JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
153 renderButton.setForeground(table.getSelectionForeground());
154 renderButton.setBackground(table.getSelectionBackground());
158 renderButton.setForeground(table.getForeground());
159 renderButton.setBackground(UIManager.getColor("Button.background"));
164 renderButton.setBorder( focusBorder );
168 renderButton.setBorder( originalBorder );
171 // renderButton.setText( (value == null) ? "" : value.toString() );
174 renderButton.setText( "" );
175 renderButton.setIcon( null );
177 else if (value instanceof Icon)
179 renderButton.setText( "" );
180 renderButton.setIcon( (Icon)value );
184 renderButton.setText( value.toString() );
185 renderButton.setIcon( null );
192 // Implement ActionListener interface
195 * The button has been pressed. Stop editing and invoke the custom Action
197 public void actionPerformed(ActionEvent e)
199 int row = table.convertRowIndexToModel( table.getEditingRow() );
200 fireEditingStopped();
204 ActionEvent event = new ActionEvent(
206 ActionEvent.ACTION_PERFORMED,
208 action.actionPerformed(event);
212 // Implement MouseListener interface
215 * When the mouse is pressed the editor is invoked. If you then drag
216 * the mouse to another cell before releasing it, the editor is still
217 * active. Make sure editing is stopped when the mouse is released.
219 public void mousePressed(MouseEvent e)
221 if (table.isEditing()
222 && table.getCellEditor() == this)
223 isButtonColumnEditor = true;
226 public void mouseReleased(MouseEvent e)
228 if (isButtonColumnEditor
229 && table.isEditing())
230 table.getCellEditor().stopCellEditing();
232 isButtonColumnEditor = false;
235 public void mouseClicked(MouseEvent e) {}
236 public void mouseEntered(MouseEvent e) {}
237 public void mouseExited(MouseEvent e) {}