Skip to content

Commit ecb89c1

Browse files
authored
Add files via upload
1 parent 027c1f2 commit ecb89c1

File tree

1 file changed

+237
-0
lines changed

1 file changed

+237
-0
lines changed

source/ButtonColumn.java

Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
import java.awt.*;
2+
import java.awt.event.*;
3+
import javax.swing.*;
4+
import javax.swing.border.*;
5+
import javax.swing.table.*;
6+
7+
/**
8+
* The ButtonColumn class provides a renderer and an editor that looks like a
9+
* JButton. The renderer and editor will then be used for a specified column
10+
* in the table. The TableModel will contain the String to be displayed on
11+
* the button.
12+
*
13+
* The button can be invoked by a mouse click or by pressing the space bar
14+
* when the cell has focus. Optionaly a mnemonic can be set to invoke the
15+
* button. When the button is invoked the provided Action is invoked. The
16+
* source of the Action will be the table. The action command will contain
17+
* the model row number of the button that was clicked.
18+
*
19+
*/
20+
public class ButtonColumn extends AbstractCellEditor
21+
implements TableCellRenderer, TableCellEditor, ActionListener, MouseListener
22+
{
23+
private JTable table;
24+
private Action action;
25+
private int mnemonic;
26+
private Border originalBorder;
27+
private Border focusBorder;
28+
29+
private JButton renderButton;
30+
private JButton editButton;
31+
private Object editorValue;
32+
private boolean isButtonColumnEditor;
33+
34+
/**
35+
* Create the ButtonColumn to be used as a renderer and editor. The
36+
* renderer and editor will automatically be installed on the TableColumn
37+
* of the specified column.
38+
*
39+
* @param table the table containing the button renderer/editor
40+
* @param action the Action to be invoked when the button is invoked
41+
* @param column the column to which the button renderer/editor is added
42+
*/
43+
public ButtonColumn(JTable table, Action action, int column)
44+
{
45+
this.table = table;
46+
this.action = action;
47+
48+
renderButton = new JButton();
49+
editButton = new JButton();
50+
editButton.setFocusPainted( false );
51+
editButton.addActionListener( this );
52+
originalBorder = editButton.getBorder();
53+
setFocusBorder( new LineBorder(Color.BLUE) );
54+
55+
TableColumnModel columnModel = table.getColumnModel();
56+
columnModel.getColumn(column).setCellRenderer( this );
57+
columnModel.getColumn(column).setCellEditor( this );
58+
table.addMouseListener( this );
59+
}
60+
61+
62+
/**
63+
* Get foreground color of the button when the cell has focus
64+
*
65+
* @return the foreground color
66+
*/
67+
public Border getFocusBorder()
68+
{
69+
return focusBorder;
70+
}
71+
72+
/**
73+
* The foreground color of the button when the cell has focus
74+
*
75+
* @param focusBorder the foreground color
76+
*/
77+
public void setFocusBorder(Border focusBorder)
78+
{
79+
this.focusBorder = focusBorder;
80+
editButton.setBorder( focusBorder );
81+
}
82+
83+
public int getMnemonic()
84+
{
85+
return mnemonic;
86+
}
87+
88+
/**
89+
* The mnemonic to activate the button when the cell has focus
90+
*
91+
* @param mnemonic the mnemonic
92+
*/
93+
public void setMnemonic(int mnemonic)
94+
{
95+
this.mnemonic = mnemonic;
96+
renderButton.setMnemonic(mnemonic);
97+
editButton.setMnemonic(mnemonic);
98+
/*
99+
Action mnemonicAction = new AbstractAction()
100+
{
101+
public void actionPerformed(ActionEvent e)
102+
{
103+
ButtonColumn.this.actionPerformed(e);
104+
}
105+
};
106+
107+
String key = "mnemonicAction";
108+
KeyStroke keyStroke = KeyStroke.getKeyStroke(mnemonic, 0);
109+
editButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(keyStroke, key);
110+
editButton.getActionMap().put(key, mnemonicAction);
111+
*/
112+
}
113+
114+
@Override
115+
public Component getTableCellEditorComponent(
116+
JTable table, Object value, boolean isSelected, int row, int column)
117+
{
118+
if (value == null)
119+
{
120+
editButton.setText( "" );
121+
editButton.setIcon( null );
122+
}
123+
else if (value instanceof Icon)
124+
{
125+
editButton.setText( "" );
126+
editButton.setIcon( (Icon)value );
127+
}
128+
else
129+
{
130+
editButton.setText( value.toString() );
131+
editButton.setIcon( null );
132+
}
133+
134+
this.editorValue = value;
135+
return editButton;
136+
}
137+
138+
@Override
139+
public Object getCellEditorValue()
140+
{
141+
return editorValue;
142+
}
143+
144+
//
145+
// Implement TableCellRenderer interface
146+
//
147+
public Component getTableCellRendererComponent(
148+
JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
149+
{
150+
if (isSelected)
151+
{
152+
renderButton.setForeground(table.getSelectionForeground());
153+
renderButton.setBackground(table.getSelectionBackground());
154+
}
155+
else
156+
{
157+
renderButton.setForeground(table.getForeground());
158+
renderButton.setBackground(UIManager.getColor("Button.background"));
159+
}
160+
161+
if (hasFocus)
162+
{
163+
renderButton.setBorder( focusBorder );
164+
}
165+
else
166+
{
167+
renderButton.setBorder( originalBorder );
168+
}
169+
170+
// renderButton.setText( (value == null) ? "" : value.toString() );
171+
if (value == null)
172+
{
173+
renderButton.setText( "" );
174+
renderButton.setIcon( null );
175+
}
176+
else if (value instanceof Icon)
177+
{
178+
renderButton.setText( "" );
179+
renderButton.setIcon( (Icon)value );
180+
}
181+
else
182+
{
183+
renderButton.setText( value.toString() );
184+
renderButton.setIcon( null );
185+
}
186+
187+
return renderButton;
188+
}
189+
190+
//
191+
// Implement ActionListener interface
192+
//
193+
/*
194+
* The button has been pressed. Stop editing and invoke the custom Action
195+
*/
196+
public void actionPerformed(ActionEvent e)
197+
{
198+
int row = table.convertRowIndexToModel( table.getEditingRow() );
199+
fireEditingStopped();
200+
201+
// Invoke the Action
202+
203+
ActionEvent event = new ActionEvent(
204+
table,
205+
ActionEvent.ACTION_PERFORMED,
206+
"" + row);
207+
action.actionPerformed(event);
208+
}
209+
210+
//
211+
// Implement MouseListener interface
212+
//
213+
/*
214+
* When the mouse is pressed the editor is invoked. If you then then drag
215+
* the mouse to another cell before releasing it, the editor is still
216+
* active. Make sure editing is stopped when the mouse is released.
217+
*/
218+
public void mousePressed(MouseEvent e)
219+
{
220+
if (table.isEditing()
221+
&& table.getCellEditor() == this)
222+
isButtonColumnEditor = true;
223+
}
224+
225+
public void mouseReleased(MouseEvent e)
226+
{
227+
if (isButtonColumnEditor
228+
&& table.isEditing())
229+
table.getCellEditor().stopCellEditing();
230+
231+
isButtonColumnEditor = false;
232+
}
233+
234+
public void mouseClicked(MouseEvent e) {}
235+
public void mouseEntered(MouseEvent e) {}
236+
public void mouseExited(MouseEvent e) {}
237+
}

0 commit comments

Comments
 (0)