|
1 /* |
|
2 * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 package com.sun.swingset3.demos.textfield; |
|
24 |
|
25 import java.awt.*; |
|
26 import java.awt.event.ActionEvent; |
|
27 import java.util.*; |
|
28 import javax.swing.*; |
|
29 import javax.swing.event.DocumentEvent; |
|
30 import javax.swing.event.DocumentListener; |
|
31 |
|
32 import com.sun.swingset3.demos.JGridPanel; |
|
33 import com.sun.swingset3.demos.ResourceManager; |
|
34 import com.sun.swingset3.DemoProperties; |
|
35 |
|
36 /** |
|
37 * JTextField Demo |
|
38 * |
|
39 * @author Pavel Porvatov |
|
40 */ |
|
41 @DemoProperties( |
|
42 value = "TextField Demo", |
|
43 category = "Text", |
|
44 description = "Demonstrates the JTextField, a control which allows to input text", |
|
45 sourceFiles = { |
|
46 "com/sun/swingset3/demos/textfield/TextFieldDemo.java", |
|
47 "com/sun/swingset3/demos/textfield/JHistoryTextField.java", |
|
48 "com/sun/swingset3/demos/JGridPanel.java", |
|
49 "com/sun/swingset3/demos/ResourceManager.java", |
|
50 "com/sun/swingset3/demos/textfield/resources/TextFieldDemo.properties", |
|
51 "com/sun/swingset3/demos/textfield/resources/images/TextFieldDemo.gif" |
|
52 } |
|
53 ) |
|
54 public class TextFieldDemo extends JPanel { |
|
55 |
|
56 private static final ResourceManager resourceManager = new ResourceManager(TextFieldDemo.class); |
|
57 public static final String DEMO_TITLE = TextFieldDemo.class.getAnnotation(DemoProperties.class).value(); |
|
58 |
|
59 private final JLabel lbHistoryTextField = new JLabel(resourceManager.getString("TextFieldDemo.historytextfield.text")); |
|
60 |
|
61 private final JHistoryTextField tfHistory = new JHistoryTextField(); |
|
62 |
|
63 private final JLabel lbDow = new JLabel(resourceManager.getString("TextFieldDemo.dow.text")); |
|
64 |
|
65 private final JFormattedTextField tfDow = new JFormattedTextField(); |
|
66 |
|
67 private final JButton btnGo = new JButton(GO); |
|
68 public static final String GO = resourceManager.getString("TextFieldDemo.go.text"); |
|
69 |
|
70 private final JLabel lbDowResult = new JLabel(); |
|
71 |
|
72 private final JLabel lbPassword = new JLabel(resourceManager.getString("TextFieldDemo.password.text")); |
|
73 |
|
74 private final JPasswordField tfPassword1 = new JPasswordField(20); |
|
75 |
|
76 private final JPasswordField tfPassword2 = new JPasswordField(20); |
|
77 |
|
78 private final DocumentListener passwordListener = new DocumentListener() { |
|
79 |
|
80 @Override |
|
81 public void insertUpdate(DocumentEvent e) { |
|
82 highlightPasswords(); |
|
83 } |
|
84 |
|
85 @Override |
|
86 public void removeUpdate(DocumentEvent e) { |
|
87 highlightPasswords(); |
|
88 } |
|
89 |
|
90 @Override |
|
91 public void changedUpdate(DocumentEvent e) { |
|
92 highlightPasswords(); |
|
93 } |
|
94 |
|
95 private void highlightPasswords() { |
|
96 Color color; |
|
97 |
|
98 if (tfPassword1.getPassword().length > 0 |
|
99 && Arrays.equals(tfPassword1.getPassword(), tfPassword2.getPassword())) { |
|
100 color = Color.GREEN; |
|
101 } else { |
|
102 color = UIManager.getColor("TextField.background"); |
|
103 } |
|
104 |
|
105 tfPassword1.setBackground(color); |
|
106 tfPassword2.setBackground(color); |
|
107 } |
|
108 }; |
|
109 |
|
110 /** |
|
111 * main method allows us to run as a standalone demo. |
|
112 * |
|
113 * @param args |
|
114 */ |
|
115 public static void main(String[] args) { |
|
116 JFrame frame = new JFrame(DEMO_TITLE); |
|
117 |
|
118 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
|
119 frame.getContentPane().add(new TextFieldDemo()); |
|
120 frame.setPreferredSize(new Dimension(800, 600)); |
|
121 frame.pack(); |
|
122 frame.setLocationRelativeTo(null); |
|
123 frame.setVisible(true); |
|
124 } |
|
125 |
|
126 public TextFieldDemo() { |
|
127 setLayout(new BorderLayout()); |
|
128 |
|
129 initUI(); |
|
130 |
|
131 tfDow.setValue(new Date()); |
|
132 |
|
133 btnGo.addActionListener((ActionEvent e) -> { |
|
134 Calendar calendar = Calendar.getInstance(Locale.ENGLISH); |
|
135 |
|
136 calendar.setTime((Date) tfDow.getValue()); |
|
137 |
|
138 lbDowResult.setText(calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.ENGLISH)); |
|
139 }); |
|
140 |
|
141 tfPassword1.getDocument().addDocumentListener(passwordListener); |
|
142 |
|
143 tfPassword2.getDocument().addDocumentListener(passwordListener); |
|
144 } |
|
145 |
|
146 private void initUI() { |
|
147 tfHistory.setHistory(Arrays.asList(resourceManager.getString("TextFieldDemo.history.words").split("\\,"))); |
|
148 |
|
149 JGridPanel pnDow = new JGridPanel(3, 2); |
|
150 |
|
151 pnDow.cell(tfDow). |
|
152 cell(btnGo). |
|
153 cell(lbDowResult); |
|
154 |
|
155 JGridPanel pnPassword = new JGridPanel(3, 2); |
|
156 |
|
157 pnPassword.cell(tfPassword1). |
|
158 cell(tfPassword2). |
|
159 cell(); |
|
160 |
|
161 JGridPanel pnContent = new JGridPanel(1, 0, 6); |
|
162 |
|
163 pnContent.setBorderEqual(10); |
|
164 |
|
165 pnContent.cell(lbHistoryTextField). |
|
166 cell(tfHistory). |
|
167 cell(lbDow, new Insets(20, 0, 0, 0)). |
|
168 cell(pnDow). |
|
169 cell(lbPassword, new Insets(20, 0, 0, 0)). |
|
170 cell(pnPassword). |
|
171 cell(); |
|
172 |
|
173 add(pnContent); |
|
174 } |
|
175 } |