3

I have a bit of an unusual problem: I have a swing label that updates for "Used letters" in the game Hangman. I have it declared like this:

used = new JLabel(" Used: ");

But then i have another area in the code where i add onto it. Here:

used.setText(used.getText() + lUsed  + " , ");

Now when the user enters letters, it updates the label and the letters are added, but if you enter enough, they push the rest of the contents of the frame out of view. The label expands the WEST side of my borderlayout so much that everything else get's pushed and then some times cut off the application window.

Is there a good way to use HTML to make it so that it word wraps the text and does not change the panel (what it's contained in) size?

    GridLayout c = new GridLayout(rows,0);
    westPanel = new JPanel(c);
    westPanel.add(guessedPanel);
    westPanel.add(usedPanel);
    frame.getContentPane().add(westPanel, BorderLayout.WEST);

How it's laid out ^^

mKorbel
  • 109,525
  • 20
  • 134
  • 319
ElectroMan
  • 156
  • 1
  • 13

2 Answers2

5

For a more general solution for those coming from Google, you can add HTML to Swing components very easily, as long as the content being added starts and ends with the proper HTML tags.

For example, you may insert HTML text into a JLabel such that:

label.setText("<html> Text </html>");

You could color center it and underline the first letter like this:

label.setText("<html><center><u>T</u>ext</center></html>");

Here's a good tutorial on how to use HTML in your Swing components.

--

For your specific solution, you can simply use HTML when adding text to the label. Instead of using used.getText() you should store the text in a String somewhere (that does not enclose the opening and closing HTML tags), and update that String, in order to be able to effectively manage the opening and closing HTML tags. So it could go something like this

String text; // Earlier in the code
...
text += ("<br> " + lUsed);
used.setText("<html>" + text + "</html>")

Among other things, you could attempt setting a preferred or maximum size of the label so that it is not allowed to become to large. Also, you could make the label a text panel or text area of some kind instead, which would automatically support word wrapping, scrolling, and other features that you may want.

Here's a guide on how to use JTextAreas.

Connorelsea
  • 2,308
  • 6
  • 26
  • 47
  • I am not sure, but don't you need `` before your html? like: `used.setText("" + used.getText() + "
    " + lUsed);`? (i did not downvote!)
    – Ben May 29 '15 at 17:03
  • Swing components are not able to use basic java text commands like `\n`- they are ignored by the system or they are implemented literally(e.g. "Used: \n" would be printed). Adding the `` means that you would like to use HTML. Without the `` tag you physically print out `
    ` instead of breaking the line.
    – ElectroMan May 29 '15 at 17:35
  • Is this not correct? Is there something else that should be included? – Connorelsea May 29 '15 at 21:13
  • `
    ` HTML/styles has much more intelligent (automatic) ways to wrap text. See my answer.
    – Andrew Thompson May 30 '15 at 01:35
1

Specify a width in styles for the body tag of the HTML. See Text Wrap in JOptionPane for an example.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433