1 /* TextFieldDemo.java -- An example showing various textfields in Swing.
2 Copyright (C) 2005, 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath examples.
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)
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.
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
23 package gnu
.classpath
.examples
.swing
;
25 import java
.awt
.BorderLayout
;
26 import java
.awt
.Color
;
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
;
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
;
59 * A simple textfield demo showing various textfields in different states.
61 public class TextFieldDemo
63 implements ActionListener
67 * A custom caret for demonstration purposes. This class is inspired by the
68 * CornerCaret from the OReilly Swing book.
70 * @author Roman Kennke (kennke@aicas.com)
72 static class CornerCaret
extends DefaultCaret
80 protected synchronized void damage(Rectangle r
)
82 if (r
== null) return;
84 y
= r
.y
+ (r
.height
* 4 / 5 - 3);
90 public void paint(Graphics g
)
92 JTextComponent comp
= getComponent();
93 if (comp
== null) return;
98 r
= comp
.modelToView(dot
);
100 catch (BadLocationException e
)
104 if (r
== null) return;
105 int dist
= r
.height
* 4 / 5 - 3;
106 if ((x
!= r
.x
) || (y
!= r
.y
+ dist
))
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);
122 static class DemoHighlightPainter
126 static DemoHighlightPainter INSTANCE
= new DemoHighlightPainter();
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
};
134 public DemoHighlightPainter()
139 private void paintHighlight(Graphics g
, Rectangle rect
)
141 g
.fillRect(rect
.x
, rect
.y
, rect
.width
, rect
.height
);
144 public void paint(Graphics g
, int p0
, int p1
, Shape bounds
, JTextComponent t
)
149 for (int i
= p0
; i
< p1
; i
++)
151 Rectangle r
= t
.modelToView(i
);
152 Point l1
= t
.modelToView(i
+ 1).getLocation();
154 g
.setColor(colors
[(int) (Math
.random() * colors
.length
)]);
155 g
.fillOval(r
.x
, r
.y
, l1
.x
- r
.x
, r
.height
);
158 catch (BadLocationException ble
)
163 public Shape
paintLayer(Graphics g
, int p0
, int p1
, Shape bounds
,
164 JTextComponent c
, View view
)
166 paint(g
, p0
, p1
, bounds
, c
);
174 * The left aligned textfields and state buttons.
179 * The right aligned textfields and state buttons.
184 * The centered textfields and state buttons.
189 * The custom colored textfields and state buttons.
195 * Some miscellaneous textfield demos.
200 * Some textfields with custom borders.
205 * Creates a new demo instance.
207 public TextFieldDemo()
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.
220 void initFrameContent()
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
);
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).
237 private void createContent()
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());
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
));
254 private JPanel
createLeftAlignedPanel()
256 compound1
= createTextFieldCompound("Left aligned", 1);
258 compound1
.setupTextfields("Hello World!",
260 new Font
[] { new Font("Dialog", Font
.PLAIN
, 8),
261 new Font("Dialog", Font
.ITALIC
, 12),
262 new Font("Dialog", Font
.BOLD
, 14)
265 return compound1
.panel
;
268 private Compound
createTextFieldCompound(String title
, int actionCommandNo
)
270 Compound compound
= new Compound();
271 compound
.panel
= new JPanel(new BorderLayout());
272 compound
.panel
.setBorder(BorderFactory
.createTitledBorder(title
));
274 compound
.textFieldPanel
= new JPanel();
275 compound
.textFieldPanel
.setLayout(new BoxLayout(compound
.textFieldPanel
, BoxLayout
.X_AXIS
));
277 compound
.panel
.add(compound
.textFieldPanel
);
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
);
298 private JPanel
createRightAlignedPanel()
300 compound2
= createTextFieldCompound("Right aligned", 2);
302 compound2
.setupTextfields("Hello World!",
304 new Font
[] { new Font("Dialog", Font
.PLAIN
, 8),
305 new Font("Dialog", Font
.ITALIC
, 12),
306 new Font("Dialog", Font
.BOLD
, 14)
309 return compound2
.panel
;
312 private JPanel
createCenteredPanel()
314 compound3
= createTextFieldCompound("Centered", 3);
316 compound3
.setupTextfields("Hello World!",
318 new Font
[] { new Font("Dialog", Font
.PLAIN
, 8),
319 new Font("Dialog", Font
.ITALIC
, 12),
320 new Font("Dialog", Font
.BOLD
, 14)
323 return compound3
.panel
;
326 private JPanel
createCustomColorPanel1()
328 compound4
= createTextFieldCompound("Custom colors I", 4);
330 compound4
.textfield1
= new JTextField("custom foreground");
331 compound4
.textfield1
.setForeground(Color
.RED
);
332 compound4
.textFieldPanel
.add(compound4
.textfield1
);
334 compound4
.textfield2
= new JTextField("custom background");
335 compound4
.textfield2
.setBackground(Color
.YELLOW
);
336 compound4
.textFieldPanel
.add(compound4
.textfield2
);
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
);
343 return compound4
.panel
;
347 private JPanel
createCustomColorPanel2()
349 compound5
= createTextFieldCompound("Custom colors II", 5);
351 compound5
.textfield1
= new JTextField("custom disabled textcolor");
352 compound5
.textfield1
.setDisabledTextColor(Color
.BLUE
);
353 compound5
.textFieldPanel
.add(compound5
.textfield1
);
355 compound5
.textfield2
= new JTextField("custom selected text color");
356 compound5
.textfield2
.setSelectedTextColor(Color
.RED
);
357 compound5
.textFieldPanel
.add(compound5
.textfield2
);
359 compound5
.textfield3
= new JTextField("custom selection color");
360 compound5
.textfield3
.setSelectionColor(Color
.BLACK
);
361 compound5
.textFieldPanel
.add(compound5
.textfield3
);
363 return compound5
.panel
;
367 private JPanel
createMiscPanel()
369 compound6
= createTextFieldCompound("Miscellaneous", 6);
371 compound6
.textfield1
= new JTextField("Custom Caret");
372 compound6
.textfield1
.setCaret(new CornerCaret());
373 compound6
.textFieldPanel
.add(compound6
.textfield1
);
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
);
383 compound6
.textfield3
= new JTextField("Custom highlighter");
384 compound6
.textfield3
.setCaret(new DefaultCaret()
386 public Highlighter
.HighlightPainter
getSelectionPainter()
388 return DemoHighlightPainter
.INSTANCE
;
391 compound6
.textFieldPanel
.add(compound6
.textfield3
);
393 return compound6
.panel
;
396 private JPanel
createCustomBordersPanel()
398 compound7
= createTextFieldCompound("Custom borders", 7);
400 compound7
.textfield1
= new JTextField("red 5 pixel lineborder");
401 compound7
.textfield1
.setBorder(new LineBorder(Color
.RED
, 5));
402 compound7
.textFieldPanel
.add(compound7
.textfield1
);
404 compound7
.textfield2
= new JTextField("complex irregular border");
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
);
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
);
415 return compound7
.panel
;
418 public void actionPerformed(ActionEvent e
)
420 if (e
.getActionCommand().equals("CLOSE"))
424 else if (e
.getActionCommand().equals("ENABLED1"))
426 boolean enabled
= compound1
.enabled
.isSelected();
427 compound1
.textfield1
.setEnabled(enabled
);
428 compound1
.textfield2
.setEnabled(enabled
);
429 compound1
.textfield3
.setEnabled(enabled
);
431 else if (e
.getActionCommand().equals("EDITABLE1"))
433 boolean editable
= compound1
.editable
.isSelected();
434 compound1
.textfield1
.setEditable(editable
);
435 compound1
.textfield2
.setEditable(editable
);
436 compound1
.textfield3
.setEditable(editable
);
438 else if (e
.getActionCommand().equals("ENABLED2"))
440 boolean enabled
= compound2
.enabled
.isSelected();
441 compound2
.textfield1
.setEnabled(enabled
);
442 compound2
.textfield2
.setEnabled(enabled
);
443 compound2
.textfield3
.setEnabled(enabled
);
445 else if (e
.getActionCommand().equals("EDITABLE2"))
447 boolean editable
= compound2
.editable
.isSelected();
448 compound2
.textfield1
.setEditable(editable
);
449 compound2
.textfield2
.setEditable(editable
);
450 compound2
.textfield3
.setEditable(editable
);
452 else if (e
.getActionCommand().equals("ENABLED3"))
454 boolean enabled
= compound3
.enabled
.isSelected();
455 compound3
.textfield1
.setEnabled(enabled
);
456 compound3
.textfield2
.setEnabled(enabled
);
457 compound3
.textfield3
.setEnabled(enabled
);
459 else if (e
.getActionCommand().equals("EDITABLE3"))
461 boolean editable
= compound3
.editable
.isSelected();
462 compound3
.textfield1
.setEditable(editable
);
463 compound3
.textfield2
.setEditable(editable
);
464 compound3
.textfield3
.setEditable(editable
);
466 else if (e
.getActionCommand().equals("ENABLED4"))
468 boolean enabled
= compound4
.enabled
.isSelected();
469 compound4
.textfield1
.setEnabled(enabled
);
470 compound4
.textfield2
.setEnabled(enabled
);
471 compound4
.textfield3
.setEnabled(enabled
);
473 else if (e
.getActionCommand().equals("EDITABLE4"))
475 boolean editable
= compound4
.editable
.isSelected();
476 compound4
.textfield1
.setEditable(editable
);
477 compound4
.textfield2
.setEditable(editable
);
478 compound4
.textfield3
.setEditable(editable
);
480 else if (e
.getActionCommand().equals("ENABLED5"))
482 boolean enabled
= compound5
.enabled
.isSelected();
483 compound5
.textfield1
.setEnabled(enabled
);
484 compound5
.textfield2
.setEnabled(enabled
);
485 compound5
.textfield3
.setEnabled(enabled
);
487 else if (e
.getActionCommand().equals("EDITABLE5"))
489 boolean editable
= compound5
.editable
.isSelected();
490 compound5
.textfield1
.setEditable(editable
);
491 compound5
.textfield2
.setEditable(editable
);
492 compound5
.textfield3
.setEditable(editable
);
494 else if (e
.getActionCommand().equals("ENABLED6"))
496 boolean enabled
= compound6
.enabled
.isSelected();
497 compound6
.textfield1
.setEnabled(enabled
);
498 compound6
.textfield2
.setEnabled(enabled
);
499 compound6
.textfield3
.setEnabled(enabled
);
501 else if (e
.getActionCommand().equals("EDITABLE6"))
503 boolean editable
= compound6
.editable
.isSelected();
504 compound6
.textfield1
.setEditable(editable
);
505 compound6
.textfield2
.setEditable(editable
);
506 compound6
.textfield3
.setEditable(editable
);
508 else if (e
.getActionCommand().equals("ENABLED7"))
510 boolean enabled
= compound7
.enabled
.isSelected();
511 compound7
.textfield1
.setEnabled(enabled
);
512 compound7
.textfield2
.setEnabled(enabled
);
513 compound7
.textfield3
.setEnabled(enabled
);
515 else if (e
.getActionCommand().equals("EDITABLE7"))
517 boolean editable
= compound7
.editable
.isSelected();
518 compound7
.textfield1
.setEditable(editable
);
519 compound7
.textfield2
.setEditable(editable
);
520 compound7
.textfield3
.setEditable(editable
);
524 public static void main(String
[] args
)
526 SwingUtilities
.invokeLater
531 TextFieldDemo app
= new TextFieldDemo();
532 app
.initFrameContent();
533 JFrame frame
= new JFrame("TextField demo");
534 frame
.getContentPane().add(app
);
536 frame
.setVisible(true);
542 * Returns a DemoFactory that creates a TextFieldDemo.
544 * @return a DemoFactory that creates a TextFieldDemo
546 public static DemoFactory
createDemoFactory()
548 return new DemoFactory()
550 public JComponent
createDemo()
552 return new TextFieldDemo();
557 static class Compound
559 JTextField textfield1
;
560 JTextField textfield2
;
561 JTextField textfield3
;
564 JPanel textFieldPanel
;
567 /** Creates and initializes the textfields with the same text and
568 * alignment but with a different font.
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.
574 void setupTextfields(String title
, int align
, Font
[] fonts
)
576 textfield1
= new JTextField(title
);
577 textfield1
.setHorizontalAlignment(align
);
578 textfield1
.setFont(fonts
[0]);
579 textFieldPanel
.add(textfield1
);
581 textfield2
= new JTextField(title
);
582 textfield2
.setHorizontalAlignment(align
);
583 textfield2
.setFont(fonts
[1]);
584 textFieldPanel
.add(textfield2
);
586 textfield3
= new JTextField(title
);
587 textfield3
.setHorizontalAlignment(align
);
588 textfield3
.setFont(fonts
[2]);
589 textFieldPanel
.add(textfield3
);