I have a JTabbedPane
which two tabs including two JScrollPane
.
Each JScrollPane
is including a JEditorPane
. If there is no image in the editor, there is no problem. When there is an image, the scroll does not go down to follow the caret position for the first editor, it's like the image gains always the caret beside it after loading at the end of loading the editor..
Here is my code:
public class CaretPositionForJEditorPane {
JTabbedPane tabs;
JScrollPane scrollPane1;
JEditorPane editor1;
JEditorPane editor2;
public CaretPositionForJEditorPane() throws IOException {
tabs = new JTabbedPane();
editor1 = new JEditorPane();
editor1.setContentType("text/html");
editor1.setText(text);
scrollToBottom();
scrollPane1 = new JScrollPane(editor1);
tabs.addTab("tab1", scrollPane1);
editor2 = new JEditorPane();
JScrollPane scrollPane2 = new JScrollPane(editor2);
tabs.addTab("tab2", scrollPane2);
tabs.addChangeListener((ChangeEvent e) -> {
switchTabs();
});
tabs.setFocusable(false);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(tabs, BorderLayout.CENTER);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.getContentPane().add(panel);
frame.setVisible(true);
}
@SuppressWarnings("ResultOfObjectAllocationIgnored")
public static void main(String args[]) {
SwingUtilities.invokeLater(() -> {
try {
new CaretPositionForJEditorPane();
} catch (IOException ex) {
Logger.getLogger(CaretPositionForJEditorPane.class.getName()).log(Level.SEVERE, null, ex);
}
});
}
private void switchTabs() {
if (tabs.getSelectedIndex() == 0) {
editor1.setText(editor2.getText());
scrollToBottom();
} else {
editor2.setText(editor1.getText());
}
}
private void scrollToBottom() {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
// ******** tried this without success
// JScrollBar bar = scrollPane1.getVerticalScrollBar();
// bar.setValue(bar.getMaximum());
// ******** tried this too without success
try {
int endPosition = editor1.getDocument().getLength();
Rectangle bottom = editor1.modelToView(endPosition);
editor1.scrollRectToVisible(bottom);
} catch (BadLocationException e) {
System.err.println("Could not scroll to " + e);
}
}
});
}
String text = " textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample\n" +
"\n" +
" <p>\n" +
" <img alt=\"img1\" src=\"http://www.joomlaworks.net/images/demos/galleries/abstract/7.jpg\">\n" +
" \n" +
" </p>\n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample textSample textSample \n" +
" textSample textSample textSample textSample";
}