views:

57

answers:

1

I use HTML to put text into a JButton. In this way I can play with colors and text size. What I do not like is the distance from the left border of the button and the text (this separation is too large). Is there a way to decrease this distance? I think it should be some parameter in the style of the HTML code.

Sample of the code:

JButton btn = new JButton("<html><span style='color:#000000; font-size: 11pt;'>" + label + "</span></html>");
+1  A: 

I would recommend doing this programmatically rather than attempting to do it with HTML as you'll be more likely to see consistent results across platforms.

JButton btn = ...
btn.setHorizontalTextPosition(SwingConstants.LEFT);

Then you can customise the font size by either overriding paintComponent (more work) or modifying the FontUIResource object at start-up (although this will affect the font size of all buttons); e.g.

FontUIResource f = new FontUIResource("Tahoma", Font.PLAIN, 11);
Enumeration<Object> it = UIManager.getDefaults().keys();

while (it.hasMoreElements()) {
    Object key = it.nextElement();
    if (UIManager.get(key) instanceof FontUIResource) {
        UIManager.put(key, f);
    }
}
Adamski
The font size is OK. I just would like to shift the text close to the left border of the button (so I can see the right part of the text).
Roman