Jump to content
  • Advertisement
Sign in to follow this  
drackill

[java] JLabel - new line

This topic is 4501 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I've been playing around with Swing for the first time and I'm just trying different things out at the moment. One problem I'm having is I want a JLabel to display a bunch of information but unfortunately I can't get it to go to a new line. I've been using Jlabel rather than anything else due to the fact it can’t be edited when running. Sorry for the rather newbish question but I'm stumpt after having no luck on google or the Java documentation Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
I don't know if there's a way around that since I don't really do java all that often, but you could just use a "JTextField" and set the editable boolean to false. Sorry if that isn't what you where looking for :)

Share this post


Link to post
Share on other sites
Quote:
Original post by Stiffler
I don't know if there's a way around that since I don't really do java all that often, but you could just use a "JTextField" and set the editable boolean to false. Sorry if that isn't what you where looking for :)


Cool thanks squire. As I'm new to Swing I didn't know JTextField had a way to disable it being editable. I'll give that ago. Would be interesting to know if there is a way to make a new line on a JLabel.

Thanks matey

Share this post


Link to post
Share on other sites
Changing its size to contain more than one line, and then using "\n" as an escape character, or html tags that break lines.

Son Of Cain

Share this post


Link to post
Share on other sites
Quote:
Original post by Son of Cain
Changing its size to contain more than one line, and then using "\n" as an escape character, or html tags that break lines.

Son Of Cain


Thank you :D I shall give that a try now. Cheers


Share this post


Link to post
Share on other sites
Quote:
Original post by drackill
Hello, I've been playing around with Swing for the first time and I'm just trying different things out at the moment.

One problem I'm having is I want a JLabel to display a bunch of information but unfortunately I can't get it to go to a new line. I've been using Jlabel rather than anything else due to the fact it can’t be edited when running.


What you want is JTextArea or JEditorPane. Call setEditable(false) to make the uneditable. The latter can be set up to allow display of simple HTML so you can easily do fomratting of the displayed text. Oh, you'll also probably want to enable word wrap if you use JTextArea.

I often use a simple extesion to JTextArea to give me a multi-line label. Something like (depending upon the needs of the project):


import java.awt.Container;
import javax.swing.JLabel;
import javax.swing.JTextArea;

public class MultiLineLabel extends JTextArea {

public MultiLineLabel(Container parent, String text,
int maxWidth, boolean labelFont) {
super();
JLabel lbl = new JLabel();
setFocusable(false);
setEditable(false);
setBackground(lbl.getBackground());
setLineWrap(true);
setWrapStyleWord(true);
if (labelFont) {
setFont(lbl.getFont());
}
setHighlighter(null);
if (maxWidth > 0) {
setColumns(Math.min(maxWidth, text.length()));
}
append((text == null || text.length() == 0) ? " " : text);
}

public MultiLineLabel(Container parent, String text) {
this(parent, text, 0, true);
}

public MultiLineLabel(Container parent, String text, boolean labelFont) {
this(parent, text, 0, labelFont);
}

public MultiLineLabel(Container parent, String text, int maxWidth) {
this(parent, text, maxWidth, true);
}
}



Share this post


Link to post
Share on other sites
Quote:
Original post by sindisil
Quote:
Original post by drackill
Hello, I've been playing around with Swing for the first time and I'm just trying different things out at the moment.

One problem I'm having is I want a JLabel to display a bunch of information but unfortunately I can't get it to go to a new line. I've been using Jlabel rather than anything else due to the fact it can’t be edited when running.


What you want is JTextArea or JEditorPane. Call setEditable(false) to make the uneditable. The latter can be set up to allow display of simple HTML so you can easily do fomratting of the displayed text. Oh, you'll also probably want to enable word wrap if you use JTextArea.

I often use a simple extesion to JTextArea to give me a multi-line label. Something like (depending upon the needs of the project):


:D Cheers I think I'll defo go with the setEditable(false). Thanks for taking the time out to post all that :) Makes more sense now.

Cheers


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!