[java] JLabel - new line

Started by
5 comments, last by drackill 18 years ago
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.
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 :)
~Mark Schadler
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
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
a.k.a javabeats at yahoo.ca
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


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);    }}


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


This topic is closed to new replies.

Advertisement