Sign in to follow this  
drackill

[java] JLabel - new line

Recommended Posts

drackill    128
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
Stiffler    163
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
drackill    128
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
drackill    128
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
sindisil    388
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
drackill    128
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this