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.4K Development
- 17 Developer Projects
- 139 Programming Languages
- 293.1K Development Tools
- 111 DevOps
- 3.1K QA/Testing
- 646.1K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 161 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
- 475 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
Comments
-
I tried adding the getMinimumSpan code to the class I posted above. It still doesn't work 100 percent.
Type in the following data
a) two tab characters
b) "one two three four..." until the scroll bar appears
c) then use the enter key to add a new line (the scrollbar disappears)
d) start typing on the new line and the scrollbar will reappear
The view has problems when tab characters are found in a preceeding line.
This is the same basic problem with the suggestion I provided 3 years ago. -
After a grueling 3 days of looking at swing code, I've finally found the solution!!!
check the following code out. No need to set the bounds to any specific size, will work for any line length, well, reasonablly long. No problems with tabs or newlines either. The only thing I can't think of solving is the caret not being visible when editing the right most part of the text area. Any suggestions?
//NoWrapTextPane.java
import java.awt.Dimension;
import javax.swing.JTextPane;
import javax.swing.text.EditorKit;
public class NoWrapTextPane extends JTextPane {
public boolean getScrollableTracksViewportWidth() {
//should not allow text to be wrapped
return false;
}
public void setSize(Dimension d) {
//dont let the Textpane get sized smaller than its parent
Dimension pSize = getParent().getSize();
if (d.width < pSize.width) {
super.setSize(pSize.width, d.height);
} else {
super.setSize(d);
}
}
protected EditorKit createDefaultEditorKit() {
return new MyEditorKit();
}
}
//MyEditorKit.java
import javax.swing.text.*;
public class MyEditorKit extends StyledEditorKit {
private static final ViewFactory defaultFactory = new MyViewFactory();
public ViewFactory getViewFactory() {
return defaultFactory;
}
public static class MyViewFactory implements ViewFactory {
public View create(Element elem) {
String kind = elem.getName();
if (kind != null) {
if (kind.equals(AbstractDocument.ContentElementName)) {
return new MyView(elem);
} else if (kind.equals(AbstractDocument.ParagraphElementName)) {
return new ParagraphView(elem);
} else if (kind.equals(AbstractDocument.SectionElementName)) {
return new BoxView(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 MyView(elem);
}
}
public static class MyView extends LabelView {
public MyView(Element e) {
super(e);
}
public float getTabbedSpan(float x, TabExpander e) {
float result = super.getTabbedSpan(x, e);
this.preferenceChanged(this, true, false);
return result;
}
}
} -
To camickr.
I investigated this a little more. Somehow the TabExpander is null but in all editor kits ParagraphView implements TabExpander. So I added a check if the expander==null use parent ParagraphView as expander.
That's a simple implementation of LabelView. I used 0 instead of real X of label (last parameter). We can also override gettabExpander() of LabelView to return parent paragraph if the expander isn't set.
regards,
Stasstatic class MyLabelView extends LabelView { public MyLabelView(Element elem) { super(elem); } public float getPreferredSpan(int axis) { float span=0; if (axis==View.X_AXIS) { int p0 = getStartOffset(); int p1 = getEndOffset(); checkPainter(); TabExpander ex=getTabExpander(); if (ex==null) { //paragraph implements TabExpander ex=(TabExpander)this.getParent().getParent(); } span = getGlyphPainter().getSpan(this, p0, p1, ex, 0); return Math.max(span, 1); } else { span=super.getPreferredSpan(axis); } return span; } }
-
To battbot.
That's really bad to call preferenceChanged in the getting tabbed span.
That means every time when you measure your label you relayout all content. I think the component will be really slow.
regards,
Stas -
I think your solution is better Stas. Thanks alot!! Now if you can also help me figure out how to make the cursor show up when it's all the way to the right, then we will have a workable JTextPane on our hands.
Frank -
Oh btw Stas, in which cases are the tab expansion based on the x position? Will setting the x position to 0 rather than it's true position cause any unwanted side-effects?
Frank -
Now if you can also help me figure out how to make the cursor show up when it's all the way to the right,This is also the default behaviour when you use a JTextArea that does not wrap, so it may not be as easy to fix.
But I'm thinking you would play with the span of every line and add a few pixels to the calculated amount to allow for the caret to be painted. -
It's better init tab expander and use super method.
Like this.
As for cursor If you scroll slightly to the right the caret becomes visible. At first glance we can just scroll one-two pixels to the right...
Need more investigations... ot you can try to add a few pixels to right inset of SectionView.
regards,
Staspublic float getPreferredSpan(int axis) { if (axis==View.X_AXIS) { TabExpander ex=getTabExpander(); if (ex==null) { //paragraph implements TabExpander ex=(TabExpander)this.getParent().getParent(); getTabbedSpan(0, ex); } } return super.getPreferredSpan(axis); }
-
Thanks for all the responses Stas and Camickr. You get the duke dollars Stas, thnx for the help.
Frank -
Hi.
Is there a way to overwrite the layout method in BlockView class without overwriting the create method in ViewFactory class?
Thnx.
This discussion has been closed.