KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sshtools > ui > swing > MultilineLabel


1 /* HEDAER */
2
3 package com.sshtools.ui.swing;
4
5 import java.util.StringTokenizer JavaDoc;
6
7 import java.awt.Font JavaDoc;
8 import java.awt.GridBagConstraints JavaDoc;
9 import java.awt.GridBagLayout JavaDoc;
10 import javax.swing.JLabel JavaDoc;
11 import javax.swing.JPanel JavaDoc;
12
13 /**
14  * Swing component that takes a string, splits it up into lines based on the
15  * newline character and displays each line.
16  *
17  * @author $Author: brett $
18  */

19
20 public class MultilineLabel extends JPanel JavaDoc {
21
22     // Private instance variables
23

24     private GridBagConstraints JavaDoc constraints;
25
26     private String JavaDoc text;
27
28     /**
29      * Creates a new MultilineLabel object.
30      */

31
32     public MultilineLabel() {
33
34         this(""); //$NON-NLS-1$
35

36     }
37
38     /**
39      * Creates a new MultilineLabel object.
40      *
41      * @param text
42      */

43
44     public MultilineLabel(String JavaDoc text) {
45
46         super(new GridBagLayout JavaDoc());
47
48         constraints = new GridBagConstraints JavaDoc();
49
50         constraints.anchor = GridBagConstraints.NORTHWEST;
51
52         constraints.fill = GridBagConstraints.NONE;
53
54         setText(text);
55
56     }
57
58     /**
59      * Set the font
60      *
61      * @param f font
62      */

63
64     public void setFont(Font JavaDoc f) {
65
66         super.setFont(f);
67
68         for (int i = 0; i < getComponentCount(); i++) {
69
70             getComponent(i).setFont(f);
71
72         }
73
74     }
75
76     /**
77      * Set the font
78      *
79      * @param text
80      */

81
82     public void setText(String JavaDoc text) {
83
84         this.text = text;
85
86         removeAll();
87
88         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(text, "\n"); //$NON-NLS-1$
89

90         constraints.weighty = 0.0;
91
92         constraints.weightx = 1.0;
93
94         while (tok.hasMoreTokens()) {
95
96             String JavaDoc t = tok.nextToken();
97
98             if (!tok.hasMoreTokens()) {
99
100                 constraints.weighty = 1.0;
101
102             }
103
104             UIUtil.jGridBagAdd(this, new JLabel JavaDoc(t), constraints,
105
106             GridBagConstraints.REMAINDER);
107
108         }
109
110         revalidate();
111
112         repaint();
113
114     }
115
116     /**
117      * Get the text
118      *
119      * @return text
120      */

121
122     public String JavaDoc getText() {
123
124         return text;
125
126     }
127
128 }
129
Popular Tags