My original plan today was to learn how to use the JProgressBar, but now I am stuck with a new problem which I was not expecting. So here is how my JFrame looks like right now.
The problem is that the JTextField right under the JTextArea is not wrapping around. It is going beyond the size of the JFrame. I want it to resize relative to the JFrame. How can i do that? I have tried the JLabel but it does the same thing, only it adds ellipses in the end where the text overflows.
Try using a JTextArea if you intend for the field to expand vertically to accommodate the text.
JTextFields are intended for a single line of text.
Related
It's been awhile since I asked a question!
I'm developing an application in Java where JLabels are used. Anyway, as you may or may not be able to see from the picture below, the text that says Category Test gets cut off and ends up saying Categor... instead. Is there any way that I can "break" the text once it fills up the width of the label? Here is the image:
What I did
I used a JTextPane like so:
JTextPane text = new JTextPane();
SimpleAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setAlignment(attr, StyleConstants.ALIGN_CENTER);
pane.add(text, c);
Then I added my text, but thanks anyway!
JLabels can't do that by default. But JLabels have some support for html, so a JLabel with the text <html>First Line<br />Second Line</html> would show up on two lines.
If you want a component that can split the lines by itself, take a look at JTextArea.
As I recall, you need to use a JTextArea if you want textwrap. JLabel doesn't do it.
You can at StyledLabel component from JIDE Common Layer open source project at http://java.net/projects/jide-oss/.
The problem with html JLabel approach is it doesn't auto-wrap and about 20 to 40 times slower than a plain JLabel.
The problem with JTextArea or JTextPane approach is it has a weird size issue and is also 20 times slower.
StyledLabel extends JLabel. Automatically line wrapping is just one of the many features it adds. And the performance is as fast as a plain JLabel.
Hope it will help.
I hope I did not miss some duplicate question, I feel like this should be trivial!
Anyway, I have a JTextArea with text in it, with automatic line wraps:
public PleaseResize(){
super();
Container cp = this.getContentPane();
JTextArea area = new JTextArea();
area.setColumns(20);
area.setLineWrap(true);
area.setEditable(false);
area.setWrapStyleWord(true);
area.setText("Once upon a midnight dreary, while I pondered, weak and weary, over many a quaint an curious volume of forgotten lore.");
cp.add(area, BorderLayout.CENTER);
cp.add(new JButton("Hallo"), BorderLayout.SOUTH);
this.pack();
}
I want the text area to resize vertically to display the whole text... but it does not. It resizes happily if I choose the line breaks via pushing in \n, but with automatic wrapping, it just remains the size it is.
I somehow feel like I am missing something totally obvious...
Edit: for clarification:
On creation-time, I do not know how many lines the text will have (due to the automatic linebreaks happening). I am receiving Text via an XML-file, and it can vary between 0 and about 30 lines after wrapping. I have the vertical space to display everything, but I do not want to scroll or to have a huge, white area when there is no or only a little text to display.
Edit 2:
After rephrasing the question, they key to the problem I was facing was apparently not resizing the JTextArea, but making sure the dialog knows how big it is!
So, here is the link back to the solution I ended up using: An automatic resizing Text above a button?
You need to wrap JTeatArea with JScrollPane like next new JScrollPane(area); and then add JScrollPane to your JFrame.
Also you create JTextArea with empty constructor, use JTextArea(int rows, int cols) to specify count of rows and columns.
I need to be able to select the text in a JLabel. Ive read some guides on the net that talk about using a JTextfield to simulate a JLabel, however this is no use to me as I my JLabel will span multiple lines. So any ideas on how to do this or if it's even possible?
You can use a non-editable JTextArea which allows for multiple lines and selectable text, and visually looks the same as a JLabel.
I do not think it is possible with a JLabel (at least I do not know how)
How exactly is word-wrapping implemented in JTextPane?
I'm trying to understand exactly how it works so that I can modify the behavior. Right now, if I have a standard JTextPane inside a JScrollPane, it will break text at spaces, but not inside long words - if there is a string of text without spaces that is wider than the window, it won't wrap/break and a horizontal scrollbar will appear. As the text width increases, the width of the ParagraphView (via getWidth()) increases to hold the text.
This article by Lapitsky says that LabelView.getBreakWeight() returns View.ExcellentBreakWeight for labels with spaces and View.GoodBreakWeight for labels without spaces (and the code in GlyphView.java seems to confirm this), so why doesn't it break? Is it somehow returning BadBreakWeight instead of GoodBreakWeight? Or is there some layout problem? Or is there a bug?
Here's some code (for your viewing pleasure):
//somewhere inside JPanel or JFrame constructor
JTextPane textPane = new JTextPane();
JScrollPane scrollPane = new JScrollPane(textPane);
add(scrollPane);
Note that it still doesn't wrap if I take out the scroll pane and just use the text pane (it just gets clipped as it goes outside the window).
The javadocs for Swing don't seem to go into enough detail on how some objects (like JTextPane, View's, and related objects) work together. Is there any further documentation of the design of such classes, perhaps detailing the purpose of each class and how they all work together? Is it just not publicly available? (Or am I the only one having trouble with things like this? Or is the insufficient documentation limited to things which aren't expected to be dealt with by a typical developer?)
The link about custom wrap (forced wrap and no wrap).
http://java-sl.com/wrap.html
The link about letter wrap
http://java-sl.com/tip_html_letter_wrap.html
I can't find an easy way to turn off word wrap in a JTextPane. I can't use JTextArea because I need different colors for different text. I have these big ugly lines that get uglier with word wrap turned on.
JTextArea has a setLineWrap() method, but I can't find it for JTextPane. Why?
Okay, I found an easy solution. Put the JTextPane into the center of a JPanel with a Border layout. Then, put the JPanel into a JScrollPane.
So, the hierarchy looks like this:
JScrollPane
JPanel (w/ Border Layout)
JTextPane
JScrollPane contains everything below it and the JTextPane is inside of everything above it.
I'm not sure why this works, but it does.