Setting Multi-Line label on the Button

This section shows you how to set the multi line label
on the button in Java Swing Applications.
This program uses html class from javax.swing.text.html*; package
and then breaks the text label of the button into two lines. If you run the
program it will look like following image:

HTML:
This is the class from the javax.swing.text.html.*;
package of Java. This class provides the facility to use the html tags in
java application for texts. There are <html></html> tag and <br>
tag have been used in this program. Tags of html are used with the string which
is the label of the button.
Here is the code of the program:
import javax.swing.*;
import javax.swing.text.html.*;
import java.awt.*;
public class MultilineLabelButton{
public static void main(String[] args){
JFrame frame = new JFrame("Multiline Label for Button");
String lbl = "<html>" + "This label" + "<br>" + "is in two lines" + "</html>";
Panel panel = new Panel();
JButton button = new JButton(lbl);
panel.add(button);
// frame.add(button);
frame.add(panel, BorderLayout.NORTH);
frame.setSize(300, 200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
|
Download this example.

|
Current Comments
2 comments so far (post your own) View All Comments Latest 10 Comments:the setMnemonic() does not seem to have effect on a label set tis way in a JButton. PLease help.
Posted by Janani on Tuesday, 02.26.08 @ 16:39pm | #50136
i prefer this tutorial always,i get more idea on programming ,excellent,to understand the requirement of the ,n well built programmes with output,if possible please provide me the tutorials on "struts",right from basic
thanks
Posted by abhishek das on Saturday, 11.10.07 @ 16:22pm | #36849