Question

I display an html file in JEditorPane inside JScrollPane inside JPanel inside JFrame. How can I add a listener to detect "Page Down" and "Page Up" events?

Here is my code:

public class Test_Url extends JPanel {

    JEditorPane editor;
    JScrollPane scroller;
    String source = "test.html";

    Test_Url(){
        super();
        setLayout(new BorderLayout());
        editor = new JEditorPane();
        editor.setEditable(false);
        File file = new File(source);
        String page = "file:///" + file.getAbsolutePath();
        try {
            editor.setPage(page);
        } catch (IOException ex) {
            Logger.getLogger(Test_Url.class.getName()).log(Level.SEVERE, null, ex);
        }

        scroller = new JScrollPane(editor, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scroller.setPreferredSize(new Dimension(500, 600));  
        add(scroller, BorderLayout.CENTER);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new Test_Url());
        frame.pack();
        frame.setVisible(true);
    }
}

Thank you!

Was it helpful?

Solution

You can add an AdjustmentListener to a JScrollBar of the scroll pane.

I don't think it will tell you whether it was scrolled up or down so you will need to track the last value and do a comparison with the current value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top