0

I'm trying to get a two line text in a JButton

The text is suppose to display between and
also between /> and just like this but for some reason, it is not working in my for loop

  JButton title[] = new JButton[6];
  JButton button[] = new JButton[30];
  String[] titleText = {"World Religion", "New Title", "New Title", "New Title", "New Title", "New Title"};


  //
  for (int i=0; i<6; i++) {
     title[i] = new JButton();
     title[i].setText("<html> <br /> </html>"+titleText[i]);
     add(title[i]); 
  }
Community
  • 1
  • 1
afrojuju_
  • 126
  • 1
  • 3
  • 12

2 Answers2

1

You need to put the text between the <html> and </html> tags as such:

title[i].setText("<html><br/>" + titleText[i] + "</html>");

The content inclosed between the tags <html></html> tells the button to interpret it as HTML content.

David Yee
  • 3,515
  • 25
  • 45
0

I suppose you're trying to split each title into lines using the space as a delimiter. If that's the case, I think this should do it:

for (int i=0; i<6; i++) {
   title[i] = new JButton();
   title[i].setText("<html>" + titleText[i].replace(" ", "<br />") + "</html>");
   add(title[i]); 
}
shmosel
  • 49,289
  • 6
  • 73
  • 138