JTextPane - dynamic word wrap - java

I tried to use jTextPane1.setText("xxx xxxx xxx xxxxx xx xxx xxxx xxx etc..."); but JTextPane does not word wrap it at all showing all the text in one line only instead.
It would be interesting to support word wrap on jTextPane1 resized too...
So my question is...
how to make JTextPane support word wrap?

Try using a JTextArea and call setWrapStyleWord(true); on its instance this should do what you need.
EDIT:
If you need to use a JTextPane as a requirement(which you said you do), then have a look at a similar question that I found which answer should be of help: How is word-wrapping implemented in JTextPane, and how do I make it wrap a string without spaces?

I had the same problem, the solution from David Kroukamp was very helpful. I changed it to JTextArea and set the following properties, as described in this tutorial:
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);

Why not use a JTextArea instead of a Pane?
http://docs.oracle.com/javase/1.5.0/docs/api/
public void setWrapStyleWord(boolean word)
Sets the style of wrapping used if the text area is wrapping lines. If set to true the lines will be wrapped at word boundaries (whitespace) if they are too long to fit within the allocated width. If set to false, the lines will be wrapped at character boundaries. By default this property is false. Parameters: word - indicates if word boundaries should be used for line wrapping See Also: getWrapStyleWord()

Related

How do I wrap text in a jTextPane?

I'm currently using a jTextArea, and that has a setLineWrap() method to indicate whether text should be wrapped or not. Do jTextPanes have an equivalent to this? I know it can wrap words, but I need it to wrap characters.
For example, "oooooooooooo..." should be wrapped and displayed as two or more lines of o's instead of one long line with a scroll bar. So is this possible with jTextPanes?
JTextPanes word wrap automatically, you have to set the size.
jTextPane.setSize(someValue);
The size should be set to something small.
But this doesn't help you because it doesn't do that with characters... Try using <br> maybe? Or check this link: http://java-sl.com/wrap.html

How can I create a new line in JTextField to type in?

I want to make a Swing program that writes updates on what you have done. It writes all the information on an uneditable JTextField.
For example:
You have changed the text to BLUE.
You have changed the text to RED.
The problem is that I can't make the two lines separate.
What I get is this:
You have changed the text to BLUE. You have changed the text to RED.
What I've tried is this: (This does not work)
TF_BetInfo.setText(TF_BetInfo.getText() + "\nYou have changed the text to BLUE.");
TF_BetInfo.setText(TF_BetInfo.getText() + "\nYou have changed the text to RED.");
you can't have multiple lines with JTextField , you can use JTextArea for that purpose , and the code wouldn't be much different too , you can still use the same code or you can just
TF_BetInfo.append("\nyou have ...... ");
where TF_Betinfo is a JTextArea rather than a JTextField .
You can't use JTextField for this. JTextField is sinle-line edit control. That is why you got all your output in one line.
If you want several lines to be printed in edit use JTextArea. In your case you can use jTextArea.append(string) (note: jTextArea is an object of class JTextArea, and string is an object of class String).
You can't actually use several lines in a JTextField, but what you can do, is using a JTextArea of the wanted size, and then use the following:
yourJTextArea.setLineWrap(true);
This will automatically detect when the JTextArea needs to use another line and add it, pretty cool, useful and you only need one code line to do it.
JTextArea is more flexible but if you really want flexibility read about JTextPane or JEditorPane, you can show URLS, internet pages and everything that comes to your mind.

Swing Component with automatic line wraping and limited width with HTML support

I want to implement a small tooltip with a scrollbar like the one in eclipse that appears when hovering above a class or member.
The problem I have is finding a way to limit only the width but not the height of a component within the scroll pane I have inside my tooltip. The component should support HTML and also wrap the text correctly when it exceeds the width of the inner bounds, but all components I have tried out have either line wrapping or HTML rendering, but not both
A way to limit only width is also nowhere to be found as every "setXSize" where X is "preferred" "max" "min" etc. all require two arguments and there is no "setMaxWidth" method for components.
Setting "setMaximumSize(new Dimension(256, Integer.MAX_VALUE);" would seem like a solution but it doesnt work as parameters set by "max" and "min" are ignored most of the time which is quite frustrating.
On request a current example of the implementation:
public class MyTooltip extends JTooltip{
private JScrollPane scroll;
private JEditorPane pane;
public MyTooltip(String htmlCode){
this.pane = new JEditorPane();
this.scroll = new JScrollPane(this.pane);
this.pane.setEditable(false);
this.pane.setContentType("text/html");
this.scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
//Here the problems begin
this.pane.setMaximumSize(new Dimension(512, Integer.MAX_VALUE));
this.scroll.setMaximumSize(new Dimension(512, Integer.MAX_VALUE));
this.pane.setText(htmlCode);
this.add(scroll);
}
}
the actual code is a bit more complex ofc. but I think this is a good approximation ...
Have you tried JTextPane with HTMLEditorKit (content type text/html)?
I think that's what you need.
Ok, the whole problem just solved itself: One had the idea to let the user write his own texts to be displayed on the tooltips, but that included letting him use multiple "spaces" for indentation when he called for it.
To let HTML render this as intended we replaced every "space" with an & nbsp; so no optimization on gaps between characters would be performed, but this of course has the result that no algorithm for automatic line wrapping would accept any "gap" between words as a suitable place to break the line.
So our implementation actually works as intended, only the Strings we give the tooltip to display are not suitable to be line broken.

JTextField with styled blocks

I need some kind of JTextField that replace carriage return characters by a styled block displaying the text <New Line> with another font, style or color.
By example, the text..
Dear Mr. Smith,\nblablabla...\nRegards...
..would be shown to the user, in a one line JTextField, this way :
Dear Mr. Smith,<New Line>blablabla...<New Line>Regards...
These <New Line> should be blocks that are selectable as if it was a single character and could be deleted with a single backspace. Pressing the return key would add a <New Line> at the caret position.
I think it can be possible by overriding PlainDocument, but I'm stuck in difficult to understand Document and EditorKit APIs. I would need some pointers about this one...
I do not have any objection using a JTextPane or JEditorPane as long as it behaves like a JTextField (one line, no scroll-bars).
Any suggestions?
Any suggestions ?
Use a JTextArea (a multi-line component) instead.
For an example, see this answer:
It would be simpler, but this is not what I have to do...
Fair enough, I missed the part that stated:
..with another font, style or color.
That would require a styled document such as JEditorPane or JTextPane.

In java, how do I add line breaks to a JButton's tooltip?

I have a JButton that uses setToolTipText(String str) and I have a few lines of writing in the tooltip that need to to be separated by line breaks. I tried concatenating + "/n" at the end of each line of text right before I wanted the line break to go, but it did not do anything. JToolTip text doesn't have a constructor with a JTextArea and I couldn't see anything in JTextArea that allowed it to convert to a String. Anyone have any ideas?
Also, how do I get a JToolTip to last longer than the default 3 seconds?
Thank you for your time.
Use HTML.
Set the tooltip to "<html>My multi-line<br>tooltip.</html>"

Categories