Reputation: 14049
I am using a JEditorPane as an editor to write comments in my application. The content type is set "text/plain". When I am writing text in it and the text fills the available space and I go on typing, the text is not moving upward to show the cursor. So I dont know where I am typing and what I am typing since it would be visible.
Could you tell me how to always show the caret by moving the above text upwards?
Instead, it could be better if I can auto-resize the editor as I am typing. The JEditorPane is inside a JPanel, so I have to resize that too. any Ideas?
Upvotes: 0
Views: 4045
Reputation: 2224
Edited to add full solution
You have to add a JScrollPane first. Then, if you don't want the scrollbars to be visible, but you want the text area to autoscroll, set
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
on the scrollpane. This will hide the scrollbars, but provide you with autoscrolling.
Here's how you'd implement auto scrolling with scroll pane, and automatic resize upto a given maximum.
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
public class SPTest extends JFrame {
private static final long serialVersionUID = 1L;
private JEditorPane editor;
private JScrollPane scrollPane;
private JPanel topPanel;
private JLabel labelTop;
public SPTest() {
super("Editor test");
initComponents();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
private void initComponents() {
editor = new JEditorPane("text/plain", null);
scrollPane = new JScrollPane(editor);
topPanel = new JPanel();
labelTop = new JLabel("main contents here");
topPanel.add(labelTop);
setSize(600, 400);
editor.setMinimumSize(new Dimension(100, 30));
editor.setPreferredSize(new Dimension(100, 60));
scrollPane.setPreferredSize(new Dimension(600, 60));
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
scrollPane.setMinimumSize(new Dimension(100, 30));
final int MAX_HEIGHT_RSZ = 120;
editor.addCaretListener(new CaretListener() {
public void caretUpdate(CaretEvent e) {
int height = Math.min(editor.getPreferredSize().height, MAX_HEIGHT_RSZ);
Dimension preferredSize = scrollPane.getPreferredSize();
preferredSize.height = height;
scrollPane.setPreferredSize(preferredSize);
SPTest.this.validate();
}
});
setLayout(new BorderLayout());
add(topPanel, BorderLayout.NORTH);
add(scrollPane, BorderLayout.SOUTH);
}
public static void main(String[] args) {
new SPTest();
}
}
You could resize You can use this JScrollPane instead of the JPanel as the container for the editor.
Upvotes: 1
Reputation: 103145
You need to put the editor inside a JScrollPane. The ScrollPane will automatically add scrollbars and remove the need to resize the editor.
Upvotes: 4