Discussions
Categories
- 197K All Categories
- 2.5K Data
- 546 Big Data Appliance
- 1.9K Data Science
- 450.8K Databases
- 221.9K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 552 MySQL Community Space
- 479 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.1K ORDS, SODA & JSON in the Database
- 556 SQLcl
- 4K SQL Developer Data Modeler
- 187.2K SQL & PL/SQL
- 21.4K SQL Developer
- 296.3K Development
- 17 Developer Projects
- 139 Programming Languages
- 293K Development Tools
- 110 DevOps
- 3.1K QA/Testing
- 646.1K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 158 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.2K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 19 Java Essentials
- 162 Java 8 Questions
- 86K Java Programming
- 81 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 205 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 468 LiveLabs
- 39 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 175 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 233 Portuguese
Disabling word wrap for JTextPane
I'm working on an extension of JTextPane, and I need to disable word wrap for it, is there anyway to do it? Please don't say just use JTextArea, it's not an option available to me. Any reply would be VERY appreciated.
Comments
-
Please don't say just use JTextAreaOk. Then how about trying to search the forum first before posting questions. The following keywords, taken from your topic title, will lead to some hits "jtextpane word wrap".
-
Ok, so I've looked at all the suggested solutions for disabling wrapping for JTextPane. These are the general solutions I found:
1. Putting TextPane inside a JPanel, then adding the JPanel inside a JScrollPane
2. Extending the TextPane class and override the getScrollableTracksViewportWidth() and getSize() methods.
However, both solutions doesn't handle the case where you tab several times, then type text with occational spaces until the horizontal scrollbar show up, then press return. Either the text will wrap as soon as you press return, or wrap when you keep on typing. Is there any 'perfect' solutions? -
Is there any 'perfect' solutions?I created this posting 3 years ago and still haven't seen a better answer posted:
http://forum.java.sun.com/thread.jspa?forumID=57&messageID=1322943
I hope you find a better solution. If you do don't forget to post the code so we can all benefit. -
I've posted the solution several times. Override EditorKit you use. Replace the layout() method of SectionView (view for root element) with something like this:
public void layout(int width, int height) {
super.layout(Integer.MAX_VALUE,height);
}
and you'll never see word wrap.
regards,
Stas -
Thanks for replying, StanislavL. I'm not sure what you meant by SectionView, i can't seem to find it in the java class list. Can you give some specifc code? I appreciate your help!
Frank -
The view is different for different EditorKits.
For HTMLEditorKit it's BlockView
For StyledEditorKit it's just BoxView with Y_AXIS.
regards,
Stas -
Stas,
I think you are the only one in the forum who understands how views are painted. Any way, I tried to implement your suggeston using code examples I've found from you in the past and came up with the following:import javax.swing.*; import javax.swing.text.*; public class NoWrapEditorKit extends StyledEditorKit { public ViewFactory getViewFactory() { return new StyledViewFactory(); } static class StyledViewFactory implements ViewFactory { public View create(Element elem) { String kind = elem.getName(); if (kind != null) { if (kind.equals(AbstractDocument.ContentElementName)) { return new LabelView(elem); } else if (kind.equals(AbstractDocument.ParagraphElementName)) { return new ParagraphView(elem); } else if (kind.equals(AbstractDocument.SectionElementName)) { return new NoWrapBoxView(elem, View.Y_AXIS); } else if (kind.equals(StyleConstants.ComponentElementName)) { return new ComponentView(elem); } else if (kind.equals(StyleConstants.IconElementName)) { return new IconView(elem); } } return new LabelView(elem); } } static class NoWrapBoxView extends BoxView { public NoWrapBoxView(Element elem, int axis) { super(elem, axis); } public void layout(int width, int height) { super.layout(1024, height); } } public static void main(String[] args) throws Exception { JTextPane textPane = new JTextPane(); textPane.setEditorKit( new NoWrapEditorKit() ); JFrame frame = new TextPaneNoWrapView(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add( new JScrollPane(textPane) ); frame.setSize(200, 200); frame.setLocationRelativeTo(null); frame.setVisible(true); } }
Now, enter two "tabs" and then start typing "one two three four five...". The text never wraps (good), but the horizontal scrollbar never appears either (bad). So I guess the question becomes, how to you prevent the text from wrapping and show the horizontal scrollbar when required.
Next, I tried to retest the suggestions I gave in the above link.
Start by clearing all the text from the text pane.
Now, enter two "tabs" and then start typing "one two three four five...". The text won't wrap and the horizontal scrollbar appears (good).
Now, use the enter key to create a new line, the text wraps (bad).
Enter text on the new line, the text unwraps (good)
Why does the text wrap/unwrap when a new line is added.
Next I tried adding the NoWrapEditorKit to my example code:
Start by clearing all the text from the text pane.
Now, enter two "tabs" and then start typing "one two three four five...". The text won't wrap and the horizontal scrollbar appears (good).
Now, use the enter key to create a new line, the horizontal scroll bar disappears (bad).
Enter text on the new line, the horizontal scrollbar reappears (good)
Again, why does the scrollbar disappear/reappear? -
I tried doing what you suggested. Here's the code i wrote..
import javax.swing.text.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.basic.BasicTextUI;
public class MyEditorKit extends StyledEditorKit {
private static final ViewFactory defaultFactory = new MyViewFactory();
public ViewFactory getViewFactory() {
return defaultFactory;
}
static class MyViewFactory implements ViewFactory {
public View create(Element elem) {
String kind = elem.getName();
if (kind != null) {
if (kind.equals(AbstractDocument.ContentElementName)) {
return new LabelView(elem);
} else if (kind.equals(AbstractDocument.ParagraphElementName)) {
return new ParagraphView(elem);
} else if (kind.equals(AbstractDocument.SectionElementName)) {
return new MyView(elem, View.Y_AXIS);
} else if (kind.equals(StyleConstants.ComponentElementName)) {
return new ComponentView(elem);
} else if (kind.equals(StyleConstants.IconElementName)) {
return new IconView(elem);
}
}
// default to text display
return new LabelView(elem);
}
}
static class MyView extends BoxView {
public MyView(Element elem, int axis) {
super(elem, axis);
}
public void layout(int width, int height) {
super.layout(Integer.MAX_VALUE,height);
}
}
public static void main(String args[]) {
JFrame f = new JFrame();
JTextPane t = new JTextPane();
t.setEditorKit(new MyEditorKit());
JScrollPane l = new JScrollPane(t);
f.setSize(600, 400);
f.getContentPane().setLayout(new BorderLayout());
f.getContentPane().add(l);
f.setVisible(true);
}
}
What comes up is a JFrame with an editorpane that doesn't respond to anything i do. If i comment out the layout() method in MyView, then everything works like a regular editorpane again, so I'm sure it's the layout method that's screwing things up. Any suggestions?
Frank -
One way to make the horizontal srcrollbar appear is to force the minimum span of the X axis of NoWrapBoxView to certain value, i.e.
public float getMinimumSpan(int axis) { if (axis == View.Y_AXIS) { return super.getMinimumSpan(axis); } else { return 1024f; } }
-
The scrollpane gets size according to minimum span.
Actually I don't know why it fails when size more than 32768... I expected that to work with Integer.MaxValue.
but I think that's enough for most cases.
regards,
Stasstatic class NoWrapBoxView extends BoxView { public NoWrapBoxView(Element elem, int axis) { super(elem, axis); } public void layout(int width, int height) { super.layout(32768, height); } public float getMinimumSpan(int axis) { return super.getPreferredSpan(axis); } }
This discussion has been closed.