KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > wingset > TextComponentExample


1 /*
2  * $Id: TextComponentExample.java,v 1.11 2005/06/03 13:17:17 blueshift Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package wingset;
15
16 import org.wings.*;
17 import org.wings.text.SNumberFormatter;
18 import org.wings.event.SDocumentListener;
19 import org.wings.event.SDocumentEvent;
20
21 import java.awt.event.ActionEvent JavaDoc;
22 import java.awt.event.ActionListener JavaDoc;
23 import java.text.NumberFormat JavaDoc;
24
25 /**
26  * @author <a HREF="mailto:mreinsch@to.com">Michael Reinsch</a>
27  * @version $Revision: 1.11 $
28  */

29 public class TextComponentExample
30         extends WingSetPane
31 {
32     private ComponentControls controls;
33     private SLabel documentEvent = new SLabel();
34     private SLabel actionEvent = new SLabel();
35
36     SDocumentListener documentListener = new SDocumentListener() {
37                 public void insertUpdate(SDocumentEvent e) {
38                     documentEvent.setText(((STextComponent) e.getSource()).getName());
39                     actionEvent.setText("button not pressed");
40                 }
41
42                 public void removeUpdate(SDocumentEvent e) {
43                     documentEvent.setText(((STextComponent) e.getSource()).getName());
44                     actionEvent.setText("button not pressed");
45                 }
46
47                 public void changedUpdate(SDocumentEvent e) {
48                     if (e.getSource() instanceof STextComponent) {
49                         STextComponent component = (STextComponent) e.getSource();
50                         documentEvent.setText(((STextComponent) e.getSource()).getName());
51                     }
52                     else {
53                         log.debug("e.getSource().getClass() = " + e.getSource().getClass());
54                     }
55                     actionEvent.setText("button not pressed");
56                 }
57             };
58
59     public SComponent createExample() {
60         controls = new ComponentControls();
61
62         SGridLayout gridLayout = new SGridLayout(2);
63         gridLayout.setHgap(4);
64         gridLayout.setVgap(4);
65         SPanel p = new SPanel(gridLayout);
66
67         SLabel l = new SLabel("STextField: ");
68         l.setVerticalAlignment(SConstants.TOP);
69         p.add(l);
70         STextField textField = new STextField();
71         textField.setName("textfield");
72         textField.addDocumentListener(documentListener);
73         p.add(textField);
74
75         l = new SLabel("STextArea: ");
76         l.setVerticalAlignment(SConstants.TOP);
77         p.add(l);
78         STextArea textArea = new STextArea();
79         textArea.setName("textarea");
80         textArea.addDocumentListener(documentListener);
81         p.add(textArea);
82
83         l = new SLabel("SFormattedTextField: ");
84         l.setVerticalAlignment(SConstants.TOP);
85         p.add(l);
86         SNumberFormatter formatter = new SNumberFormatter(NumberFormat.getNumberInstance(getSession().getLocale()));
87         SFormattedTextField formattedTextField = new SFormattedTextField(formatter);
88         formattedTextField.setName("formattedTextField");
89         formattedTextField.addDocumentListener(documentListener);
90         p.add(formattedTextField);
91
92         p.add(new SLabel("DocumentEvent: "));
93         p.add(documentEvent);
94
95         SButton button = new SButton("Submit");
96         p.add(button);
97         p.add(actionEvent);
98
99         button.addActionListener(new ActionListener JavaDoc() {
100             public void actionPerformed(ActionEvent JavaDoc e) {
101                 actionEvent.setText("Button pressed");
102             }
103         });
104
105         SForm f = new SForm(new SBorderLayout());
106         f.addActionListener(new ActionListener JavaDoc() {
107             public void actionPerformed(ActionEvent JavaDoc e) {
108                 actionEvent.setText(actionEvent.getText() + " + form event");
109             }
110         });
111
112         controls.addSizable(textField);
113         controls.addSizable(textArea);
114         controls.addSizable(formattedTextField);
115
116         f.add(controls, SBorderLayout.NORTH);
117         f.add(p, SBorderLayout.CENTER);
118         return f;
119     }
120 }
121
122
123
Popular Tags