![]() |
The Java Developers Almanac 1.4 |
|
e745. Creating a Multiline Label for a JButton ComponentA button label can show simple HTML tags when surrounded by the tags<HTML> and </HTML>. This example shows how to create
multiple lines by using the <BR> tag. See the
javax.swing.text.html.HTML class documentation for a list of
supported tags.
String label = "<html>"+"This is a"+"<br>"+"swing button"+"</html>";
// Create an action with the label
Action action = new AbstractAction(label) {
// This method is called when the button is pressed
public void actionPerformed(ActionEvent evt) {
// Perform action
}
};
// Create the button
JButton button = new JButton(action);
By default, the lines are left justified. This label text will center
the lines:
button.setText("<html><center>"+"This is a"+"<br>"+"swing button"+"</center></html>");
This label text italicizes the second line:
button.setText("<html>"+"This is a"+"<br><i>"+"swing button"+"</i></html>");
e744. Changing the Label of a JButton Component e746. Adding an Icon to a JButton Component e747. Moving the Icon in a JButton Component e748. Moving the Label/Icon Pair in a JButton Component e749. Setting the Gap Size Between the Label and Icon in a JButton Component e750. Adding a Disabled Icon to a JButton Component e751. Adding a Rollover and Pressed Icon to a JButton Component © 2002 Addison-Wesley. |