Java – how to control the centering of JButton HTML text in NetBeans?
I tried to put one or two lines on JButton; for example
+----------+ | READER | | STOP | +----------+
But I can't focus it on the button I go to JButton's Attribute Editor and enter < HTML > < center > reader < br > stop for the text attribute This causes the two words to be centered relative to each other, but together they still seem to move to the right of the button face, such as:
+----------+ | READER| | STOP | +----------+
There are also horizontal & vertical alignment & text position attributes. I set them all to center, but this has no effect I can see
Editor: here is a description of how to layout when I omit < center >. If someone is confused about my description, "reader and stop are mutually respectful left aligned, but right aligned on the button":
+----------+ | READER| | STOP | +----------+
Edit: This is the code generated by NetBeans:
readerStopButton_.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.light")); readerStopButton_.setFont(new java.awt.Font("Geneva",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_);
Editor: This is a screenshot of the button's view of me There are many I don't know about layout, so I'm likely to omit some key information But basically I let NetBeans do all the work except providing HTML text
Edit: opens a replacement screenshot showing all buttons Please note that the words that do not use HTML (single word words) are aligned correctly, and the two that use HTML are messing up
Solution
The reason is that you limit the size of the button. By default, the button has an allowance. You can delete the margin as follows:
readerStopButton_.setMargin(new Insets(0,-30,-30));
package swing; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Insets; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; public class HTMLTextTest { public static void main(String[] args) { SwingUtilities.invokelater(new Runnable() { public void run() { createAndShowGUI(); } }); } private static void createAndShowGUI() { final JFrame frame = new JFrame(); frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new FlowLayout()); JPanel operationButtons_ = new JPanel(); JButton readerStopButton_ = new JButton(); readerStopButton_.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.light")); readerStopButton_.setFont(new java.awt.Font("Geneva",12)); // NOI18N readerStopButton_.setText("<html><center>READER<br>STOP</center></html>\n"); readerStopButton_.setToolTipText("<html><b>Stop</b> button is currently inactive. "); readerStopButton_.setMargin(new Insets(0,-30)); readerStopButton_.setPreferredSize(new Dimension(66,40)); operationButtons_.add(readerStopButton_); readerStopButton_ = new JButton(); readerStopButton_.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.light")); readerStopButton_.setFont(new java.awt.Font("Geneva",12)); // NOI18N readerStopButton_.setText("<html><center>READER<br>STOP</center></html>\n"); readerStopButton_.setToolTipText("<html><b>Stop</b> button is currently inactive. "); System.out.println(readerStopButton_.getPreferredSize()); readerStopButton_.setPreferredSize(new Dimension(66,40)); operationButtons_.add(readerStopButton_); operationButtons_.add(new JButton("yCoder.com")); frame.add(operationButtons_); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }