2
* $Id: JXTitledSeparator.java,v 1.12 2007/10/20 16:42:52 rah003 Exp $
4
* Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5
* Santa Clara, California 95054, U.S.A. All rights reserved.
7
* This library is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Lesser General Public
9
* License as published by the Free Software Foundation; either
10
* version 2.1 of the License, or (at your option) any later version.
12
* This library is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this library; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
package org.jdesktop.swingx;
24
import java.awt.Color;
26
import java.awt.GridBagConstraints;
27
import java.awt.GridBagLayout;
28
import java.awt.Insets;
29
import javax.swing.Box;
30
import javax.swing.Icon;
31
import javax.swing.JLabel;
32
import javax.swing.JSeparator;
33
import javax.swing.SwingConstants;
34
import javax.swing.UIManager;
35
import javax.swing.plaf.ColorUIResource;
36
import javax.swing.plaf.FontUIResource;
39
* <p>A simple horizontal separator that contains a title.<br/>
41
* <p>JXTitledSeparator allows you to specify the title via the {@link #setTitle} method.
42
* The title alignment may be specified by using the {@link #setHorizontalAlignment}
43
* method, and accepts all the same arguments as the {@link javax.swing.JLabel#setHorizontalAlignment}
46
* <p>In addition, you may specify an Icon to use with this separator. The icon
47
* will appear "leading" the title (on the left in left-to-right languages,
48
* on the right in right-to-left languages). To change the position of the
49
* title with respect to the icon, call {@link #setHorizontalTextPosition}.</p>
51
* <p>The default font and color of the title comes from the <code>LookAndFeel</code>, mimicking
52
* the font and color of the {@link javax.swing.border.TitledBorder}</p>
54
* <p>Here are a few example code snippets:
56
* //create a plain separator
57
* JXTitledSeparator sep = new JXTitledSeparator();
58
* sep.setText("Customer Info");
60
* //create a separator with an icon
61
* sep = new JXTitledSeparator();
62
* sep.setText("Customer Info");
63
* sep.setIcon(new ImageIcon("myimage.png"));
65
* //create a separator with an icon to the right of the title,
67
* sep = new JXTitledSeparator();
68
* sep.setText("Customer Info");
69
* sep.setIcon(new ImageIcon("myimage.png"));
70
* sep.setHorizontalAlignment(SwingConstants.CENTER);
71
* sep.setHorizontalTextPosition(SwingConstants.TRAILING);
77
public class JXTitledSeparator extends JXPanel {
79
* Implementation detail: the label used to display the title
83
* Implementation detail: a separator to use on the left of the
84
* title if alignment is centered or right justified
86
private JSeparator leftSeparator;
88
* Implementation detail: a separator to use on the right of the
89
* title if alignment is centered or left justified
91
private JSeparator rightSeparator;
94
* Creates a new instance of <code>JXTitledSeparator</code>. The default title is simply
95
* an empty string. Default justification is <code>LEADING</code>, and the default
96
* horizontal text position is <code>TRAILING</code> (title follows icon)
98
public JXTitledSeparator() {
103
* Creates a new instance of <code>JXTitledSeparator</code> with the specified
104
* title. Default horizontal alignment is <code>LEADING</code>, and the default
105
* horizontal text position is <code>TRAILING</code> (title follows icon)
107
public JXTitledSeparator(String title) {
108
this(title, SwingConstants.LEADING, null);
112
* Creates a new instance of <code>JXTitledSeparator</code> with the specified
113
* title and horizontal alignment. The default
114
* horizontal text position is <code>TRAILING</code> (title follows icon)
116
public JXTitledSeparator(String title, int horizontalAlignment) {
117
this(title, horizontalAlignment, null);
121
* Creates a new instance of <code>JXTitledSeparator</code> with the specified
122
* title, icon, and horizontal alignment. The default
123
* horizontal text position is <code>TRAILING</code> (title follows icon)
125
public JXTitledSeparator(String title, int horizontalAlignment, Icon icon) {
126
setLayout(new GridBagLayout());
128
label = new JLabel(title) {
130
public void updateUI(){
136
label.setHorizontalAlignment(horizontalAlignment);
137
leftSeparator = new JSeparator();
138
rightSeparator = new JSeparator();
147
* Implementation detail. Handles updates of title color and font on LAF change. For more
148
* details see swingx#451.
150
protected void updateTitle()
152
if (label == null) return;
154
Color c = label.getForeground();
155
if (c == null || c instanceof ColorUIResource)
156
setForeground(UIManager.getColor("TitledBorder.titleColor"));
158
Font f = label.getFont();
159
if (f == null || f instanceof FontUIResource)
160
setFont(UIManager.getFont("TitledBorder.font"));
164
* Implementation detail. lays out this component, showing/hiding components
165
* as necessary. Actually changes the containment (removes and adds components).
166
* <code>JXTitledSeparator</code> is treated as a single component rather than
169
private void layoutSeparator() {
172
switch (label.getHorizontalAlignment()) {
173
case SwingConstants.LEFT:
174
case SwingConstants.LEADING:
175
case SwingConstants.WEST:
176
add(label, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(0,0,0,0), 0, 0));
177
add(Box.createHorizontalStrut(3), new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(0,0,0,0), 0, 0));
178
add(rightSeparator, new GridBagConstraints(2, 0, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(0,0,0,0), 0, 0));
180
case SwingConstants.RIGHT:
181
case SwingConstants.TRAILING:
182
case SwingConstants.EAST:
183
add(rightSeparator, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(0,0,0,0), 0, 0));
184
add(Box.createHorizontalStrut(3), new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(0,0,0,0), 0, 0));
185
add(label, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(0,0,0,0), 0, 0));
187
case SwingConstants.CENTER:
189
add(leftSeparator, new GridBagConstraints(0, 0, 1, 1, 0.5, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(0,0,0,0), 0, 0));
190
add(Box.createHorizontalStrut(3), new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(0,0,0,0), 0, 0));
191
add(label, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0,0,0,0), 0, 0));
192
add(Box.createHorizontalStrut(3), new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(0,0,0,0), 0, 0));
193
add(rightSeparator, new GridBagConstraints(4, 0, 1, 1, 0.5, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(0,0,0,0), 0, 0));
198
* Sets the title for the separator. This may be simple html, or plain
201
* @param title the new title. Any string input is acceptable
203
public void setTitle(String title) {
204
String old = getTitle();
205
label.setText(title);
206
firePropertyChange("title", old, getTitle());
212
* @return the title being used for this <code>JXTitledSeparator</code>.
213
* This will be the raw title text, and so may include html tags etc
214
* if they were so specified in #setTitle.
216
public String getTitle() {
217
return label.getText();
221
* <p>Sets the alignment of the title along the X axis. If leading, then
222
* the title will lead the separator (in left-to-right languages,
223
* the title will be to the left and the separator to the right). If centered,
224
* then a separator will be to the left, followed by the title (centered),
225
* followed by a separator to the right. Trailing will have the title
226
* on the right with a separator to its left, in left-to-right languages.</p>
228
* <p>LEFT and RIGHT always position the text left or right of the separator,
229
* respectively, regardless of the language orientation.</p>
231
* @param alignment One of the following constants
232
* defined in <code>SwingConstants</code>:
234
* <code>CENTER</code>,
235
* <code>RIGHT</code>,
236
* <code>LEADING</code> (the default) or
237
* <code>TRAILING</code>.
239
* @throws IllegalArgumentException if the alignment does not match one of
240
* the accepted inputs.
241
* @see SwingConstants
242
* @see #getHorizontalAlignment
244
public void setHorizontalAlignment(int alignment) {
245
int old = getHorizontalAlignment();
246
label.setHorizontalAlignment(alignment);
247
if (old != getHorizontalAlignment()) {
250
firePropertyChange("horizontalAlignment", old, getHorizontalAlignment());
254
* Returns the alignment of the title contents along the X axis.
256
* @return The value of the horizontalAlignment property, one of the
257
* following constants defined in <code>SwingConstants</code>:
259
* <code>CENTER</code>,
260
* <code>RIGHT</code>,
261
* <code>LEADING</code> or
262
* <code>TRAILING</code>.
264
* @see #setHorizontalAlignment
265
* @see SwingConstants
267
public int getHorizontalAlignment() {
268
return label.getHorizontalAlignment();
272
* Sets the horizontal position of the title's text,
273
* relative to the icon.
275
* @param position One of the following constants
276
* defined in <code>SwingConstants</code>:
278
* <code>CENTER</code>,
279
* <code>RIGHT</code>,
280
* <code>LEADING</code>, or
281
* <code>TRAILING</code> (the default).
282
* @throws IllegalArgumentException if the position does not match one of
283
* the accepted inputs.
285
public void setHorizontalTextPosition(int position) {
286
int old = getHorizontalTextPosition();
287
label.setHorizontalTextPosition(position);
288
firePropertyChange("horizontalTextPosition", old, getHorizontalTextPosition());
292
* Returns the horizontal position of the title's text,
293
* relative to the icon.
295
* @return One of the following constants
296
* defined in <code>SwingConstants</code>:
298
* <code>CENTER</code>,
299
* <code>RIGHT</code>,
300
* <code>LEADING</code> or
301
* <code>TRAILING</code>.
303
* @see SwingConstants
305
public int getHorizontalTextPosition() {
306
return label.getHorizontalTextPosition();
310
* Defines the icon this component will display. If
311
* the value of icon is null, nothing is displayed.
313
* The default value of this property is null.
315
* @see #setHorizontalTextPosition
318
public void setIcon(Icon icon) {
319
Icon old = getIcon();
321
firePropertyChange("icon", old, getIcon());
325
* Returns the graphic image (glyph, icon) that the
326
* <code>JXTitledSeparator</code> displays.
331
public Icon getIcon() {
332
return label.getIcon();
339
public void setForeground(Color foreground) {
341
label.setForeground(foreground);
343
super.setForeground(foreground);
350
public void setFont(Font font) {