Source Code Cross Referenced for IntegerTextField.java in  » Testing » jacareto » jacareto » toolkit » swing » 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 » Testing » jacareto » jacareto.toolkit.swing 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Jacareto Copyright (c) 2002-2005
003:         * Applied Computer Science Research Group, Darmstadt University of
004:         * Technology, Institute of Mathematics & Computer Science,
005:         * Ludwigsburg University of Education, and Computer Based
006:         * Learning Research Group, Aachen University. All rights reserved.
007:         *
008:         * Jacareto is free software; you can redistribute it and/or
009:         * modify it under the terms of the GNU General Public
010:         * License as published by the Free Software Foundation; either
011:         * version 2 of the License, or (at your option) any later version.
012:         *
013:         * Jacareto is distributed in the hope that it will be useful,
014:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
015:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
016:         * General Public License for more details.
017:         *
018:         * You should have received a copy of the GNU General Public
019:         * License along with Jacareto; if not, write to the Free
020:         * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021:         *
022:         */
023:
024:        package jacareto.toolkit.swing;
025:
026:        import java.awt.event.ActionEvent;
027:        import java.awt.event.KeyEvent;
028:
029:        import javax.swing.AbstractAction;
030:        import javax.swing.JTextField;
031:        import javax.swing.KeyStroke;
032:        import javax.swing.text.Keymap;
033:
034:        /**
035:         * A text field which just allows input of integer numbers. BUGS: You can insert non-integer text
036:         * with copy&paste at the moment.
037:         *
038:         * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
039:         * @version 1.0
040:         */
041:        public class IntegerTextField extends JTextField {
042:            /**
043:             * Creates a new integer text field with the specified number of columns. The initial text
044:             * value is set to 0.
045:             *
046:             * @param columns the number of columns.
047:             */
048:            public IntegerTextField(int columns) {
049:                this (0, columns);
050:            }
051:
052:            /**
053:             * Creates a new integer text field with the specified initial value and number of columns.
054:             *
055:             * @param value the initial integer value
056:             * @param columns the number of columns.
057:             */
058:            public IntegerTextField(int value, int columns) {
059:                super (columns);
060:                setValue(value);
061:
062:                AddToTextAction addToTextAction = new AddToTextAction();
063:                Keymap keymap = addKeymap("IntegerTextFieldKeyMap", getKeymap());
064:                keymap.setDefaultAction(new AbstractAction() {
065:                    public void actionPerformed(ActionEvent e) {
066:                    };
067:                });
068:
069:                for (int i = 0; i <= 9; i++) {
070:                    keymap.addActionForKeyStroke(KeyStroke
071:                            .getKeyStroke(("" + i).charAt(0)), addToTextAction);
072:                }
073:
074:                keymap.addActionForKeyStroke(KeyStroke.getKeyStroke(
075:                        KeyEvent.VK_MINUS, 0), addToTextAction);
076:                setKeymap(keymap);
077:            }
078:
079:            /**
080:             * Sets the value of the text field to the specified value
081:             *
082:             * @param value the new value of the text field
083:             */
084:            public void setValue(int value) {
085:                setText("" + value);
086:            }
087:
088:            /**
089:             * Returns the integer value of the text field, or 0 if the the actual text is no number ("" or
090:             * "-", for example).
091:             *
092:             * @return the value
093:             */
094:            public int getValue() {
095:                try {
096:                    return (new Integer(getText())).intValue();
097:                } catch (NumberFormatException n) {
098:                    return 0;
099:                }
100:            }
101:
102:            /**
103:             * Sets the text of the textfield. You should not use this method, but the method {@link
104:             * #setValue(int)}.
105:             *
106:             * @param newText The new text of the text field. The text must be an integer or the empty
107:             *        string or <code>null</code>. In the second and the third case the text will be set
108:             *        to 0.
109:             */
110:            public void setText(String newText) {
111:                try {
112:                    if ((newText == null) || newText.equals("")) {
113:                        super .setText("0");
114:                    } else if (newText.equals("-")) {
115:                        super .setText("-");
116:                    } else {
117:                        super .setText(newText);
118:                    }
119:
120:                    setCaretPosition(0);
121:                } catch (NumberFormatException n) {
122:                    // ignored
123:                }
124:            }
125:
126:            class AddToTextAction extends AbstractAction {
127:                //~ Methods --------------------------------------------------------------------------------
128:
129:                public void actionPerformed(ActionEvent e) {
130:                    if (!(e.getActionCommand().equals("-"))
131:                            || ((getCaretPosition() == 0) && !getText().equals(
132:                                    "0"))) {
133:                        replaceSelection(e.getActionCommand());
134:                    }
135:                }
136:            }
137:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.