How to add a multi line text to a JButton with the line unknown? (dynamically)

23,446

You'll need to at least know how each line is delimited. For example if lines are delimited with newline characters, you could do:

String twoLines = "Two\nLines";
JButton b = 
    new JButton("<html>" + twoLines.replaceAll("\\n", "<br>") + "</html>");
Share:
23,446
William Falcon
Author by

William Falcon

Python, Java, Objective-C, Swift, Javascript, Python, MYSQL, HTML, CSS, Illustrator, Photoshop, Matlab. Deep learning research assistant focused on Convolutional and GAN applications to Neuroscience.

Updated on April 03, 2020

Comments

  • William Falcon
    William Falcon about 4 years

    How do you make the text on a JButton multiline? I have read that most use HTML, but what happens when it is dynamic and you don't know the size of the lines, or what the lines are?

    Right now the text on my button just ends with ... as opposed to making a new line.

    Most other methods assume you know the string and are hard coded, so you can't do this on the fly.

    Edited: I created a method that will do this dynamically for a button