Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

JTextArea to look like JLabel

843805Jan 18 2007 — edited Jan 22 2007
I did do some searching....go easy on me (first posting of a question)

I have a textarea on my frame I use like a status bar.....giving the user hints on what they can/should do based on the applications state. I'd like using the JTextArea because I don't have to concern myself with how long of a string I put in it.....in the worst case the user sees some scrollbars.

I would like to remove the etched look from the text area though. Is that possible? Basically I want a JTextArea that looks visually just like a JLabel. I've tried using JLabel, but when the text to be displayed is dynamic, it makes sizing the label next to impossible. The functionality of the JTextArea seems to work the best, if I can get that etched/raised look supressed.

thanks to all and many thanks to the plethra of info I've found in the forumns already!!!!

JJ

Comments

kirillcool
Call setBorder(new EmptyBorder(0,0,0,0));
843805
kirillg -
that's not what I'm looking for. All EmptyBorder does is create space between the text in the area and the etched border of the area. If you call setBorder() and provide any border available from BorderFactory, it applies that border around the text in the area, but inside of the etched "outline" (I hesitate to call it a border). I need the etched outline to go away....

Would this be easier to obtain with some other JComponent?

I need to display a dynamic string on my frame. The string might be longer then the component I'm using to display it, so I need a component that will automatically handle the wrapping. As far as I can gather, the only way to do that with a JLabel is to use <HTML> in the label text and add <BR>. But I really can't do that with the string cause it's used other places also so I don't want to corrupt it.

HELP! :-)
843805
See in terms of looks you can make a JTextField look like a JLabel by the following ...

JTextField.setEditable(false);
JTextField.setBorder(BorderFactory.createEmptyuBorder(0,0,0,0));
JTextField.setMargin(new Insets(0,0,0,0)); //to make it small

For what you want, you may do this...:

String s= "your string";

JLabel a=new JLabel("<html>"+s+"</html>");
This way you will achieve what you want and also it will an html.
When you do this thing it will automatically wrap the text in the label and the text will come to the next line automatically without any problems. You will not need to add <br>. the text will not get truncated into something like "your..." as you are thinking, when it's html then it wraps text automatically based on the labels horizontal size...that's what my experience has it.

So you may try this....
kirillcool
kirillg -
that's not what I'm looking for. All EmptyBorder
does is create space between the text in the area and
the etched border of the area. If you call
setBorder() and provide any border available from
BorderFactory, it applies that border around the text
in the area, but inside of the etched "outline" (I
hesitate to call it a border). I need the etched
outline to go away....
Hmm, i beg to differ.
import java.awt.FlowLayout;

import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class RoTextField extends JFrame {
	public RoTextField() {
		this.setLayout(new FlowLayout());
		JTextArea textArea = new JTextArea("some text");
		textArea.setBorder(new EmptyBorder(0, 0, 0, 0));
		textArea.setColumns(20);
		textArea.append("\nSome string");
		this.add(textArea);

		this.setSize(300, 100);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public static void main(String[] args) throws Exception {
		// UIManager.setLookAndFeel(new SubstanceLookAndFeel());
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				new RoTextField().setVisible(true);
			}
		});
	}
}
This produces text area without border.
kirillcool
By the way, a thought just hit me - how come you see this etched border at all? Under neither Metal nor Windows LAF it's there. How about a (so-beloved) SSCCE?
794069
I need to display a dynamic string on my frame. The
string might be longer then the component I'm using
to display it, so I need a component that will
automatically handle the wrapping. As far as I can
gather, the only way to do that with a JLabel is to
use <HTML> in the label text and add <BR>. But I
really can't do that with the string cause it's used
other places also so I don't want to corrupt it.
What you display and it's corresponding representation in memory, don't have to be the same. Just as you wouldn't display a double directly in a label (You wouldn't right?), so you can format your value before displaying it - mutliple views on a single model
1 - 6
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Feb 19 2007
Added on Jan 18 2007
6 comments
1,098 views