本文介绍了JScrollPane滚动条跳转到JEditorPane.setText()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
每次(第一次除外)我在JEditorPane滚动条中设置文本跳转到JEditorPane的底部。有什么方法可以避免这种情况吗?我试过的一种方法是在setText()之后使用setCaretPosition(0)。它有效,但它不是我需要的。滚动条跳下然后跳起来。有没有办法避免在settext()处滚动条的任何移动?
Every time (except first time) I set text in JEditorPane scrollbar jumps to the bottom of JEditorPane. Is there any way to avoid this? One way that I tried is to use setCaretPosition(0) after setText(). It works but it's not exactly what I need. Scrollbar jumps down and then jumps up. Is there a way to avoid any movement of scrollbar at settext()?
我的代码:
package test;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JScrollPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
public class Test extends JApplet implements HyperlinkListener {
JEditorPane dataDisplayer = new JEditorPane();
public void init() {
ataDisplayer.setEditable(false);
dataDisplayer.add(new JButton());
dataDisplayer.setContentType("text/html");
JScrollPane jsp = new JScrollPane(dataDisplayer);
dataDisplayer.setText("<a href=''>Change Text</a><br><br><br><br><br><br><br><br><br><br>bla");
this.dataDisplayer.addHyperlinkListener(this);
setContentPane(jsp);
}
@Override
public void hyperlinkUpdate(HyperlinkEvent arg0) {
dataDisplayer.setText("<a href=''>Change Text</a><br><br><br><br><br><br><br><br><br><br>bla bla");
}
}
推荐答案
有不同的方法可以解决这个问题。一种是简单地避免插入符号可见性调整,如下所示:
There are different ways to go with that. One would be to simply avoid caret visibility adjustement, like this:
import java.awt.Rectangle;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.DefaultCaret;
public class TestEditorPane implements HyperlinkListener {
private final class MyCaret extends DefaultCaret {
@Override
protected void adjustVisibility(Rectangle nloc) {
}
}
JEditorPane dataDisplayer = new JEditorPane();
private MyCaret caret;
@Override
public void hyperlinkUpdate(HyperlinkEvent arg0) {
dataDisplayer.setText("<a href=''>Change Text</a><br><br><br><br><br><br><br><br><br><br>bla bla");
}
public void init() {
JFrame frame = new JFrame();
dataDisplayer.setEditable(false);
caret = new MyCaret();
dataDisplayer.setCaret(caret);
dataDisplayer.add(new JButton());
dataDisplayer.setContentType("text/html");
JScrollPane jsp = new JScrollPane(dataDisplayer);
dataDisplayer.setText("<a href=''>Change Text</a><br><br><br><br><br><br><br><br><br><br>bla");
this.dataDisplayer.addHyperlinkListener(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(jsp);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestEditorPane().init();
}
});
}
}
这篇关于JScrollPane滚动条跳转到JEditorPane.setText()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!