Creating a Multiline Label for a JButton Component
A 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.
By default, the lines are left justified. This label text will center
the lines:
This label text italicizes the second line:
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);
button.setText("<html><center>"+"This is a"+"<br>"+"swing button"+"</center></html>");
button.setText("<html>"+"This is a"+"<br><i>"+"swing button"+"</i></html>");
Post a comment