由于JEditorPane中的Image,JScrollPane不会更新其滚动以向下跟随插入位置

时间:2014-11-20 16:44:20

标签: java swing jscrollpane jeditorpane

我有JTabbedPane两个标签,其中包括两个JScrollPane

每个JScrollPane都包含JEditorPane。如果编辑器中没有图像,则没有问题。当有图像时,滚动不会跟随第一个编辑器的插入位置,就像图像在加载编辑器结束后加载后总是插入其后的插入符号。

这是我的代码:

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";
}

0 个答案:

没有答案