Some reader favorites:
EJB fundamentals and session beans
Create a scrollable virtual desktop in Swing
Wizard API updated!
Tim Boudreau has released a new version of the Swing Wizard library (version 0.997) that fixes the WizardException bug reported in JavaWorld's recent Open Source Java Project profile. The article's examples have been reworked to test out the new, improved WizardException. Thanks, Tim, for this helpful fix!
Open Source Java Projects: The Wizard API
Suppose I want to create the button with a label as shown below:
"Your
Name"
"Name" will be on the second line.
I tried to create it with:
new JButton("Your \\n Name")
Unfortunately, that doesn't work. Do you have any suggestions?
The short answer: you can't do it without a bit of work on your part. The JButton does not intrinsically have the ability to wrap long lines or start a new line when the text contains a \n.
All JFC components are rendered through a corresponding ComponentUI. Instead of putting all of the rendering code in the JButton's paint method, the JButton delegates its display to a ButtonUI (an extension of ComponentUI). The ComponentUI allows us to change the look and feel of a Java Swing GUI on the fly by simply swapping in new ComponentUI renderers into our JComponents.
One implementation of ButtonUI that a JButton may use is the BasicButtonUI. If you're brave enough to look at the BasicButtonUI's code, you'll notice the method:
protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text)
paintText() does all the string drawing dirty work. If we dig a little further, you'll see that this method further delegates the actual
drawing to the drawString() method in BasicGraphicsUtils:
public static void drawString(Graphics g,String text,int underlinedChar,int x,int y)
drawString() does a little processing and then simply calls the Graphics-based drawString() method. We had to dig a bit, but now it's fairly obvious why JButton won't wrap text around a new line.
So, to do what you want to do, you have a few choices:
JButton by extending JButton and overriding its paint method. In that method, you would tokenize your string based on \n, do a few spacing calculations, draw your button, and then draw your broken text. However, if you do it that way, your button
will lose its pluggable look and feel. You will also lose many of the other Swing display tricks.
ButtonUI that formats your text properly.
JButton. Instead, a combination of JLabels and the JButton will give you what you need. Try the following: JButton b = new JButton();
b.setLayout(new BorderLayout());
JLabel label1 = new JLabel("Your");
JLabel label2 = new JLabel("Name");
b.add(BorderLayout.NORTH,label1);
b.add(BorderLayout.SOUTH,label2);
If you find that a multiline button is something you use a lot, you can wrap the above solution into a convenience class,
say MultiLineButton. That class would extend JButton, encapsulate two JLabel instances, and have void setLabel1Text(String text) and void setLabel2Text(String text) methods.
| HTML rendering in Swing is possible! | |
|---|---|
|
Free Download - 5 Minute Product Review. When slow equals Off: Manage the complexity of Web applications - Symphoniq
![]()
Free Download - 5 Minute Product Review. Realize the benefits of real user monitoring in less than an hour. - Symphoniq