JEditorPane in a JScrollPane
Hi,
I'm trying to use a JEditorPane, in a JScrollPane, in a JSplitPane. It kind of works, but it doesn't scroll in the way I'd like it to; I'd like it to behave like Notepad, without "Word Wrap" on (although eventually I'd like to make this configurable - is that possible?).
I'm having several problems with this:
- The JEditorPane permenantly expands when I expand the JSplitPane that contains it. This means that if I expand the split pane and then shrink it again, the JEditorPane is suddenly too big to be displayed, and a horizontal scrollbar becomes active at the bottom. Ideally I'd like it to always shrink to fit the size of the split pane that contains it.
- If I type a full line in the editor pane and continue typing, it wraps onto the next line automatically. Ideally in this situation I'd like it to expand to fit the extra text, requiring horizontal scrolling.
Any help would be appreciated.
Thanks,
- Danjb
Re: JEditorPane in a JScrollPane
Every so often, you'll stumble upon something really stupid in Java; this seems to be one of those times.
My immediate suggestion would be to use a JTextArea instead, but since you're using a JEditorPane, I would assume you are trying to do things beyond what JTextArea is capable of.
So, it would seem we have to be creative. I found link that could be useful: "Forced line wrap" and "No wrap" in the JEditorPane
Read the section that says: "No wrap" implementation and tell me if that works.
Re: JEditorPane in a JScrollPane
Thanks for your reply - I'm glad it's not just me being stupid.
I tried following those instructions, though, and got stuck at the first hurdle; "We override ParagraphView class with own implementation of layout()". I don't know what it means by this, as I can find no "layout" method to override.
Re: JEditorPane in a JScrollPane
Does anyone have any insight on this?
Re: JEditorPane in a JScrollPane
Quote:
Originally Posted by
Danjb
Does anyone have any insight on this?
Any chance of seeing an SSCCE that demonstrates what you're talking about?
Re: JEditorPane in a JScrollPane
Quote:
Originally Posted by
KevinWorkman
Any chance of seeing an
SSCCE that demonstrates what you're talking about?
Of course, and thanks for looking into it:
HTML Code:
package gui;
import java.awt.Dimension;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.ScrollPaneConstants;
public class SplitScrollJEditor extends JFrame {
public SplitScrollJEditor() {
// Right Component
JEditorPane textArea = new JEditorPane();
JScrollPane textAreaScrollPane = new JScrollPane(textArea,
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
// Left Component
JPanel panel = new JPanel();
JScrollPane panelScrollPane = new JScrollPane(panel);
panelScrollPane.setMinimumSize(new Dimension(100,100));
// Split Pane
JSplitPane splitPane = new JSplitPane();
splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
splitPane.setDividerLocation(200);
splitPane.setContinuousLayout(true);
splitPane.setLeftComponent(panelScrollPane);
splitPane.setRightComponent(textAreaScrollPane);
setSize(800,600);
add(splitPane);
setVisible(true);
}
public static void main(String[] args) {
SplitScrollJEditor frame = new SplitScrollJEditor();
}
}
Notice if you drag the divider to the right (making the JEditorPane smaller), the JEdiorPane retains its original size and therefore requires horizontal scrolling.
Also, if you type a whole line of text, it will automatically wrap to the next line. I want to make this word wrap entirely optional.
Thanks again,
- Danjb
Re: JEditorPane in a JScrollPane
Alright, I wrestled with this a little, and I'm afraid I don't have a good answer for you. Is there a reason you have to use a JEditorPane instead of a JTextArea? JTextArea seems to work exactly as you describe.
Hopefully somebody smarter than me can figure this out. Here's an even more condensed SSCCE:
Code java:
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class SplitScrollJEditor{
public static void main(String[] args) {
JFrame frame = new JFrame("Text Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//this is the desired behavior
// JTextArea text = new JTextArea();
//why doesn't it work with JEditorPane?
JEditorPane text = new JEditorPane();
frame.add(new JScrollPane(text));
frame.setSize(500, 500);
frame.setVisible(true);
}
}
Make the JFrame wider, then smaller, and the horizontal scroll bars appear. However, if you use the JTextArea instead, it seems to work fine. Argh! I can see why you'd be frustrated. I'm sure there's a reason for this happening, I just don't know it.
Re: JEditorPane in a JScrollPane
Hehe, thanks again for looking into it anyway. I might just use a JTextArea, at least for now, but eventually I'd like to be able to display slightly more elaborate documents...
If anyone can suss this out, that would be much appreciated!
Re: JEditorPane in a JScrollPane
Do something for debug purposes. Get the dimensions of the JEditorPane before and after the divider is moved. If they are the same, it means the viewing window changed but the JEditorPane itself has not. To fix it, maybe you could just resize the JEditorPane.
Re: JEditorPane in a JScrollPane
Quote:
Originally Posted by
KevinWorkman
Hopefully somebody smarter than me can figure this out.
I don't think this makes me smarter than you :), but in your SSCCE the scrollbar appears whenever you decrease the viewport width because JEditorPane overrides getPreferredSize() to do this. The API docs say:
"The preferred size for JEditorPane is slightly altered from the preferred size of the superclass. If the size of the viewport has become smaller than the minimum size of the component, the scrollable definition for tracking width or height will turn to false. The default viewport layout will give the preferred size, and that is not desired in the case where the scrollable is tracking. In that case the normal preferred size is adjusted to the minimum size. This allows things like HTML tables to shrink down to their minimum size and then be laid out at their minimum size, refusing to shrink any further."
The JEditorPane minimum width seems to track the current viewport width as it increases (I'm not sure why), so any decrease in viewport width always makes it smaller than the minimum width of the JEditorPane, thus triggering this behaviour.
You can stop this behaviour by overriding JEditorPane.getScrollableTracksViewportWidth() to always return 'true', but this will obviously interfere with the intended behaviour of the JEditorPane in certain situations.
Re: JEditorPane in a JScrollPane
Hey,
Thanks for all the info guys - for now I'm using a JTextArea until I can get some other problems sorted out but this will come in very handy later!
- Danjb