We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Multiple lines in JLabel

naveenu
naveenu asked
on
Medium Priority
536 Views
Last Modified: 2008-02-01
i want to display a string of text on JLabel in multiple lines.
can anyone help me out.
i heard PsMultiLineLabel() can be used for this,but couldn't do so.
So please tell whether this method should be used or is their any other
way with its implementation.
Thanks in advance
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
You could simply use the newlien character:

JLabel test = new JLabel("This \nis \na \nmultiple \nline \label");
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>You could simply use the newlien character:

No - that won't work
Should be:
JLabel test = new JLabel("<HTML>This<BR>is<BR>a<BR>multiple<BR>line<BR>label");

Almoost all other HTML features work!

;JOOP!
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
>>You could simply use the newlien character:
>
>No - that won't work

You are right.

Author

Commented:
i cant use html nor newline
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>i cant use html nor newline

Why not?
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Then you will have to write your own class that extends BasicLabelUI and exhibits the behaviour you want.

http://www.codeguru.com/java/articles/198.shtml
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> i cant use html nor newline

JLabel will allow it by default (<HTML>....<br>...., etc) - you don't need anything extra
CERTIFIED EXPERT
Top Expert 2016

Commented:
naveenu, did you even try HTML?
This solution has worked over years for everyone and has been
asked for many times.

;JOOP!
CERTIFIED EXPERT
Top Expert 2016
Commented:
Of course you can also use a JTextArea as a label. Set the word and line wrap appropriately

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
i will try HTML.But my problem is i have a long string saved in a variable.
So i wanted a way to display this string in multiple lines;as we can do in
JTextArea(),which takes the number of rows(columns).
i cant use JTextArea() coz it is going out of components area,i can use only JLabel.
So any advice

Well what's your problem then?????
Do it as I said!

;JOOP!
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Keep a variable that tells the length of each line. Then you can do:

StringBuffer sb = new StringBuffer ( "<HTML>" ) ;
String[] array = displayMessage.toCharArray () ;

for ( int i = 0, j = 1, count = array.length ; i < count ; i ++, j ++ )
{
  sb.append ( array[i] ) ;

  if ( j % length == 0 )
    sb.append ( "<BR>" ) ; // end if
}

sb.append ( "</HTML>" ) ;
label.setText ( sb.toString () ) ;
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>i cant use JTextArea() coz it is going out of components area

That won't make any difference. A JLabel is likely to be just the same size to be able show all the lines.

If you want to change the 'growing' effect you could add the JTextArea to a JScrollPane and add the scroll pane to your container

Author

Commented:
Well the correct answer would  be

JTextArea text=new JTextArea("Lengthy String");
      text.setEditable(false);
      text.setLineWrap(true);
      text.setWrapStyleWord(true);
      text.setSize(50,50);
      text.setMinimumSize(text.getPreferredSize());

It works similar to Label but with multiple lines.

Only CEHJ came close.
So i think points should be given back to me.
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>Well the correct answer would  be ...

That will still grow. The only way to constrain the growth and still have something usable is to use a scroll pane as i mentioned

>>- PAQ-ing the question and refunding 50 points

Unacceptable sorry
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> i will try HTML.But my problem is i have a long string saved in a variable.

That is what I explained in http:#16297869

And like CEHJ said the size of JLabel would anyway have to be of the size of the JTextArea as long as the amount information being displayed is the same. Also, scuiriware put you in the right direction initially.

This should have been a split, but 50 is less for a 3-way split.
CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
sciuriware also deserved some points, I think.
CERTIFIED EXPERT
Top Expert 2016

Commented:
scuiriware suggested using html after i did, which wasn't seen as too acceptable anyway. No offence intended s ;-)
CEHJ got only 100 points for his efforts.
Leave it like that.
Another time, another opportunity.

;JOOP!

Author

Commented:
But really speaking i could not benefit from CEHJ & found the answer on my own;but later saw that he had suggested the same thing.
never mind enjoy :-)
Using HTML was out of context since when u have a long string how can u insert into it <br> etc.

thank for help
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> since when u have a long string how can u insert into it

You probably missed seeing my comment though I reminded you: >> http:#16297869

Author

Commented:
yeah i saw that but who want to work that much when u can have readymade in TextArea()
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Well, if you want a solution and when you get it, you can't discard it by saying who wants to do it just because there is no shorter way to do it.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
It was certainly not out of context and not something remotely difficult or impossible. Anyway I'm fine with JTextArea if that is your decision.

Author

Commented:
:-)
See? CEHJ deserved A.

But it remains peanuts.

;JOOP!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.