JLabel with several text lines?

Hello,

I trying to display a book title in some swing object. The title will vary in string length.

If I used a jlabel, should I be-able to set two lines in the label so when the first line is filled, the extra text would use the second line?

Or is there some other swing object you could recommend?

Thanks
AntoniRyszard656Asked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
Try using a JTextArea with line wrap on.

0
 
CEHJCommented:
Try

label.setText("<html>Line one<br>Line 2</html>");
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> I be-able to set two lines in the label so when the first line is filled, the extra text would use the second line

If you want to use the HTML approach, then you would have to put the <br> off-hand, which might not necessarily be at the place where it is needed (as per the length of the text). Since you need it to change dynamically based on the text which you put into the JLabel, you can try JTextArea, but it would not look like a label :)

You can try this: Since JLabel extends Component, you can add a layout-manager to the JLabel and then add a JTextPane to it, set the background of the JTextPane to what is that of the JLabel (or make it opaque).

JLabel label = new JLabel () ;
label.setLayout new BorderLayout () ;
JTextPane pane = new JTextPane () ;
pane.setBackground ( label.getBackground () ) ; // or make it opaque
pane.setText ( "Your text goes here" ) ;
label.add ( pane, BorderLayout.CENTER ) ;
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
AntoniRyszard656Author Commented:
Thanks

Could anyone demonstrate how to use a JTextField and the line wrap function, for two lines.

Would be easier to use the JTextArea?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
The JTextField will not take multiple lines. You would need to use a JTextArea.
0
 
AntoniRyszard656Author Commented:
The code would be something like:

JTextArea textComp = new JTextArea(2, 40);

textComp.setText("Sams Teach Yourself Java 2 in 21 Days");
textComp.setlinewrap(true);
0
 
Mayank SConnect With a Mentor Associate Director - Product EngineeringCommented:
Yes, that should work.

http://www.javaalmanac.com/egs/javax.swing.text/ta_Wrap.html

>> setlinewrap

I hope you meant: setLineWrap
0
 
AntoniRyszard656Author Commented:
Is there a function to stop the textarea from being editable?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
setEditable ( false ) ;
0
 
AntoniRyszard656Author Commented:
Thanks
0
 
CEHJCommented:
Here's a custom MultiLineLabel written by renowned Java author David Flanagan:

http://www.oreilly.com/catalog/javanut/examples/section5/MultiLineLabel.java

0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> Thanks

Does this mean the question is closed ;-) ?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.