1

So I have a JLabel here that's text is set from a String taken from a file seen here:

Label lName = new JLabel("Charater Name:    " + data[FileIO.NAME]);
        lName.setForeground(Color.WHITE);
        lName.setBounds(640 - 110, 5, 110, 30);
        add(lName);

Since the name could be 3 letters long to 24 letters long I don't want to use a fixed width, I want the label's width to wrap around the text enough so that the text isn't cut off but so that it's not wasting a bunch of space, how can I do this?

Edit: The JLabel's location will be set programmatically depending on it's width.

Currently my only solution is setting the width to this: (int) (lName.getText().length() * (double) 6.8) Which lasts for around 40 letters before being cut off.

Crimson Bloom
  • 287
  • 2
  • 13
  • I don't know whether you mean the same, but this link provides a similar solution. Try having a look at it. http://stackoverflow.com/questions/2420742/make-a-jlabel-wrap-its-text-by-setting-a-max-width – kiner_shah Jul 30 '16 at 12:41
  • I looked at this before and tried it but it doesn't do what I want. – Crimson Bloom Jul 30 '16 at 12:45
  • Ok. I guess there must be some way to do it. Will let you know if I find any! – kiner_shah Jul 30 '16 at 12:47

0 Answers0