KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mc4j > console > swing > editor > ButtonEditor


1 /*
2  * Copyright 2002-2004 Greg Hinkle
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.mc4j.console.swing.editor;
18
19 import javax.swing.*;
20 import java.awt.event.ActionListener JavaDoc;
21 import java.awt.event.ActionEvent JavaDoc;
22 import java.awt.*;
23
24 /**
25  * @author Greg Hinkle (ghinkle@users.sourceforge.net), Feb 3, 2005
26  * @version $Revision: 570 $($Author: ghinkl $ / $Date: 2006-04-12 15:14:16 -0400 (Wed, 12 Apr 2006) $)
27  */

28
29 public class ButtonEditor extends DefaultCellEditor {
30     protected JButton button;
31     private String JavaDoc label;
32     private boolean isPushed;
33
34     public ButtonEditor(JCheckBox checkBox) {
35         super(checkBox);
36         button = new JButton();
37         button.setOpaque(true);
38         button.addActionListener(new ActionListener JavaDoc() {
39             public void actionPerformed(ActionEvent JavaDoc e) {
40                 fireEditingStopped();
41             }
42         });
43     }
44
45     
46
47     public Component getTableCellEditorComponent(JTable table, Object JavaDoc value,
48                                                  boolean isSelected, int row, int column) {
49         if (isSelected) {
50             button.setForeground(table.getSelectionForeground());
51             button.setBackground(table.getSelectionBackground());
52         } else {
53             button.setForeground(table.getForeground());
54             button.setBackground(table.getBackground());
55         }
56         label = (value == null) ? "" : value.toString();
57         button.setText(label);
58         isPushed = true;
59         return button;
60     }
61
62     public Object JavaDoc getCellEditorValue() {
63         if (isPushed) {
64             //
65
//
66
JOptionPane.showMessageDialog(button, label + ": Ouch!");
67             // System.out.println(label + ": Ouch!");
68
}
69         isPushed = false;
70         return new String JavaDoc(label);
71     }
72
73     public boolean stopCellEditing() {
74         isPushed = false;
75         return super.stopCellEditing();
76     }
77
78     protected void fireEditingStopped() {
79         super.fireEditingStopped();
80     }
81 }
Popular Tags