Source Code Cross Referenced for NumberChooser.java in  » Testing » abbot-1.0.1 » example » 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 » abbot 1.0.1 » example 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package example;
002:
003:        import java.awt.*;
004:        import java.awt.event.*;
005:        import java.beans.*;
006:
007:        import javax.swing.*;
008:
009:        /**
010:         * Pick a number, any number.  Well, maybe not any number.
011:         */
012:
013:        public class NumberChooser extends JPanel implements  ActionListener {
014:            protected ArrowButton up, down;
015:            protected JTextField field;
016:
017:            public NumberChooser() {
018:                setLayout(new BorderLayout());
019:                JPanel buttons = new JPanel(new GridLayout(0, 1));
020:                down = new ArrowButton(ArrowButton.DOWN);
021:                down.addActionListener(this );
022:                add(field = new JTextField(4), BorderLayout.CENTER);
023:                field.addActionListener(this );
024:                up = new ArrowButton(ArrowButton.UP);
025:                up.addActionListener(this );
026:                buttons.add(up);
027:                buttons.add(down);
028:                add(buttons, BorderLayout.EAST);
029:            }
030:
031:            public NumberChooser(int min, int max, int value) {
032:                this ();
033:                setMinimum(min);
034:                setMaximum(max);
035:                setValue(value);
036:            }
037:
038:            public void requestFocus() {
039:                field.requestFocus();
040:            }
041:
042:            public void setColumns(int c) {
043:                field.setColumns(c);
044:            }
045:
046:            public int getColumns() {
047:                return field.getColumns();
048:            }
049:
050:            public synchronized void setValue(int v) {
051:                field.setText(String.valueOf(v));
052:                fireValueChange(getValue());
053:            }
054:
055:            public int getValue() {
056:                try {
057:                    return clamp(Integer.parseInt(field.getText()));
058:                } catch (NumberFormatException ex) {
059:                    return clamp(0);
060:                }
061:            }
062:
063:            protected int minimum = Integer.MIN_VALUE;
064:
065:            public synchronized void setMinimum(int m) {
066:                minimum = m;
067:            }
068:
069:            public int getMinimum() {
070:                return minimum;
071:            }
072:
073:            protected int maximum = Integer.MAX_VALUE;
074:
075:            public synchronized void setMaximum(int m) {
076:                maximum = m;
077:            }
078:
079:            public int getMaximum() {
080:                return maximum;
081:            }
082:
083:            protected int clamp(int v) {
084:                return Math.max(minimum, Math.min(maximum, v));
085:            }
086:
087:            protected int step = 1;
088:
089:            public synchronized void setStep(int s) {
090:                if (s <= 0)
091:                    throw new IllegalArgumentException("Step too small (" + s
092:                            + ").");
093:                step = s;
094:            }
095:
096:            public int getStep() {
097:                return step;
098:            }
099:
100:            public synchronized void actionPerformed(ActionEvent e) {
101:                int value = getValue();
102:                if (e.getSource() == down) {
103:                    if (value > minimum) {
104:                        value = (value - step > value) ? minimum : clamp(value
105:                                - step);
106:                        setValue(value);
107:                        fireValueChange(value);
108:                    }
109:                } else if (e.getSource() == up) {
110:                    if (value < maximum) {
111:                        value = (value + step < value) ? maximum : clamp(value
112:                                + step);
113:                        setValue(value);
114:                        fireValueChange(value);
115:                    }
116:                } else if (e.getSource() == field) {
117:                    try {
118:                        int v = Integer.parseInt(e.getActionCommand());
119:                        if ((v < minimum) || (v > maximum))
120:                            getToolkit().beep();
121:                        else
122:                            fireValueChange(v);
123:                    } catch (NumberFormatException ex) {
124:                        getToolkit().beep();
125:                    }
126:                }
127:            }
128:
129:            protected PropertyChangeSupport listeners = new PropertyChangeSupport(
130:                    this );
131:
132:            public void addPropertyChangeListener(PropertyChangeListener l) {
133:                listeners.addPropertyChangeListener(l);
134:            }
135:
136:            public void removePropertyChangeListener(PropertyChangeListener l) {
137:                listeners.removePropertyChangeListener(l);
138:            }
139:
140:            Integer oValue = new Integer(0);
141:
142:            protected void fireValueChange(int v) {
143:                listeners.firePropertyChange("value", oValue,
144:                        oValue = new Integer(v));
145:            }
146:
147:            public static void main(String[] args) {
148:                try {
149:                    final JFrame frame = new JFrame("NumberChooser unit test");
150:                    frame.addWindowListener(new WindowAdapter() {
151:                        public void windowClosing(WindowEvent e) {
152:                            Window w = e.getWindow();
153:                            w.setVisible(false);
154:                            w.dispose();
155:                            System.exit(0);
156:                        }
157:                    });
158:                    JPanel panel = new JPanel(new GridLayout(0, 1));
159:                    final JLabel label = new JLabel("Enter a value");
160:                    NumberChooser chooser = new NumberChooser();
161:                    panel.add(label);
162:                    panel.add(chooser);
163:                    chooser
164:                            .addPropertyChangeListener(new PropertyChangeListener() {
165:                                public void propertyChange(
166:                                        PropertyChangeEvent ev) {
167:                                    System.out.println(((Integer) ev
168:                                            .getNewValue()).intValue());
169:                                }
170:                            });
171:                    frame.getContentPane().add(panel);
172:                    frame.pack();
173:                    frame.setVisible(true);
174:                } catch (Exception exc) {
175:                    System.out.println("Exception: " + exc.getMessage());
176:                    exc.printStackTrace();
177:                    System.exit(1);
178:                }
179:            }
180:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.