1 /* TextAreaDemo.java -- An example showing various textareas 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
.JTabbedPane
;
46 import javax
.swing
.JTextArea
;
47 import javax
.swing
.SwingUtilities
;
48 import javax
.swing
.text
.BadLocationException
;
49 import javax
.swing
.text
.DefaultCaret
;
50 import javax
.swing
.text
.Highlighter
;
51 import javax
.swing
.text
.JTextComponent
;
52 import javax
.swing
.text
.View
;
53 import javax
.swing
.text
.LayeredHighlighter
.LayerPainter
;
56 * A simple textare demo showing various textareas in different states.
58 public class TextAreaDemo
60 implements ActionListener
64 * A custom caret for demonstration purposes. This class is inspired by the
65 * CornerCaret from the OReilly Swing book.
67 * @author Roman Kennke (kennke@aicas.com)
69 static class CornerCaret
78 protected synchronized void damage(Rectangle r
)
83 y
= r
.y
+ (r
.height
* 4 / 5 - 3);
89 public void paint(Graphics g
)
91 JTextComponent comp
= getComponent();
98 r
= comp
.modelToView(dot
);
100 catch (BadLocationException e
)
106 int dist
= r
.height
* 4 / 5 - 3;
107 if ((x
!= r
.x
) || (y
!= r
.y
+ dist
))
117 g
.drawLine(r
.x
, r
.y
+ dist
, r
.x
, r
.y
+ dist
+ 4);
118 g
.drawLine(r
.x
, r
.y
+ dist
+ 4, r
.x
+ 4, r
.y
+ dist
+ 4);
123 static class DemoHighlightPainter
126 static DemoHighlightPainter INSTANCE
= new DemoHighlightPainter();
128 static Color
[] colors
= { Color
.BLUE
, Color
.CYAN
, Color
.GRAY
, Color
.GREEN
,
129 Color
.MAGENTA
, Color
.ORANGE
, Color
.PINK
,
130 Color
.ORANGE
, Color
.RED
, Color
.BLUE
, Color
.YELLOW
};
132 public DemoHighlightPainter()
137 private void paintHighlight(Graphics g
, Rectangle rect
)
139 g
.fillRect(rect
.x
, rect
.y
, rect
.width
, rect
.height
);
142 public void paint(Graphics g
, int p0
, int p1
, Shape bounds
, JTextComponent t
)
147 for (int i
= p0
; i
< p1
; i
++)
149 Rectangle r
= t
.modelToView(i
);
150 Point l1
= t
.modelToView(i
+ 1).getLocation();
152 g
.setColor(colors
[(int) (Math
.random() * colors
.length
)]);
153 g
.fillOval(r
.x
, r
.y
, l1
.x
- r
.x
, r
.height
);
156 catch (BadLocationException ble
)
162 public Shape
paintLayer(Graphics g
, int p0
, int p1
, Shape bounds
,
163 JTextComponent c
, View view
)
165 paint(g
, p0
, p1
, bounds
, c
);
172 * The non wrapping text areas and state buttons.
187 * The char wrapping textareas and state buttons.
200 * The word wrapping textareas and state buttons.
213 * The custom colored textareas and state buttons.
215 JTextArea textarea10
;
217 JTextArea textarea11
;
219 JTextArea textarea12
;
221 JTextArea textarea13
;
223 JTextArea textarea14
;
225 JTextArea textarea14b
;
232 * Some miscellaneous textarea demos.
234 JTextArea textarea15
;
236 JTextArea textarea16
;
238 JTextArea textarea17
;
245 * Creates a new demo instance.
247 public TextAreaDemo()
254 * When the demo is run independently, the frame is displayed, so we should
255 * initialise the content panel (including the demo content and a close
256 * button). But when the demo is run as part of the Swing activity board, only
257 * the demo content panel is used, the frame itself is never displayed, so we
258 * can avoid this step.
260 void initFrameContent()
262 JPanel closePanel
= new JPanel();
263 JButton closeButton
= new JButton("Close");
264 closeButton
.setActionCommand("CLOSE");
265 closeButton
.addActionListener(this);
266 closePanel
.add(closeButton
);
267 add(closePanel
, BorderLayout
.SOUTH
);
271 * Returns a panel with the demo content. The panel uses a BorderLayout(), and
272 * the BorderLayout.SOUTH area is empty, to allow callers to add controls to
273 * the bottom of the panel if they want to (a close button is added if this
274 * demo is being run as a standalone demo).
276 private void createContent()
278 setLayout(new BorderLayout());
279 JTabbedPane tabPane
= new JTabbedPane();
280 tabPane
.addTab("Non-wrap", createNonWrapPanel());
281 tabPane
.addTab("Char-wrap", createCharWrapPanel());
282 tabPane
.addTab("Word-wrap", createWordWrapPanel());
283 tabPane
.addTab("Custom colors", createCustomColoredPanel());
284 tabPane
.addTab("Misc", createMiscPanel());
288 private JPanel
createNonWrapPanel()
290 JPanel panel
= new JPanel(new BorderLayout());
291 panel
.setBorder(BorderFactory
.createTitledBorder("Not wrapping"));
293 panel1
= new JPanel(new GridLayout(2, 2));
295 textarea1
= new JTextArea("Hello World!");
296 textarea1
.setFont(new Font("Dialog", Font
.PLAIN
, 8));
297 panel1
.add(new JScrollPane(textarea1
));
299 textarea2
= new JTextArea("Hello World!");
300 textarea2
.setFont(new Font("Dialog", Font
.ITALIC
, 12));
301 panel1
.add(new JScrollPane(textarea2
));
303 textarea3
= new JTextArea("Hello World!");
304 textarea3
.setFont(new Font("Dialog", Font
.BOLD
, 14));
305 panel1
.add(new JScrollPane(textarea3
));
309 JPanel statePanel
= new JPanel();
310 statePanel
.setLayout(new BoxLayout(statePanel
, BoxLayout
.Y_AXIS
));
311 statePanel
.add(Box
.createVerticalGlue());
312 enabled1
= new JCheckBox("enabled");
313 enabled1
.setSelected(true);
314 enabled1
.addActionListener(this);
315 enabled1
.setActionCommand("ENABLED1");
316 statePanel
.add(enabled1
);
317 editable1
= new JCheckBox("editable");
318 editable1
.setSelected(true);
319 editable1
.addActionListener(this);
320 editable1
.setActionCommand("EDITABLE1");
321 statePanel
.add(editable1
);
322 statePanel
.add(Box
.createVerticalGlue());
323 panel
.add(statePanel
, BorderLayout
.EAST
);
328 private JPanel
createCharWrapPanel()
330 JPanel panel
= new JPanel(new BorderLayout());
331 panel
.setBorder(BorderFactory
.createTitledBorder("Wrap at char bounds"));
333 JPanel innerPanel
= new JPanel(new GridLayout(2, 2));
335 textarea4
= new JTextArea("Hello World!");
336 textarea4
.setLineWrap(true);
337 textarea4
.setFont(new Font("Dialog", Font
.PLAIN
, 8));
338 innerPanel
.add(new JScrollPane(textarea4
));
340 textarea5
= new JTextArea("Hello World!");
341 textarea5
.setLineWrap(true);
342 textarea5
.setFont(new Font("Dialog", Font
.ITALIC
, 12));
343 innerPanel
.add(new JScrollPane(textarea5
));
345 textarea6
= new JTextArea("Hello World!");
346 textarea6
.setLineWrap(true);
347 textarea6
.setFont(new Font("Dialog", Font
.BOLD
, 14));
348 innerPanel
.add(new JScrollPane(textarea6
));
350 panel
.add(innerPanel
);
352 JPanel statePanel
= new JPanel();
353 statePanel
.setLayout(new BoxLayout(statePanel
, BoxLayout
.Y_AXIS
));
354 statePanel
.add(Box
.createVerticalGlue());
355 enabled2
= new JCheckBox("enabled");
356 enabled2
.setSelected(true);
357 enabled2
.addActionListener(this);
358 enabled2
.setActionCommand("ENABLED2");
359 statePanel
.add(enabled2
);
360 editable2
= new JCheckBox("editable");
361 editable2
.setSelected(true);
362 editable2
.addActionListener(this);
363 editable2
.setActionCommand("EDITABLE2");
364 statePanel
.add(editable2
);
365 statePanel
.add(Box
.createVerticalGlue());
366 panel
.add(statePanel
, BorderLayout
.EAST
);
371 private JPanel
createWordWrapPanel()
373 JPanel panel
= new JPanel(new BorderLayout());
374 panel
.setBorder(BorderFactory
.createTitledBorder("Wrap at word bounds"));
376 JPanel innerPanel
= new JPanel(new GridLayout(2, 2));
378 textarea7
= new JTextArea("Hello World!");
379 textarea7
.setWrapStyleWord(true);
380 textarea7
.setLineWrap(true);
381 textarea7
.setFont(new Font("Dialog", Font
.PLAIN
, 8));
382 innerPanel
.add(new JScrollPane(textarea7
));
384 textarea8
= new JTextArea("Hello World!");
385 textarea8
.setWrapStyleWord(true);
386 textarea8
.setLineWrap(true);
387 textarea8
.setFont(new Font("Dialog", Font
.ITALIC
, 12));
388 innerPanel
.add(new JScrollPane(textarea8
));
390 textarea9
= new JTextArea("Hello World!");
391 textarea9
.setWrapStyleWord(true);
392 textarea9
.setLineWrap(true);
393 textarea9
.setFont(new Font("Dialog", Font
.BOLD
, 14));
394 innerPanel
.add(new JScrollPane(textarea9
));
396 panel
.add(innerPanel
);
398 JPanel statePanel
= new JPanel();
399 statePanel
.setLayout(new BoxLayout(statePanel
, BoxLayout
.Y_AXIS
));
400 statePanel
.add(Box
.createVerticalGlue());
401 enabled3
= new JCheckBox("enabled");
402 enabled3
.setSelected(true);
403 enabled3
.addActionListener(this);
404 enabled3
.setActionCommand("ENABLED3");
405 statePanel
.add(enabled3
);
406 editable3
= new JCheckBox("editable");
407 editable3
.setSelected(true);
408 editable3
.addActionListener(this);
409 editable3
.setActionCommand("EDITABLE3");
410 statePanel
.add(editable3
);
411 statePanel
.add(Box
.createVerticalGlue());
412 panel
.add(statePanel
, BorderLayout
.EAST
);
417 private JPanel
createCustomColoredPanel()
419 JPanel panel
= new JPanel(new BorderLayout());
421 JPanel innerPanel
= new JPanel(new GridLayout(3, 2));
422 panel
.setBorder(BorderFactory
.createTitledBorder("Custom colors"));
424 textarea10
= new JTextArea("custom foreground", 10, 15);
425 textarea10
.setForeground(Color
.GREEN
);
426 innerPanel
.add(new JScrollPane(textarea10
));
428 textarea11
= new JTextArea("custom background", 10, 15);
429 textarea11
.setBackground(Color
.YELLOW
);
430 innerPanel
.add(new JScrollPane(textarea11
));
432 textarea12
= new JTextArea("custom disabled textcolor", 10, 15);
433 textarea12
.setDisabledTextColor(Color
.BLUE
);
434 innerPanel
.add(new JScrollPane(textarea12
));
436 textarea13
= new JTextArea("custom selected text color", 10, 15);
437 textarea13
.setSelectedTextColor(Color
.RED
);
438 innerPanel
.add(new JScrollPane(textarea13
));
440 textarea14
= new JTextArea("custom selection color", 10, 15);
441 textarea14
.setSelectionColor(Color
.RED
);
442 innerPanel
.add(new JScrollPane(textarea14
));
444 textarea14b
= new JTextArea("custom selection and selected text color", 10, 15);
445 textarea14b
.setSelectedTextColor(Color
.WHITE
);
446 textarea14b
.setSelectionColor(Color
.BLACK
);
447 innerPanel
.add(new JScrollPane(textarea14b
));
449 panel
.add(innerPanel
);
451 JPanel statePanel
= new JPanel();
452 statePanel
.setLayout(new BoxLayout(statePanel
, BoxLayout
.Y_AXIS
));
453 statePanel
.add(Box
.createVerticalGlue());
454 enabled4
= new JCheckBox("enabled");
455 enabled4
.setSelected(true);
456 enabled4
.addActionListener(this);
457 enabled4
.setActionCommand("ENABLED4");
458 statePanel
.add(enabled4
);
459 editable4
= new JCheckBox("editable");
460 editable4
.setSelected(true);
461 editable4
.addActionListener(this);
462 editable4
.setActionCommand("EDITABLE4");
463 statePanel
.add(editable4
);
464 statePanel
.add(Box
.createVerticalGlue());
465 panel
.add(statePanel
, BorderLayout
.EAST
);
470 private JPanel
createMiscPanel()
472 JPanel panel
= new JPanel(new BorderLayout());
473 panel
.setBorder(BorderFactory
.createTitledBorder("Miscellaneous"));
475 JPanel innerPanel
= new JPanel(new GridLayout(2, 2));
477 textarea15
= new JTextArea("Custom Caret");
478 textarea15
.setCaret(new CornerCaret());
479 innerPanel
.add(new JScrollPane(textarea15
));
481 textarea16
= new JTextArea("Custom Caret color");
482 textarea16
.setCaretColor(Color
.MAGENTA
);
483 innerPanel
.add(new JScrollPane(textarea16
));
485 textarea16
= new JTextArea("Custom Selection painter");
486 textarea16
.setFont(new Font("Dialog", Font
.PLAIN
, 24));
487 textarea16
.setCaret(new DefaultCaret()
489 public Highlighter
.HighlightPainter
getSelectionPainter()
491 return DemoHighlightPainter
.INSTANCE
;
495 innerPanel
.add(new JScrollPane(textarea16
));
497 panel
.add(innerPanel
);
499 JPanel statePanel
= new JPanel();
500 statePanel
.setLayout(new BoxLayout(statePanel
, BoxLayout
.Y_AXIS
));
501 statePanel
.add(Box
.createVerticalGlue());
502 enabled5
= new JCheckBox("enabled");
503 enabled5
.setSelected(true);
504 enabled5
.addActionListener(this);
505 enabled5
.setActionCommand("ENABLED5");
506 statePanel
.add(enabled5
);
507 editable5
= new JCheckBox("editable");
508 editable5
.setSelected(true);
509 editable5
.addActionListener(this);
510 editable5
.setActionCommand("EDITABLE5");
511 statePanel
.add(editable5
);
512 statePanel
.add(Box
.createVerticalGlue());
513 panel
.add(statePanel
, BorderLayout
.EAST
);
518 public void actionPerformed(ActionEvent e
)
520 if (e
.getActionCommand().equals("CLOSE"))
524 else if (e
.getActionCommand().equals("ENABLED1"))
526 boolean enabled
= enabled1
.isSelected();
527 textarea1
.setEnabled(enabled
);
528 textarea2
.setEnabled(enabled
);
529 textarea3
.setEnabled(enabled
);
531 else if (e
.getActionCommand().equals("EDITABLE1"))
533 boolean editable
= editable1
.isSelected();
534 textarea1
.setEditable(editable
);
535 textarea2
.setEditable(editable
);
536 textarea3
.setEditable(editable
);
538 else if (e
.getActionCommand().equals("ENABLED2"))
540 boolean enabled
= enabled2
.isSelected();
541 textarea4
.setEnabled(enabled
);
542 textarea5
.setEnabled(enabled
);
543 textarea6
.setEnabled(enabled
);
545 else if (e
.getActionCommand().equals("EDITABLE2"))
547 boolean editable
= editable2
.isSelected();
548 textarea4
.setEditable(editable
);
549 textarea5
.setEditable(editable
);
550 textarea6
.setEditable(editable
);
552 else if (e
.getActionCommand().equals("ENABLED3"))
554 boolean enabled
= enabled3
.isSelected();
555 textarea7
.setEnabled(enabled
);
556 textarea8
.setEnabled(enabled
);
557 textarea9
.setEnabled(enabled
);
559 else if (e
.getActionCommand().equals("EDITABLE3"))
561 boolean editable
= editable3
.isSelected();
562 textarea7
.setEditable(editable
);
563 textarea8
.setEditable(editable
);
564 textarea9
.setEditable(editable
);
566 else if (e
.getActionCommand().equals("ENABLED4"))
568 boolean enabled
= enabled4
.isSelected();
569 textarea10
.setEnabled(enabled
);
570 textarea11
.setEnabled(enabled
);
571 textarea12
.setEnabled(enabled
);
572 textarea13
.setEnabled(enabled
);
573 textarea14
.setEnabled(enabled
);
574 textarea14b
.setEnabled(enabled
);
576 else if (e
.getActionCommand().equals("EDITABLE4"))
578 boolean editable
= editable4
.isSelected();
579 textarea10
.setEditable(editable
);
580 textarea11
.setEditable(editable
);
581 textarea12
.setEditable(editable
);
582 textarea13
.setEditable(editable
);
583 textarea14
.setEditable(editable
);
584 textarea14b
.setEditable(editable
);
588 public static void main(String
[] args
)
590 SwingUtilities
.invokeLater
595 TextAreaDemo app
= new TextAreaDemo();
596 app
.initFrameContent();
597 JFrame frame
= new JFrame();
598 frame
.getContentPane().add(app
);
600 frame
.setVisible(true);
606 * Returns a DemoFactory that creates a TextAreaDemo.
608 * @return a DemoFactory that creates a TextAreaDemo
610 public static DemoFactory
createDemoFactory()
612 return new DemoFactory()
614 public JComponent
createDemo()
616 return new TextAreaDemo();