After you setText(), add the following codes://go to end editorPane.selectAll(); int length=editroPane.getSelectedText().length(); editorPane.setSelectionStart(length); editorPane.setSelectionEnd(length); //must have this statement.//if go to home: editorPane.setSelectionStart(0); editorPane.setSelectionEnd(0); //must have
editorPane.selectAll();
int length=editroPane.getSelectedText().length();
editorPane.setSelectionStart(length);
editorPane.setSelectionEnd(length); //must have this statement.//if go to home:
editorPane.setSelectionStart(0);
editorPane.setSelectionEnd(0); //must have
1、控制JScrollBar,在setText(String)后计算JScrollBar的最大位置
JScrollPane sPane = new JScrollPane(textArea);
JScrollBar sBar = sPane.getVerticalScrollBar();
sBar.setValue(int value);//value为具体的位置sBar.getMaximum()//得到最大
sBar.getMinimum()//得到最小sBar.setValue(int value);要写在setVisible(true);后面
2、将光标设置到输入字符串的末尾
textPane.setCaretPosition(int position);
//editroPane.getSelectedText().length();可取得editroPane中字符串的总数
<------ 树欲静而风不止 ------>
可是为什么窗体事件里,例如:
JTextField里面keyPress事件中
if (evt.getKeyCode()==KeyEvent.VK_ENTER ){
//content是StringBuffer
content.append(JTextField.getText());
JEditorPane.setText(content.toString());
}这种情况下内容不会跳到顶部,而且自动向下滚。但是在后台线程调用JEditorPane.setText(content.toString());,内容会跳到顶部。何解?如何解决?
post your code.
while (true){
theLine = networkIn.readLine();
if(theLine==null)break;
content.append(theLine);//content是一个StringBuffer
jEditorPane.setText(content.toString());
}
这样刷新内容后,pane里面显示在整个内容的最上面如果是在JTextField里面keyPress事件中
if (evt.getKeyCode()==KeyEvent.VK_ENTER ){
//content是StringBuffer
content.append(JTextField.getText());
JEditorPane.setText(content.toString());
}这种情况下内容不会跳到顶部,而且自动向下滚。
在JFrame中的BorderLayout.CENTER中同时加两个相同的JEditorPane,先将最上面的隐藏,给隐藏的JEditorPane中setText(String str)和设置好了滚动条位置后再把它显示出来,这样看着就好象直接到了最底部。
呵呵~~~~这样做很麻烦,而且也没什么意思,建议你写个方法让滚动条以加速度自动滚下来,这样看着效果也会不错的。<------ 树欲静而风不止 ------>