Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / examples / gnu / classpath / examples / swing / TextFieldDemo.java
blob5ddf116806154130600d00433258075b093afcb7
1 /* TextFieldDemo.java -- An example showing various textfields in Swing.
2 Copyright (C) 2005, 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)
9 any later version.
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
19 02110-1301 USA.
23 package gnu.classpath.examples.swing;
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.Rectangle;
31 import java.awt.event.ActionEvent;
32 import java.awt.event.ActionListener;
34 import javax.swing.BorderFactory;
35 import javax.swing.Box;
36 import javax.swing.BoxLayout;
37 import javax.swing.JButton;
38 import javax.swing.JCheckBox;
39 import javax.swing.JFrame;
40 import javax.swing.JPanel;
41 import javax.swing.JTextField;
42 import javax.swing.text.BadLocationException;
43 import javax.swing.text.DefaultCaret;
44 import javax.swing.text.JTextComponent;
46 /**
47 * A simple textfield demo showing various textfields in different states.
49 public class TextFieldDemo
50 extends JFrame
51 implements ActionListener
54 /**
55 * A custom caret for demonstration purposes. This class is inspired by the
56 * CornerCaret from the OReilly Swing book.
58 * @author Roman Kennke (kennke@aicas.com)
60 static class CornerCaret extends DefaultCaret
62 public CornerCaret()
64 super();
65 setBlinkRate(500);
68 protected synchronized void damage(Rectangle r)
70 if (r == null) return;
71 x = r.x;
72 y = r.y + (r.height * 4 / 5 - 3);
73 width = 5;
74 height = 5;
75 repaint();
78 public void paint(Graphics g)
80 JTextComponent comp = getComponent();
81 if (comp == null) return;
82 int dot = getDot();
83 Rectangle r = null;
84 try
86 r = comp.modelToView(dot);
88 catch (BadLocationException e)
90 return;
92 if (r == null) return;
93 int dist = r.height * 4 / 5 - 3;
94 if ((x != r.x) || (y != r.y + dist))
96 repaint();
97 x = r.x;
98 y = r.y + dist;
99 width = 5;
100 height = 5;
102 if (isVisible())
104 g.drawLine(r.x, r.y + dist, r.x, r.y + dist + 4);
105 g.drawLine(r.x, r.y + dist + 4, r.x + 4, r.y + dist + 4);
111 * The left aligned textfields and state buttons.
113 JTextField textfield1;
114 JTextField textfield2;
115 JTextField textfield3;
116 JCheckBox enabled1;
117 JCheckBox editable1;
118 JPanel textFieldPanel1;
120 * The right aligned textfields and state buttons.
122 JTextField textfield4;
123 JTextField textfield5;
124 JTextField textfield6;
125 JCheckBox enabled2;
126 JCheckBox editable2;
129 * The centered textfields and state buttons.
131 JTextField textfield7;
132 JTextField textfield8;
133 JTextField textfield9;
134 JCheckBox enabled3;
135 JCheckBox editable3;
138 * The custom colored textfields and state buttons.
140 JTextField textfield10;
141 JTextField textfield11;
142 JTextField textfield12;
143 JTextField textfield13;
144 JTextField textfield14;
145 JCheckBox enabled4;
146 JCheckBox editable4;
149 * Some miscallenous textfield demos.
151 JTextField textfield15;
152 JTextField textfield16;
153 JCheckBox enabled5;
154 JCheckBox editable5;
157 * Creates a new demo instance.
159 * @param title the frame title.
161 public TextFieldDemo(String title)
163 super(title);
164 JPanel content = createContent();
165 JPanel closePanel = new JPanel();
166 JButton closeButton = new JButton("Close");
167 closeButton.setActionCommand("CLOSE");
168 closeButton.addActionListener(this);
169 closePanel.add(closeButton);
170 content.add(closePanel, BorderLayout.SOUTH);
171 getContentPane().add(content);
175 * Returns a panel with the demo content. The panel
176 * uses a BorderLayout(), and the BorderLayout.SOUTH area
177 * is empty, to allow callers to add controls to the
178 * bottom of the panel if they want to (a close button is
179 * added if this demo is being run as a standalone demo).
181 JPanel createContent()
183 JPanel content = new JPanel(new BorderLayout());
184 JPanel panel = new JPanel(new GridLayout(5, 1));
185 panel.add(createLeftAlignedPanel());
186 panel.add(createRightAlignedPanel());
187 panel.add(createCenteredPanel());
188 panel.add(createCustomColoredPanel());
189 panel.add(createMiscPanel());
190 content.add(panel);
191 //content.setPreferredSize(new Dimension(400, 300));
192 return content;
195 private JPanel createLeftAlignedPanel()
197 JPanel panel = new JPanel(new BorderLayout());
198 panel.setBorder(BorderFactory.createTitledBorder("Left aligned"));
200 textFieldPanel1 = new JPanel();
201 textFieldPanel1.setLayout(new BoxLayout(textFieldPanel1, BoxLayout.X_AXIS));
203 textfield1 = new JTextField("Hello World!");
204 textfield1.setHorizontalAlignment(JTextField.LEFT);
205 textfield1.setFont(new Font("Dialog", Font.PLAIN, 8));
206 textFieldPanel1.add(textfield1);
208 textfield2 = new JTextField("Hello World!");
209 textfield2.setHorizontalAlignment(JTextField.LEFT);
210 textfield2.setFont(new Font("Dialog", Font.ITALIC, 12));
211 textFieldPanel1.add(textfield2);
213 textfield3 = new JTextField("Hello World!");
214 textfield3.setHorizontalAlignment(JTextField.LEFT);
215 textfield3.setFont(new Font("Dialog", Font.BOLD, 14));
216 textFieldPanel1.add(textfield3);
218 panel.add(textFieldPanel1);
220 JPanel statePanel = new JPanel();
221 statePanel.setLayout(new BoxLayout(statePanel, BoxLayout.Y_AXIS));
222 statePanel.add(Box.createVerticalGlue());
223 enabled1 = new JCheckBox("enabled");
224 enabled1.setSelected(true);
225 enabled1.addActionListener(this);
226 enabled1.setActionCommand("ENABLED1");
227 statePanel.add(enabled1);
228 editable1 = new JCheckBox("editable");
229 editable1.setSelected(true);
230 editable1.addActionListener(this);
231 editable1.setActionCommand("EDITABLE1");
232 statePanel.add(editable1);
233 statePanel.add(Box.createVerticalGlue());
234 panel.add(statePanel, BorderLayout.EAST);
236 return panel;
239 private JPanel createRightAlignedPanel()
241 JPanel panel = new JPanel(new BorderLayout());
242 panel.setBorder(BorderFactory.createTitledBorder("Right aligned"));
244 JPanel textFieldPanel = new JPanel();
245 textFieldPanel.setLayout(new BoxLayout(textFieldPanel, BoxLayout.X_AXIS));
247 textfield4 = new JTextField("Hello World!");
248 textfield4.setHorizontalAlignment(JTextField.RIGHT);
249 textfield4.setFont(new Font("Dialog", Font.PLAIN, 8));
250 textFieldPanel.add(textfield4);
252 textfield5 = new JTextField("Hello World!");
253 textfield5.setHorizontalAlignment(JTextField.RIGHT);
254 textfield5.setFont(new Font("Dialog", Font.ITALIC, 12));
255 textFieldPanel.add(textfield5);
257 textfield6 = new JTextField("Hello World!");
258 textfield6.setHorizontalAlignment(JTextField.RIGHT);
259 textfield6.setFont(new Font("Dialog", Font.BOLD, 14));
260 textFieldPanel.add(textfield6);
262 panel.add(textFieldPanel);
264 JPanel statePanel = new JPanel();
265 statePanel.setLayout(new BoxLayout(statePanel, BoxLayout.Y_AXIS));
266 statePanel.add(Box.createVerticalGlue());
267 enabled2 = new JCheckBox("enabled");
268 enabled2.setSelected(true);
269 enabled2.addActionListener(this);
270 enabled2.setActionCommand("ENABLED2");
271 statePanel.add(enabled2);
272 editable2 = new JCheckBox("editable");
273 editable2.setSelected(true);
274 editable2.addActionListener(this);
275 editable2.setActionCommand("EDITABLE2");
276 statePanel.add(editable2);
277 statePanel.add(Box.createVerticalGlue());
278 panel.add(statePanel, BorderLayout.EAST);
280 return panel;
283 private JPanel createCenteredPanel()
285 JPanel panel = new JPanel(new BorderLayout());
286 panel.setBorder(BorderFactory.createTitledBorder("Centered"));
288 JPanel textFieldPanel = new JPanel();
289 textFieldPanel.setLayout(new BoxLayout(textFieldPanel, BoxLayout.X_AXIS));
291 textfield7 = new JTextField("Hello World!");
292 textfield7.setHorizontalAlignment(JTextField.CENTER);
293 textfield7.setFont(new Font("Dialog", Font.PLAIN, 8));
294 textFieldPanel.add(textfield7);
296 textfield8 = new JTextField("Hello World!");
297 textfield8.setHorizontalAlignment(JTextField.CENTER);
298 textfield8.setFont(new Font("Dialog", Font.ITALIC, 12));
299 textFieldPanel.add(textfield8);
301 textfield9 = new JTextField("Hello World!");
302 textfield9.setHorizontalAlignment(JTextField.CENTER);
303 textfield9.setFont(new Font("Dialog", Font.BOLD, 14));
304 textFieldPanel.add(textfield9);
306 panel.add(textFieldPanel);
308 JPanel statePanel = new JPanel();
309 statePanel.setLayout(new BoxLayout(statePanel, BoxLayout.Y_AXIS));
310 statePanel.add(Box.createVerticalGlue());
311 enabled3 = new JCheckBox("enabled");
312 enabled3.setSelected(true);
313 enabled3.addActionListener(this);
314 enabled3.setActionCommand("ENABLED3");
315 statePanel.add(enabled3);
316 editable3 = new JCheckBox("editable");
317 editable3.setSelected(true);
318 editable3.addActionListener(this);
319 editable3.setActionCommand("EDITABLE3");
320 statePanel.add(editable3);
321 statePanel.add(Box.createVerticalGlue());
322 panel.add(statePanel, BorderLayout.EAST);
324 return panel;
327 private JPanel createCustomColoredPanel()
329 JPanel panel = new JPanel(new BorderLayout());
331 JPanel textFieldPanel = new JPanel();
332 panel.setBorder(BorderFactory.createTitledBorder("Custom colors"));
333 textFieldPanel.setLayout(new BoxLayout(textFieldPanel, BoxLayout.X_AXIS));
335 textfield10 = new JTextField("custom foreground");
336 textfield10.setForeground(Color.GREEN);
337 textFieldPanel.add(textfield10);
339 textfield11 = new JTextField("custom background");
340 textfield11.setForeground(Color.YELLOW);
341 textFieldPanel.add(textfield11);
343 textfield12 = new JTextField("custom disabled textcolor");
344 textfield12.setDisabledTextColor(Color.BLUE);
345 textFieldPanel.add(textfield12);
347 textfield13 = new JTextField("custom selected text color");
348 textfield13.setSelectedTextColor(Color.RED);
349 textFieldPanel.add(textfield13);
351 textfield14 = new JTextField("custom selection color");
352 textfield14.setSelectionColor(Color.CYAN);
353 textFieldPanel.add(textfield14);
355 panel.add(textFieldPanel);
357 JPanel statePanel = new JPanel();
358 statePanel.setLayout(new BoxLayout(statePanel, BoxLayout.Y_AXIS));
359 statePanel.add(Box.createVerticalGlue());
360 enabled4 = new JCheckBox("enabled");
361 enabled4.setSelected(true);
362 enabled4.addActionListener(this);
363 enabled4.setActionCommand("ENABLED4");
364 statePanel.add(enabled4);
365 editable4 = new JCheckBox("editable");
366 editable4.setSelected(true);
367 editable4.addActionListener(this);
368 editable4.setActionCommand("EDITABLE4");
369 statePanel.add(editable4);
370 statePanel.add(Box.createVerticalGlue());
371 panel.add(statePanel, BorderLayout.EAST);
373 return panel;
376 private JPanel createMiscPanel()
378 JPanel panel = new JPanel(new BorderLayout());
379 panel.setBorder(BorderFactory.createTitledBorder("Miscallenous"));
381 JPanel textFieldPanel = new JPanel();
382 textFieldPanel.setLayout(new BoxLayout(textFieldPanel, BoxLayout.X_AXIS));
384 textfield15 = new JTextField("Custom Caret");
385 textfield15.setCaret(new CornerCaret());
386 textFieldPanel.add(textfield15);
388 textfield16 = new JTextField("Custom Caret color");
389 textfield16.setCaretColor(Color.MAGENTA);
390 textFieldPanel.add(textfield16);
392 panel.add(textFieldPanel);
394 JPanel statePanel = new JPanel();
395 statePanel.setLayout(new BoxLayout(statePanel, BoxLayout.Y_AXIS));
396 statePanel.add(Box.createVerticalGlue());
397 enabled5 = new JCheckBox("enabled");
398 enabled5.setSelected(true);
399 enabled5.addActionListener(this);
400 enabled5.setActionCommand("ENABLED5");
401 statePanel.add(enabled5);
402 editable5 = new JCheckBox("editable");
403 editable5.setSelected(true);
404 editable5.addActionListener(this);
405 editable5.setActionCommand("EDITABLE5");
406 statePanel.add(editable5);
407 statePanel.add(Box.createVerticalGlue());
408 panel.add(statePanel, BorderLayout.EAST);
410 return panel;
413 public void actionPerformed(ActionEvent e)
415 if (e.getActionCommand().equals("CLOSE"))
417 System.exit(0);
419 else if (e.getActionCommand().equals("ENABLED1"))
421 boolean enabled = enabled1.isSelected();
422 textfield1.setEnabled(enabled);
423 textfield2.setEnabled(enabled);
424 textfield3.setEnabled(enabled);
426 else if (e.getActionCommand().equals("EDITABLE1"))
428 boolean editable = editable1.isSelected();
429 textfield1.setEditable(editable);
430 textfield2.setEditable(editable);
431 textfield3.setEditable(editable);
433 else if (e.getActionCommand().equals("ENABLED2"))
435 boolean enabled = enabled2.isSelected();
436 textfield4.setEnabled(enabled);
437 textfield5.setEnabled(enabled);
438 textfield6.setEnabled(enabled);
440 else if (e.getActionCommand().equals("EDITABLE2"))
442 boolean editable = editable2.isSelected();
443 textfield4.setEditable(editable);
444 textfield5.setEditable(editable);
445 textfield6.setEditable(editable);
447 else if (e.getActionCommand().equals("ENABLED3"))
449 boolean enabled = enabled3.isSelected();
450 textfield7.setEnabled(enabled);
451 textfield8.setEnabled(enabled);
452 textfield9.setEnabled(enabled);
454 else if (e.getActionCommand().equals("EDITABLE3"))
456 boolean editable = editable3.isSelected();
457 textfield7.setEditable(editable);
458 textfield8.setEditable(editable);
459 textfield9.setEditable(editable);
461 else if (e.getActionCommand().equals("ENABLED4"))
463 boolean enabled = enabled4.isSelected();
464 textfield10.setEnabled(enabled);
465 textfield11.setEnabled(enabled);
466 textfield12.setEnabled(enabled);
467 textfield13.setEnabled(enabled);
468 textfield14.setEnabled(enabled);
470 else if (e.getActionCommand().equals("EDITABLE4"))
472 boolean editable = editable4.isSelected();
473 textfield10.setEditable(editable);
474 textfield11.setEditable(editable);
475 textfield12.setEditable(editable);
476 textfield13.setEditable(editable);
477 textfield14.setEditable(editable);
481 public static void main(String[] args)
483 TextFieldDemo app = new TextFieldDemo("TextField Demo");
484 app.pack();
485 app.setVisible(true);