• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 476
  • Last Modified:

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
0
AntoniRyszard656
Asked:
AntoniRyszard656
  • 5
  • 4
  • 2
  • +1
2 Solutions
 
CEHJCommented:
Try

label.setText("<html>Line one<br>Line 2</html>");
0
 
objectsCommented:
Try using a JTextArea with line wrap on.

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 SAssociate 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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

  • 5
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now