Multiple lines in JLabel

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
naveenuAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
0
girionisCommented:
You could simply use the newlien character:

JLabel test = new JLabel("This \nis \na \nmultiple \nline \label");
0
CEHJCommented:
>>You could simply use the newlien character:

No - that won't work
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

sciuriwareCommented:
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!
0
girionisCommented:
>>You could simply use the newlien character:
>
>No - that won't work

You are right.
0
naveenuAuthor Commented:
i cant use html nor newline
0
CEHJCommented:
>>i cant use html nor newline

Why not?
0
girionisCommented:
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
0
Mayank SAssociate Director - Product EngineeringCommented:
>> i cant use html nor newline

JLabel will allow it by default (<HTML>....<br>...., etc) - you don't need anything extra
0
sciuriwareCommented:
naveenu, did you even try HTML?
This solution has worked over years for everyone and has been
asked for many times.

;JOOP!
0
CEHJCommented:
Of course you can also use a JTextArea as a label. Set the word and line wrap appropriately
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
naveenuAuthor 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

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

;JOOP!
0
Mayank SAssociate Director - Product EngineeringCommented:
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 () ) ;
0
CEHJCommented:
>>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
0
naveenuAuthor 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.
0
CEHJCommented:
>>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
0
Mayank SAssociate Director - Product EngineeringCommented:
>> 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.
0
CEHJCommented:
:-)
0
Mayank SAssociate Director - Product EngineeringCommented:
sciuriware also deserved some points, I think.
0
CEHJCommented:
scuiriware suggested using html after i did, which wasn't seen as too acceptable anyway. No offence intended s ;-)
0
sciuriwareCommented:
CEHJ got only 100 points for his efforts.
Leave it like that.
Another time, another opportunity.

;JOOP!
0
naveenuAuthor 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
0
Mayank SAssociate Director - Product EngineeringCommented:
>> 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
0
naveenuAuthor Commented:
yeah i saw that but who want to work that much when u can have readymade in TextArea()
0
Mayank SAssociate Director - Product EngineeringCommented:
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.
0
Mayank SAssociate Director - Product EngineeringCommented:
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.
0
naveenuAuthor Commented:
:-)
0
sciuriwareCommented:
See? CEHJ deserved A.

But it remains peanuts.

;JOOP!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.