3

How can I insert a newline when I am using JLabel.setText()? I tried to use Html but can seem to make it work for setText, only for the initial declaration of the jLabel

the way to do it when initially declaring the jlabel is:

label = new JLabel("<html>Hello World!<br>blahblahblah</html>");

my code:

textString += "<html> quantityTextField.getText() +
   theInventory.findItem(UPCTextField.getText()).toString() + <br> </html>";

purchaseInfo.setText( textString);

it displays the html tags and the method names instead of the string returned by the methods

mKorbel
  • 109,525
  • 20
  • 134
  • 319
trs
  • 2,454
  • 13
  • 42
  • 61

7 Answers7

5

If your setText() call changes the preferred dimensions of the JLabel, then you need to call revalidate() on the container to get the layout redone.

Looking at the code snippet you've added, I see a
at the very end of the line -- which won't really do anything anyway -- and a lot of misquoted method calls which are done such that the method names are part of the HTML. If you do something along the lines of

label.setText("<html>Hello World!<br>blahblahblah</html>");
label.getParent().revalidate();

your newline ought to show up.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
4

The formatting of your text is all wrong. This is the string that you're currently setting:

"<html>Hello World!<br>blahblahblah</html><html> quantityTextField.getText()+ theInventory.findItem(UPCTextField.getText()).toString() + <br> </html>"

Now tell me the problem isn't painfully obvious (by the way, HTML isn't your only problem)... Anyway, for more information, please see How to Use HTML in Swing Components.

mre
  • 43,520
  • 33
  • 120
  • 170
2
textString+="<html> " + quantityTextField.getText()+ theInventory.findItem(UPCTextField.getText()).toString() + "<br> </html>";

funny.

MarianP
  • 2,719
  • 18
  • 17
  • well, you included your 'method calls' in the string, so they were not evaluated. "quantityTextField.getText()" is a String, quantityTextField.getText() returns result of a call to the method. – MarianP Oct 05 '11 at 20:12
0

Anyone not testing their answer should qualify it with "I think".

As others have said, \n won't work, .revalidate() isn't necessary, and html is. See Newline in JLabel

Community
  • 1
  • 1
gherson
  • 169
  • 2
  • 9
0

Hello bro insert your new line between "pre"

JLabel l=new JLabel("<html><pre>Hello world\nBlahblahblah</pre></html>");

i hope this help you

0

You appear to be concatenating something with your html string.

Surround the text in the <html> tags and only use the <html> tags once.

Garrett Hall
  • 29,524
  • 10
  • 61
  • 76
0

In personal experience, I wouldn't use HTML tags in a JLabel. If you're not doing any formatting (which it looks like you aren't, other than inserting a line break), you're better off using character codes like '\n' because of simplicity and size.

[code] label = new JLabel("Hello World!\nblahblahblah");

textString += quantityTextField.getText() + theInventory.findItem(UPCTextField.getText()).toString() + "\n";

purchaseInfo.setText( textString); [/code]

The reason the method names were showing up was because you had them enclosed within quotes. It was taking the methods as actual text to write to the screen, not a set of instructions to perform.