Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I control the centering of JButton HTML text in NetBeans?

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.

better view of problem

like image 469
Chap Avatar asked Aug 05 '11 21:08

Chap


2 Answers

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:

enter image description here

like image 143
trashgod Avatar answered Sep 20 '22 01:09

trashgod


I'm not sure what you're talking about. I used the code you've included and it works just fine.


public final class HTMLTextButtonDemo {

    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                createAndShowGUI();             
            }
        });
    }

    private static void createAndShowGUI(){
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());

        JButton readerStopButton_ = new JButton();
        readerStopButton_.setFocusPainted(false);
        readerStopButton_.setBackground(UIManager.getDefaults().getColor("Button.light"));
        readerStopButton_.setFont(new Font("Geneva", 0, 12)); // NOI18N
        readerStopButton_.setText("<html><center>READER<br>STOP</center></html>\n");
        readerStopButton_.setHorizontalTextPosition(SwingConstants.CENTER);

        frame.add(readerStopButton_);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

enter image description here


The HTML text is center justified. It's hard to imagine that you're seeing something different.

like image 43
mre Avatar answered Sep 19 '22 01:09

mre