Source Code Cross Referenced for ComboBoxCellEditor.java in  » Swing-Library » swingx » org » jdesktop » swingx » autocomplete » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Swing Library » swingx » org.jdesktop.swingx.autocomplete 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Id: ComboBoxCellEditor.java,v 1.5 2007/01/29 08:52:44 Bierhance Exp $
003:         *
004:         * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
005:         * Santa Clara, California 95054, U.S.A. All rights reserved.
006:         *
007:         * This library is free software; you can redistribute it and/or
008:         * modify it under the terms of the GNU Lesser General Public
009:         * License as published by the Free Software Foundation; either
010:         * version 2.1 of the License, or (at your option) any later version.
011:         * 
012:         * This library is distributed in the hope that it will be useful,
013:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
014:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015:         * Lesser General Public License for more details.
016:         * 
017:         * You should have received a copy of the GNU Lesser General Public
018:         * License along with this library; if not, write to the Free Software
019:         * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
020:         */
021:        package org.jdesktop.swingx.autocomplete;
022:
023:        import java.awt.event.ActionEvent;
024:        import java.awt.event.KeyAdapter;
025:        import java.awt.event.KeyEvent;
026:        import java.beans.PropertyChangeEvent;
027:        import java.beans.PropertyChangeListener;
028:        import java.io.Serializable;
029:
030:        import javax.swing.AbstractCellEditor;
031:        import javax.swing.ComboBoxEditor;
032:        import javax.swing.JComboBox;
033:        import javax.swing.JComponent;
034:        import javax.swing.table.TableCellEditor;
035:
036:        /**
037:         * <p>This is a cell editor that can be used when a combo box (that has been set
038:         * up for automatic completion) is to be used in a JTable. The
039:         * {@link javax.swing.DefaultCellEditor DefaultCellEditor} won't work in this
040:         * case, because each time an item gets selected it stops cell editing and hides
041:         * the combo box.
042:         * </p>
043:         * <p>
044:         * Usage example:
045:         * </p>
046:         * <p>
047:         * <pre><code>
048:         * JTable table = ...;
049:         * JComboBox comboBox = ...;
050:         * ...
051:         * TableColumn column = table.getColumnModel().getColumn(0);
052:         * column.setCellEditor(new ComboBoxCellEditor(comboBox));
053:         * </code></pre>
054:         * </p>
055:         */
056:        public class ComboBoxCellEditor extends AbstractCellEditor implements 
057:                TableCellEditor, Serializable {
058:
059:            /** the combo box */
060:            private JComboBox comboBox;
061:            /** a Listener listening for key events (handling enter-key) and changes of
062:             * the combo box' editor component.*/
063:            private Handler handler;
064:
065:            /**
066:             * Creates a new ComboBoxCellEditor.
067:             * @param comboBox the comboBox that should be used as the cell editor.
068:             */
069:            public ComboBoxCellEditor(final JComboBox comboBox) {
070:                this .comboBox = comboBox;
071:
072:                handler = new Handler();
073:
074:                // Don't do this:
075:                // this.comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
076:                // it probably breaks various things
077:
078:                // hitting enter in the combo box should stop cellediting
079:                JComponent editorComponent = (JComponent) comboBox.getEditor()
080:                        .getEditorComponent();
081:                editorComponent.addKeyListener(handler);
082:                // remove the editor's border - the cell itself already has one
083:                editorComponent.setBorder(null);
084:
085:                // editor component might change (e.g. a look&feel change)
086:                // the new editor component needs to be modified then (keyListener, border)
087:                comboBox.addPropertyChangeListener(handler);
088:            }
089:
090:            // ------ Implementing CellEditor ------
091:            /**
092:             * Returns the value contained in the combo box
093:             * @return the value contained in the combo box
094:             */
095:            public Object getCellEditorValue() {
096:                return comboBox.getSelectedItem();
097:            }
098:
099:            /**
100:             * Tells the combo box to stop editing and accept any partially edited value as the value of the combo box.
101:             * Always returns true.
102:             * @return true
103:             */
104:            public boolean stopCellEditing() {
105:                if (comboBox.isEditable()) {
106:                    // Notify the combo box that editing has stopped (e.g. User pressed F2)
107:                    comboBox.actionPerformed(new ActionEvent(this , 0, ""));
108:                }
109:                fireEditingStopped();
110:                return true;
111:            }
112:
113:            // ------ Implementing TableCellEditor ------
114:            /**
115:             * Sets an initial value for the combo box.
116:             * Returns the combo box that should be added to the client's Component hierarchy.
117:             * Once installed in the client's hierarchy this combo box will then be able to draw and receive user input.
118:             * @param table the JTable that is asking the editor to edit; can be null
119:             * @param value the value of the cell to be edited; null is a valid value
120:             * @param isSelected will be ignored
121:             * @param row the row of the cell being edited
122:             * @param column the column of the cell being edited
123:             * @return the combo box for editing
124:             */
125:            public java.awt.Component getTableCellEditorComponent(
126:                    javax.swing.JTable table, Object value, boolean isSelected,
127:                    int row, int column) {
128:                comboBox.setSelectedItem(value);
129:                return comboBox;
130:            }
131:
132:            // ------ Implementing TreeCellEditor ------
133:            //    public java.awt.Component getTreeCellEditorComponent(javax.swing.JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row) {
134:            //        String stringValue = tree.convertValueToText(value, isSelected, expanded, leaf, row, false);
135:            //        comboBox.setSelectedItem(stringValue);
136:            //        return comboBox;
137:            //    }
138:
139:            class Handler extends KeyAdapter implements  PropertyChangeListener {
140:                public void keyPressed(KeyEvent keyEvent) {
141:                    int keyCode = keyEvent.getKeyCode();
142:                    if (keyCode == keyEvent.VK_ENTER)
143:                        stopCellEditing();
144:                }
145:
146:                public void propertyChange(PropertyChangeEvent e) {
147:                    if (e.getPropertyName().equals("editor")) {
148:                        ComboBoxEditor editor = comboBox.getEditor();
149:                        if (editor != null
150:                                && editor.getEditorComponent() != null) {
151:                            JComponent editorComponent = (JComponent) comboBox
152:                                    .getEditor().getEditorComponent();
153:                            editorComponent.addKeyListener(this);
154:                            editorComponent.setBorder(null);
155:                        }
156:                    }
157:                }
158:            }
159:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.