I'm trying to put a two-line piece of text on a JButton; e.g.
+----------+
| READER |
| STOP |
+----------+
But I'm having trouble getting it centered on the button. I go to the Property editor for the JButton, and enter <html><center>READER<br>STOP
for the text property. This causes the two words to be centered with respect to each other, but together they still appear to be shifted toward the right side of the face of the button, as in:
+----------+
| READER|
| STOP |
+----------+
There are also Horizontal & Vertical Alignment & Text Position properties, which I've set all to CENTER, but this doesn't have any effect that I can see.
EDIT: here is an illustration of how it is laid out when I omit <center>
entirely, in case anyone is confused by my description that "READER and STOP are left-justified w/respect to each other, but right-justified on the button":
+----------+
| READER|
| STOP |
+----------+
EDIT: here is the code generated by NetBeans:
readerStopButton_.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.light"));
readerStopButton_.setFont(new java.awt.Font("Geneva", 0, 12)); // NOI18N
readerStopButton_.setText("<html><center>READER<br>STOP</center></html>\n");
readerStopButton_.setToolTipText("<html><b>Stop</b> button is currently inactive. ");
readerStopButton_.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
readerStopButton_.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
readerStopButton_ActionPerformed(evt);
}
});
operationButtons_.add(readerStopButton_);
EDIT: Here's a screen capture of how the button looks to me. There is a lot I don't know about doing layouts, so it's quite possible I'm omitting some crucial info. But basically I'm letting NetBeans do all the work, except for providing the HTML text.
EDIT: Putting up a replacement screen shot that shows all buttons. Note that the ones that don't use HTML (the single-word ones) are correctly aligned, while the two that do use HTML are messed up.
Don't forget the closing tag:
JButton button = new JButton("<html><center>READER<br>STOP</center></html>");
See also How to Use HTML in Swing Components.
Addendum: I have shamelessly cloned @mre's correct HTML above to produce the following image: