Below is a shortened version of my code to illustrate the problem. I was hoping scrollRectToVisible would move the scrollbar and scrollpane back to the top but it remains at the bottom. Thanks in advance for any suggestions.
package testing;
import javax.swing.SwingUtilities;
public class Testing {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new GUItest();
}
});
}
}
package testing;
import java.awt.Dimension;
import java.awt.Rectangle;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
public class GUItest extends JFrame {
private JEditorPane myEditorPane;
private JScrollPane myScrollPane;
public GUItest() {
myEditorPane = new JEditorPane();
myScrollPane = new JScrollPane(myEditorPane);
myScrollPane.setPreferredSize(new Dimension(400, 200));
getContentPane().add(myScrollPane);
myEditorPane.setContentType("text/html");
myEditorPane.setText("<html>" + "test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>" + "</html>");
Rectangle rect = new Rectangle(1, 1, 1, 1);
myEditorPane.scrollRectToVisible(rect);
pack();
setVisible(true);
}
}
It works if you do the scrolling "in the next frame render cycle" after the JFrame
is visible:
import javax.swing.SwingUtilities;
import java.awt.Dimension;
import java.awt.Rectangle;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
public class GUItest extends JFrame {
private JEditorPane myEditorPane;
private JScrollPane myScrollPane;
public GUItest(){
myEditorPane = new JEditorPane();
myScrollPane = new JScrollPane(myEditorPane);
myScrollPane.setPreferredSize(new Dimension(400, 100));
getContentPane().add(myScrollPane);
myEditorPane.setContentType("text/html");
myEditorPane.setText("<html>" + "test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>" + "</html>");
Rectangle rect = new Rectangle(1,1,1,1);
pack();
setVisible(true);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
myEditorPane.scrollRectToVisible(rect);
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new GUItest();
}
});
}
}
It looks like the JFrame
has to be valid/visible to issue the scroll command.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.