Search code examples
buttontextjbuttonfont-sizeline-breaks

JButton - How can I break the line and set two different text sizes in the same button?


I want to put two text types in one button.. The upper one would be a big number, the lower one wuld be a smaller text. The code below already breaks the line however I couldn't manipulate the font.

String twoLines = "Two\nLines";
JButton b = new JButton("<html>" + twoLines.replaceAll("\\n", "<br>") + "</html>");

Solution

  • I want to put two text types in one button. The upper one would be a big number, the lower one wuld be a smaller text.

    This is just an issue with html/css styling. You can surround the text to be styled with a span element containing the necessary style

    JButton button = new JButton("<html>Top<br /><span style=\"font-size: 0.9em\">bottom</span></html>");
    

    Alternatively, you can use a paragraph element to create a new line, and style the paragraph as needed.

    JButton button = new JButton("<html><p>Top</p><p style=\"font-size:0.9em\">Bottom</p></html>");