Disable Word wrap in JTextPane - java

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.

Related

How to make a JFrame scrollable?

I would like to scroll through the contents of my JFrame up and down, preferably with a scroll bar. I don't want to wrap the contents inside a JPanel or JScrollPane, because this causes some visual glitches with my application.
Any idea on how to do this?
JScrollPane would be the easiest way; you say there are glitches, but that probably indicates a problem in your code that will still be a problem even without using a JScrollPane.
If you're absolutely set on not using a JScrollPane, you should create a JPanel using BorderLayout, add a JPanel (call it 'center') with BorderLayout.CENTER and layout set to null. Add your content within 'center', and add another JScrollBar to BorderLayout.EAST, add an AdjustmentListener to the JScrollBar. When the adjustmentListener triggers, you need to move your content (Component.setLocation(...)) that's in center to the relative y offset of the JScrollBar and call repaint on 'center'

JTextPane only shows inserted components in single line

I need to insert numbers of JButtons in a JTextPane. It is done via insertComponent() method. but there is a problem, the components are in a line and JScrollPane doesn't scroll neither vertically nor horizontally (however I just want vertically). what should I do?
A simple solution is to add spaces between the buttons, these allow the JTextPane to wrap the buttons.
Alternatively, you can insert a single JComponent and add the buttons as children of that single component. This would allow you to use any layout you like for the buttons (FlowLayout is probably what you want).

Dynamically resize textarea to fit content

Here is a textarea I have put in my program using netbeans. The first image shows what the textarea looks like when I run the program. The second image is show the textarea after I press a button that adds about 50 "Hello world" strings to the text area - only the first 6 get shown. I need the textarea's height to dynamically increase to fit all these strings. Anyone know how to do it?
Edit: I dont want scrollbars so using a JScrollPane is not an option.
I wouldn't use JTextArea for anything except prototyping. It has the bitter functionality of notepad. But it's up to you.
I would use a JEditorPane or JTextPane. I know you can't size it based on character size but that's for the best. For word wrap, you can do setContentType("text/html"); and wrap the text in <'p><'/p> tags.
(Note you still use JScrollPane for scrolling. In fact, the scroll pane works for any component)
See the differences: http://docs.oracle.com/javase/tutorial/uiswing/components/editorpane.html
Try this http://java-sl.com/tip_text_height_measuring.html
I haven't tried it with JTextArea but suppose it should work
Include the JTextArea inside a JScrollPane
JTextArea textArea = new JTextArea();
JScrollPane scrollArea = new JScrollPane(textArea);
This will dynamically change the text area based on if scrolling is needed or not

Text in a JTextField not wrapping around

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.

How is word-wrapping implemented in JTextPane, and how do I make it wrap a string without spaces?

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

Categories