1 |
781 |
jeremybenn |
/* TextFieldDemo.java -- An example showing various textfields in Swing.
|
2 |
|
|
Copyright (C) 2005, 2006 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath examples.
|
5 |
|
|
|
6 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License as published by
|
8 |
|
|
the Free Software Foundation; either version 2, or (at your option)
|
9 |
|
|
any later version.
|
10 |
|
|
|
11 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
|
|
General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License
|
17 |
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
18 |
|
|
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
|
|
02110-1301 USA.
|
20 |
|
|
*/
|
21 |
|
|
|
22 |
|
|
|
23 |
|
|
package gnu.classpath.examples.swing;
|
24 |
|
|
|
25 |
|
|
import java.awt.BorderLayout;
|
26 |
|
|
import java.awt.Color;
|
27 |
|
|
import java.awt.Font;
|
28 |
|
|
import java.awt.Graphics;
|
29 |
|
|
import java.awt.GridLayout;
|
30 |
|
|
import java.awt.Point;
|
31 |
|
|
import java.awt.Rectangle;
|
32 |
|
|
import java.awt.Shape;
|
33 |
|
|
import java.awt.event.ActionEvent;
|
34 |
|
|
import java.awt.event.ActionListener;
|
35 |
|
|
|
36 |
|
|
import javax.swing.BorderFactory;
|
37 |
|
|
import javax.swing.Box;
|
38 |
|
|
import javax.swing.BoxLayout;
|
39 |
|
|
import javax.swing.JButton;
|
40 |
|
|
import javax.swing.JCheckBox;
|
41 |
|
|
import javax.swing.JComponent;
|
42 |
|
|
import javax.swing.JFrame;
|
43 |
|
|
import javax.swing.JPanel;
|
44 |
|
|
import javax.swing.JScrollPane;
|
45 |
|
|
import javax.swing.JTextField;
|
46 |
|
|
import javax.swing.SwingUtilities;
|
47 |
|
|
import javax.swing.border.CompoundBorder;
|
48 |
|
|
import javax.swing.border.EmptyBorder;
|
49 |
|
|
import javax.swing.border.LineBorder;
|
50 |
|
|
import javax.swing.border.TitledBorder;
|
51 |
|
|
import javax.swing.text.BadLocationException;
|
52 |
|
|
import javax.swing.text.DefaultCaret;
|
53 |
|
|
import javax.swing.text.Highlighter;
|
54 |
|
|
import javax.swing.text.JTextComponent;
|
55 |
|
|
import javax.swing.text.View;
|
56 |
|
|
import javax.swing.text.LayeredHighlighter.LayerPainter;
|
57 |
|
|
|
58 |
|
|
/**
|
59 |
|
|
* A simple textfield demo showing various textfields in different states.
|
60 |
|
|
*/
|
61 |
|
|
public class TextFieldDemo
|
62 |
|
|
extends JPanel
|
63 |
|
|
implements ActionListener
|
64 |
|
|
{
|
65 |
|
|
|
66 |
|
|
/**
|
67 |
|
|
* A custom caret for demonstration purposes. This class is inspired by the
|
68 |
|
|
* CornerCaret from the OReilly Swing book.
|
69 |
|
|
*
|
70 |
|
|
* @author Roman Kennke (kennke@aicas.com)
|
71 |
|
|
*/
|
72 |
|
|
static class CornerCaret extends DefaultCaret
|
73 |
|
|
{
|
74 |
|
|
public CornerCaret()
|
75 |
|
|
{
|
76 |
|
|
super();
|
77 |
|
|
setBlinkRate(500);
|
78 |
|
|
}
|
79 |
|
|
|
80 |
|
|
protected synchronized void damage(Rectangle r)
|
81 |
|
|
{
|
82 |
|
|
if (r == null) return;
|
83 |
|
|
x = r.x;
|
84 |
|
|
y = r.y + (r.height * 4 / 5 - 3);
|
85 |
|
|
width = 5;
|
86 |
|
|
height = 5;
|
87 |
|
|
repaint();
|
88 |
|
|
}
|
89 |
|
|
|
90 |
|
|
public void paint(Graphics g)
|
91 |
|
|
{
|
92 |
|
|
JTextComponent comp = getComponent();
|
93 |
|
|
if (comp == null) return;
|
94 |
|
|
int dot = getDot();
|
95 |
|
|
Rectangle r = null;
|
96 |
|
|
try
|
97 |
|
|
{
|
98 |
|
|
r = comp.modelToView(dot);
|
99 |
|
|
}
|
100 |
|
|
catch (BadLocationException e)
|
101 |
|
|
{
|
102 |
|
|
return;
|
103 |
|
|
}
|
104 |
|
|
if (r == null) return;
|
105 |
|
|
int dist = r.height * 4 / 5 - 3;
|
106 |
|
|
if ((x != r.x) || (y != r.y + dist))
|
107 |
|
|
{
|
108 |
|
|
repaint();
|
109 |
|
|
x = r.x;
|
110 |
|
|
y = r.y + dist;
|
111 |
|
|
width = 5;
|
112 |
|
|
height = 5;
|
113 |
|
|
}
|
114 |
|
|
if (isVisible())
|
115 |
|
|
{
|
116 |
|
|
g.drawLine(r.x, r.y + dist, r.x, r.y + dist + 4);
|
117 |
|
|
g.drawLine(r.x, r.y + dist + 4, r.x + 4, r.y + dist + 4);
|
118 |
|
|
}
|
119 |
|
|
}
|
120 |
|
|
}
|
121 |
|
|
|
122 |
|
|
static class DemoHighlightPainter
|
123 |
|
|
extends LayerPainter
|
124 |
|
|
{
|
125 |
|
|
|
126 |
|
|
static DemoHighlightPainter INSTANCE = new DemoHighlightPainter();
|
127 |
|
|
|
128 |
|
|
|
129 |
|
|
static Color[] colors = { Color.BLUE, Color.CYAN, Color.GRAY, Color.GREEN,
|
130 |
|
|
Color.MAGENTA, Color.ORANGE, Color.PINK,
|
131 |
|
|
Color.ORANGE, Color.RED, Color.BLUE, Color.YELLOW };
|
132 |
|
|
|
133 |
|
|
|
134 |
|
|
public DemoHighlightPainter()
|
135 |
|
|
{
|
136 |
|
|
super();
|
137 |
|
|
}
|
138 |
|
|
|
139 |
|
|
private void paintHighlight(Graphics g, Rectangle rect)
|
140 |
|
|
{
|
141 |
|
|
g.fillRect(rect.x, rect.y, rect.width, rect.height);
|
142 |
|
|
}
|
143 |
|
|
|
144 |
|
|
public void paint(Graphics g, int p0, int p1, Shape bounds, JTextComponent t)
|
145 |
|
|
{
|
146 |
|
|
try
|
147 |
|
|
{
|
148 |
|
|
|
149 |
|
|
for (int i = p0; i < p1; i++)
|
150 |
|
|
{
|
151 |
|
|
Rectangle r = t.modelToView(i);
|
152 |
|
|
Point l1 = t.modelToView(i + 1).getLocation();
|
153 |
|
|
|
154 |
|
|
g.setColor(colors[(int) (Math.random() * colors.length)]);
|
155 |
|
|
g.fillOval(r.x, r.y, l1.x - r.x, r.height);
|
156 |
|
|
}
|
157 |
|
|
}
|
158 |
|
|
catch (BadLocationException ble)
|
159 |
|
|
{
|
160 |
|
|
}
|
161 |
|
|
}
|
162 |
|
|
|
163 |
|
|
public Shape paintLayer(Graphics g, int p0, int p1, Shape bounds,
|
164 |
|
|
JTextComponent c, View view)
|
165 |
|
|
{
|
166 |
|
|
paint(g, p0, p1, bounds, c);
|
167 |
|
|
|
168 |
|
|
return bounds;
|
169 |
|
|
}
|
170 |
|
|
|
171 |
|
|
}
|
172 |
|
|
|
173 |
|
|
/**
|
174 |
|
|
* The left aligned textfields and state buttons.
|
175 |
|
|
*/
|
176 |
|
|
Compound compound1;
|
177 |
|
|
|
178 |
|
|
/**
|
179 |
|
|
* The right aligned textfields and state buttons.
|
180 |
|
|
*/
|
181 |
|
|
Compound compound2;
|
182 |
|
|
|
183 |
|
|
/**
|
184 |
|
|
* The centered textfields and state buttons.
|
185 |
|
|
*/
|
186 |
|
|
Compound compound3;
|
187 |
|
|
|
188 |
|
|
/**
|
189 |
|
|
* The custom colored textfields and state buttons.
|
190 |
|
|
*/
|
191 |
|
|
Compound compound4;
|
192 |
|
|
Compound compound5;
|
193 |
|
|
|
194 |
|
|
/**
|
195 |
|
|
* Some miscellaneous textfield demos.
|
196 |
|
|
*/
|
197 |
|
|
Compound compound6;
|
198 |
|
|
|
199 |
|
|
/**
|
200 |
|
|
* Some textfields with custom borders.
|
201 |
|
|
*/
|
202 |
|
|
Compound compound7;
|
203 |
|
|
|
204 |
|
|
/**
|
205 |
|
|
* Creates a new demo instance.
|
206 |
|
|
*/
|
207 |
|
|
public TextFieldDemo()
|
208 |
|
|
{
|
209 |
|
|
super();
|
210 |
|
|
createContent();
|
211 |
|
|
}
|
212 |
|
|
|
213 |
|
|
/**
|
214 |
|
|
* When the demo is run independently, the frame is displayed, so we should
|
215 |
|
|
* initialise the content panel (including the demo content and a close
|
216 |
|
|
* button). But when the demo is run as part of the Swing activity board,
|
217 |
|
|
* only the demo content panel is used, the frame itself is never displayed,
|
218 |
|
|
* so we can avoid this step.
|
219 |
|
|
*/
|
220 |
|
|
void initFrameContent()
|
221 |
|
|
{
|
222 |
|
|
JPanel closePanel = new JPanel();
|
223 |
|
|
JButton closeButton = new JButton("Close");
|
224 |
|
|
closeButton.setActionCommand("CLOSE");
|
225 |
|
|
closeButton.addActionListener(this);
|
226 |
|
|
closePanel.add(closeButton);
|
227 |
|
|
add(closePanel, BorderLayout.SOUTH);
|
228 |
|
|
}
|
229 |
|
|
|
230 |
|
|
/**
|
231 |
|
|
* Returns a panel with the demo content. The panel
|
232 |
|
|
* uses a BorderLayout(), and the BorderLayout.SOUTH area
|
233 |
|
|
* is empty, to allow callers to add controls to the
|
234 |
|
|
* bottom of the panel if they want to (a close button is
|
235 |
|
|
* added if this demo is being run as a standalone demo).
|
236 |
|
|
*/
|
237 |
|
|
private void createContent()
|
238 |
|
|
{
|
239 |
|
|
setLayout(new BorderLayout());
|
240 |
|
|
JPanel panel = new JPanel(new GridLayout(7, 1));
|
241 |
|
|
panel.add(createLeftAlignedPanel());
|
242 |
|
|
panel.add(createRightAlignedPanel());
|
243 |
|
|
panel.add(createCenteredPanel());
|
244 |
|
|
panel.add(createCustomColorPanel1());
|
245 |
|
|
panel.add(createCustomColorPanel2());
|
246 |
|
|
panel.add(createCustomBordersPanel());
|
247 |
|
|
panel.add(createMiscPanel());
|
248 |
|
|
|
249 |
|
|
// Put everything in a scroll pane to make it neccessary
|
250 |
|
|
// to reach the bottom inner panels if the screen is to small.
|
251 |
|
|
add(new JScrollPane(panel));
|
252 |
|
|
}
|
253 |
|
|
|
254 |
|
|
private JPanel createLeftAlignedPanel()
|
255 |
|
|
{
|
256 |
|
|
compound1 = createTextFieldCompound("Left aligned", 1);
|
257 |
|
|
|
258 |
|
|
compound1.setupTextfields("Hello World!",
|
259 |
|
|
JTextField.LEFT,
|
260 |
|
|
new Font[] { new Font("Dialog", Font.PLAIN, 8),
|
261 |
|
|
new Font("Dialog", Font.ITALIC, 12),
|
262 |
|
|
new Font("Dialog", Font.BOLD, 14)
|
263 |
|
|
});
|
264 |
|
|
|
265 |
|
|
return compound1.panel;
|
266 |
|
|
}
|
267 |
|
|
|
268 |
|
|
private Compound createTextFieldCompound(String title, int actionCommandNo)
|
269 |
|
|
{
|
270 |
|
|
Compound compound = new Compound();
|
271 |
|
|
compound.panel = new JPanel(new BorderLayout());
|
272 |
|
|
compound.panel.setBorder(BorderFactory.createTitledBorder(title));
|
273 |
|
|
|
274 |
|
|
compound.textFieldPanel = new JPanel();
|
275 |
|
|
compound.textFieldPanel.setLayout(new BoxLayout(compound.textFieldPanel, BoxLayout.X_AXIS));
|
276 |
|
|
|
277 |
|
|
compound.panel.add(compound.textFieldPanel);
|
278 |
|
|
|
279 |
|
|
JPanel statePanel = new JPanel();
|
280 |
|
|
statePanel.setLayout(new BoxLayout(statePanel, BoxLayout.Y_AXIS));
|
281 |
|
|
statePanel.add(Box.createVerticalGlue());
|
282 |
|
|
compound.enabled = new JCheckBox("enabled");
|
283 |
|
|
compound.enabled.setSelected(true);
|
284 |
|
|
compound.enabled.addActionListener(this);
|
285 |
|
|
compound.enabled.setActionCommand("ENABLED" + actionCommandNo);
|
286 |
|
|
statePanel.add(compound.enabled);
|
287 |
|
|
compound.editable = new JCheckBox("editable");
|
288 |
|
|
compound.editable.setSelected(true);
|
289 |
|
|
compound.editable.addActionListener(this);
|
290 |
|
|
compound.editable.setActionCommand("EDITABLE" + actionCommandNo);
|
291 |
|
|
statePanel.add(compound.editable);
|
292 |
|
|
statePanel.add(Box.createVerticalGlue());
|
293 |
|
|
compound.panel.add(statePanel, BorderLayout.EAST);
|
294 |
|
|
|
295 |
|
|
return compound;
|
296 |
|
|
}
|
297 |
|
|
|
298 |
|
|
private JPanel createRightAlignedPanel()
|
299 |
|
|
{
|
300 |
|
|
compound2 = createTextFieldCompound("Right aligned", 2);
|
301 |
|
|
|
302 |
|
|
compound2.setupTextfields("Hello World!",
|
303 |
|
|
JTextField.RIGHT,
|
304 |
|
|
new Font[] { new Font("Dialog", Font.PLAIN, 8),
|
305 |
|
|
new Font("Dialog", Font.ITALIC, 12),
|
306 |
|
|
new Font("Dialog", Font.BOLD, 14)
|
307 |
|
|
});
|
308 |
|
|
|
309 |
|
|
return compound2.panel;
|
310 |
|
|
}
|
311 |
|
|
|
312 |
|
|
private JPanel createCenteredPanel()
|
313 |
|
|
{
|
314 |
|
|
compound3 = createTextFieldCompound("Centered", 3);
|
315 |
|
|
|
316 |
|
|
compound3.setupTextfields("Hello World!",
|
317 |
|
|
JTextField.CENTER,
|
318 |
|
|
new Font[] { new Font("Dialog", Font.PLAIN, 8),
|
319 |
|
|
new Font("Dialog", Font.ITALIC, 12),
|
320 |
|
|
new Font("Dialog", Font.BOLD, 14)
|
321 |
|
|
});
|
322 |
|
|
|
323 |
|
|
return compound3.panel;
|
324 |
|
|
}
|
325 |
|
|
|
326 |
|
|
private JPanel createCustomColorPanel1()
|
327 |
|
|
{
|
328 |
|
|
compound4 = createTextFieldCompound("Custom colors I", 4);
|
329 |
|
|
|
330 |
|
|
compound4.textfield1 = new JTextField("custom foreground");
|
331 |
|
|
compound4.textfield1.setForeground(Color.RED);
|
332 |
|
|
compound4.textFieldPanel.add(compound4.textfield1);
|
333 |
|
|
|
334 |
|
|
compound4.textfield2 = new JTextField("custom background");
|
335 |
|
|
compound4.textfield2.setBackground(Color.YELLOW);
|
336 |
|
|
compound4.textFieldPanel.add(compound4.textfield2);
|
337 |
|
|
|
338 |
|
|
compound4.textfield3 = new JTextField("custom foreground and background");
|
339 |
|
|
compound4.textfield3.setForeground(Color.RED);
|
340 |
|
|
compound4.textfield3.setBackground(Color.YELLOW);
|
341 |
|
|
compound4.textFieldPanel.add(compound4.textfield3);
|
342 |
|
|
|
343 |
|
|
return compound4.panel;
|
344 |
|
|
|
345 |
|
|
}
|
346 |
|
|
|
347 |
|
|
private JPanel createCustomColorPanel2()
|
348 |
|
|
{
|
349 |
|
|
compound5 = createTextFieldCompound("Custom colors II", 5);
|
350 |
|
|
|
351 |
|
|
compound5.textfield1 = new JTextField("custom disabled textcolor");
|
352 |
|
|
compound5.textfield1.setDisabledTextColor(Color.BLUE);
|
353 |
|
|
compound5.textFieldPanel.add(compound5.textfield1);
|
354 |
|
|
|
355 |
|
|
compound5.textfield2 = new JTextField("custom selected text color");
|
356 |
|
|
compound5.textfield2.setSelectedTextColor(Color.RED);
|
357 |
|
|
compound5.textFieldPanel.add(compound5.textfield2);
|
358 |
|
|
|
359 |
|
|
compound5.textfield3 = new JTextField("custom selection color");
|
360 |
|
|
compound5.textfield3.setSelectionColor(Color.BLACK);
|
361 |
|
|
compound5.textFieldPanel.add(compound5.textfield3);
|
362 |
|
|
|
363 |
|
|
return compound5.panel;
|
364 |
|
|
|
365 |
|
|
}
|
366 |
|
|
|
367 |
|
|
private JPanel createMiscPanel()
|
368 |
|
|
{
|
369 |
|
|
compound6 = createTextFieldCompound("Miscellaneous", 6);
|
370 |
|
|
|
371 |
|
|
compound6.textfield1 = new JTextField("Custom Caret");
|
372 |
|
|
compound6.textfield1.setCaret(new CornerCaret());
|
373 |
|
|
compound6.textFieldPanel.add(compound6.textfield1);
|
374 |
|
|
|
375 |
|
|
compound6.textfield2 = new JTextField("Custom Caret color");
|
376 |
|
|
compound6.textfield2.setForeground(Color.LIGHT_GRAY);
|
377 |
|
|
compound6.textfield2.setBackground(Color.BLACK);
|
378 |
|
|
compound6.textfield2.setSelectedTextColor(Color.BLACK);
|
379 |
|
|
compound6.textfield2.setCaretColor(Color.WHITE);
|
380 |
|
|
compound6.textfield2.setSelectionColor(Color.DARK_GRAY);
|
381 |
|
|
compound6.textFieldPanel.add(compound6.textfield2);
|
382 |
|
|
|
383 |
|
|
compound6.textfield3 = new JTextField("Custom highlighter");
|
384 |
|
|
compound6.textfield3.setCaret(new DefaultCaret()
|
385 |
|
|
{
|
386 |
|
|
public Highlighter.HighlightPainter getSelectionPainter()
|
387 |
|
|
{
|
388 |
|
|
return DemoHighlightPainter.INSTANCE;
|
389 |
|
|
}
|
390 |
|
|
});
|
391 |
|
|
compound6.textFieldPanel.add(compound6.textfield3);
|
392 |
|
|
|
393 |
|
|
return compound6.panel;
|
394 |
|
|
}
|
395 |
|
|
|
396 |
|
|
private JPanel createCustomBordersPanel()
|
397 |
|
|
{
|
398 |
|
|
compound7 = createTextFieldCompound("Custom borders", 7);
|
399 |
|
|
|
400 |
|
|
compound7.textfield1 = new JTextField("red 5 pixel lineborder");
|
401 |
|
|
compound7.textfield1.setBorder(new LineBorder(Color.RED, 5));
|
402 |
|
|
compound7.textFieldPanel.add(compound7.textfield1);
|
403 |
|
|
|
404 |
|
|
compound7.textfield2 = new JTextField("complex irregular border");
|
405 |
|
|
|
406 |
|
|
CompoundBorder innerCompound = new CompoundBorder(new EmptyBorder(5, 40, 15, 10), new LineBorder(Color.BLACK));
|
407 |
|
|
CompoundBorder outerCompound = new CompoundBorder(new LineBorder(Color.BLACK), innerCompound);
|
408 |
|
|
compound7.textfield2.setBorder(outerCompound);
|
409 |
|
|
compound7.textFieldPanel.add(compound7.textfield2);
|
410 |
|
|
|
411 |
|
|
compound7.textfield3 = new JTextField("a titled border", 10);
|
412 |
|
|
compound7.textfield3.setBorder(new TitledBorder(null, "Freak Out Border", TitledBorder.CENTER, TitledBorder.LEFT));
|
413 |
|
|
compound7.textFieldPanel.add(compound7.textfield3);
|
414 |
|
|
|
415 |
|
|
return compound7.panel;
|
416 |
|
|
}
|
417 |
|
|
|
418 |
|
|
public void actionPerformed(ActionEvent e)
|
419 |
|
|
{
|
420 |
|
|
if (e.getActionCommand().equals("CLOSE"))
|
421 |
|
|
{
|
422 |
|
|
System.exit(0);
|
423 |
|
|
}
|
424 |
|
|
else if (e.getActionCommand().equals("ENABLED1"))
|
425 |
|
|
{
|
426 |
|
|
boolean enabled = compound1.enabled.isSelected();
|
427 |
|
|
compound1.textfield1.setEnabled(enabled);
|
428 |
|
|
compound1.textfield2.setEnabled(enabled);
|
429 |
|
|
compound1.textfield3.setEnabled(enabled);
|
430 |
|
|
}
|
431 |
|
|
else if (e.getActionCommand().equals("EDITABLE1"))
|
432 |
|
|
{
|
433 |
|
|
boolean editable = compound1.editable.isSelected();
|
434 |
|
|
compound1.textfield1.setEditable(editable);
|
435 |
|
|
compound1.textfield2.setEditable(editable);
|
436 |
|
|
compound1.textfield3.setEditable(editable);
|
437 |
|
|
}
|
438 |
|
|
else if (e.getActionCommand().equals("ENABLED2"))
|
439 |
|
|
{
|
440 |
|
|
boolean enabled = compound2.enabled.isSelected();
|
441 |
|
|
compound2.textfield1.setEnabled(enabled);
|
442 |
|
|
compound2.textfield2.setEnabled(enabled);
|
443 |
|
|
compound2.textfield3.setEnabled(enabled);
|
444 |
|
|
}
|
445 |
|
|
else if (e.getActionCommand().equals("EDITABLE2"))
|
446 |
|
|
{
|
447 |
|
|
boolean editable = compound2.editable.isSelected();
|
448 |
|
|
compound2.textfield1.setEditable(editable);
|
449 |
|
|
compound2.textfield2.setEditable(editable);
|
450 |
|
|
compound2.textfield3.setEditable(editable);
|
451 |
|
|
}
|
452 |
|
|
else if (e.getActionCommand().equals("ENABLED3"))
|
453 |
|
|
{
|
454 |
|
|
boolean enabled = compound3.enabled.isSelected();
|
455 |
|
|
compound3.textfield1.setEnabled(enabled);
|
456 |
|
|
compound3.textfield2.setEnabled(enabled);
|
457 |
|
|
compound3.textfield3.setEnabled(enabled);
|
458 |
|
|
}
|
459 |
|
|
else if (e.getActionCommand().equals("EDITABLE3"))
|
460 |
|
|
{
|
461 |
|
|
boolean editable = compound3.editable.isSelected();
|
462 |
|
|
compound3.textfield1.setEditable(editable);
|
463 |
|
|
compound3.textfield2.setEditable(editable);
|
464 |
|
|
compound3.textfield3.setEditable(editable);
|
465 |
|
|
}
|
466 |
|
|
else if (e.getActionCommand().equals("ENABLED4"))
|
467 |
|
|
{
|
468 |
|
|
boolean enabled = compound4.enabled.isSelected();
|
469 |
|
|
compound4.textfield1.setEnabled(enabled);
|
470 |
|
|
compound4.textfield2.setEnabled(enabled);
|
471 |
|
|
compound4.textfield3.setEnabled(enabled);
|
472 |
|
|
}
|
473 |
|
|
else if (e.getActionCommand().equals("EDITABLE4"))
|
474 |
|
|
{
|
475 |
|
|
boolean editable = compound4.editable.isSelected();
|
476 |
|
|
compound4.textfield1.setEditable(editable);
|
477 |
|
|
compound4.textfield2.setEditable(editable);
|
478 |
|
|
compound4.textfield3.setEditable(editable);
|
479 |
|
|
}
|
480 |
|
|
else if (e.getActionCommand().equals("ENABLED5"))
|
481 |
|
|
{
|
482 |
|
|
boolean enabled = compound5.enabled.isSelected();
|
483 |
|
|
compound5.textfield1.setEnabled(enabled);
|
484 |
|
|
compound5.textfield2.setEnabled(enabled);
|
485 |
|
|
compound5.textfield3.setEnabled(enabled);
|
486 |
|
|
}
|
487 |
|
|
else if (e.getActionCommand().equals("EDITABLE5"))
|
488 |
|
|
{
|
489 |
|
|
boolean editable = compound5.editable.isSelected();
|
490 |
|
|
compound5.textfield1.setEditable(editable);
|
491 |
|
|
compound5.textfield2.setEditable(editable);
|
492 |
|
|
compound5.textfield3.setEditable(editable);
|
493 |
|
|
}
|
494 |
|
|
else if (e.getActionCommand().equals("ENABLED6"))
|
495 |
|
|
{
|
496 |
|
|
boolean enabled = compound6.enabled.isSelected();
|
497 |
|
|
compound6.textfield1.setEnabled(enabled);
|
498 |
|
|
compound6.textfield2.setEnabled(enabled);
|
499 |
|
|
compound6.textfield3.setEnabled(enabled);
|
500 |
|
|
}
|
501 |
|
|
else if (e.getActionCommand().equals("EDITABLE6"))
|
502 |
|
|
{
|
503 |
|
|
boolean editable = compound6.editable.isSelected();
|
504 |
|
|
compound6.textfield1.setEditable(editable);
|
505 |
|
|
compound6.textfield2.setEditable(editable);
|
506 |
|
|
compound6.textfield3.setEditable(editable);
|
507 |
|
|
}
|
508 |
|
|
else if (e.getActionCommand().equals("ENABLED7"))
|
509 |
|
|
{
|
510 |
|
|
boolean enabled = compound7.enabled.isSelected();
|
511 |
|
|
compound7.textfield1.setEnabled(enabled);
|
512 |
|
|
compound7.textfield2.setEnabled(enabled);
|
513 |
|
|
compound7.textfield3.setEnabled(enabled);
|
514 |
|
|
}
|
515 |
|
|
else if (e.getActionCommand().equals("EDITABLE7"))
|
516 |
|
|
{
|
517 |
|
|
boolean editable = compound7.editable.isSelected();
|
518 |
|
|
compound7.textfield1.setEditable(editable);
|
519 |
|
|
compound7.textfield2.setEditable(editable);
|
520 |
|
|
compound7.textfield3.setEditable(editable);
|
521 |
|
|
}
|
522 |
|
|
}
|
523 |
|
|
|
524 |
|
|
public static void main(String[] args)
|
525 |
|
|
{
|
526 |
|
|
SwingUtilities.invokeLater
|
527 |
|
|
(new Runnable()
|
528 |
|
|
{
|
529 |
|
|
public void run()
|
530 |
|
|
{
|
531 |
|
|
TextFieldDemo app = new TextFieldDemo();
|
532 |
|
|
app.initFrameContent();
|
533 |
|
|
JFrame frame = new JFrame("TextField demo");
|
534 |
|
|
frame.getContentPane().add(app);
|
535 |
|
|
frame.pack();
|
536 |
|
|
frame.setVisible(true);
|
537 |
|
|
}
|
538 |
|
|
});
|
539 |
|
|
}
|
540 |
|
|
|
541 |
|
|
/**
|
542 |
|
|
* Returns a DemoFactory that creates a TextFieldDemo.
|
543 |
|
|
*
|
544 |
|
|
* @return a DemoFactory that creates a TextFieldDemo
|
545 |
|
|
*/
|
546 |
|
|
public static DemoFactory createDemoFactory()
|
547 |
|
|
{
|
548 |
|
|
return new DemoFactory()
|
549 |
|
|
{
|
550 |
|
|
public JComponent createDemo()
|
551 |
|
|
{
|
552 |
|
|
return new TextFieldDemo();
|
553 |
|
|
}
|
554 |
|
|
};
|
555 |
|
|
}
|
556 |
|
|
|
557 |
|
|
static class Compound
|
558 |
|
|
{
|
559 |
|
|
JTextField textfield1;
|
560 |
|
|
JTextField textfield2;
|
561 |
|
|
JTextField textfield3;
|
562 |
|
|
JCheckBox enabled;
|
563 |
|
|
JCheckBox editable;
|
564 |
|
|
JPanel textFieldPanel;
|
565 |
|
|
JPanel panel;
|
566 |
|
|
|
567 |
|
|
/** Creates and initializes the textfields with the same text and
|
568 |
|
|
* alignment but with a different font.
|
569 |
|
|
*
|
570 |
|
|
* @param title The text for the textfields.
|
571 |
|
|
* @param align The alignment for the textfields.
|
572 |
|
|
* @param fonts The fonts to be used for the textfields.
|
573 |
|
|
*/
|
574 |
|
|
void setupTextfields(String title, int align, Font[] fonts)
|
575 |
|
|
{
|
576 |
|
|
textfield1 = new JTextField(title);
|
577 |
|
|
textfield1.setHorizontalAlignment(align);
|
578 |
|
|
textfield1.setFont(fonts[0]);
|
579 |
|
|
textFieldPanel.add(textfield1);
|
580 |
|
|
|
581 |
|
|
textfield2 = new JTextField(title);
|
582 |
|
|
textfield2.setHorizontalAlignment(align);
|
583 |
|
|
textfield2.setFont(fonts[1]);
|
584 |
|
|
textFieldPanel.add(textfield2);
|
585 |
|
|
|
586 |
|
|
textfield3 = new JTextField(title);
|
587 |
|
|
textfield3.setHorizontalAlignment(align);
|
588 |
|
|
textfield3.setFont(fonts[2]);
|
589 |
|
|
textFieldPanel.add(textfield3);
|
590 |
|
|
}
|
591 |
|
|
|
592 |
|
|
}
|
593 |
|
|
|
594 |
|
|
}
|