Mouse block scrolling MouseWheelListener

    JEditorPane.addMouseWheelListener(new MouseWheelListener() {
        public void mouseWheelMoved(MouseWheelEvent e) {


When I add these lines - in the rollout the JEditorPane stops working. How can it be cured?


source to share

2 answers

You should post more of your code to get better help. However, this simple demo worked fine for me (i.e. scrolling still works after adding MouseWheelListener

in JScrollPane


import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;

public class Frame
    public static void main( String[] args )
        JFrame frame = new JFrame( );
        JEditorPane pane = new JEditorPane( );

        String t = "";
        for ( int i = 0 ; i < 10000 ; i++ ) t += "t";

        pane.setText( t );

        JScrollPane scroll = new JScrollPane( pane );

        scroll.addMouseWheelListener( new MouseWheelListener( )
            public void mouseWheelMoved( MouseWheelEvent e )
                System.out.println( "Scroll" );

        frame.add( scroll );
        frame.setSize( 400, 400 );
        frame.setVisible( true );




You can read about how MouseWheelEvents are posted in the Javadoc:

In short, events are delivered to the top-most component under the cursor and in the swing, in most cases the mouse wheel events are handled by the JScrollPane. The JEditorPane contains scrolling, so if you add a listener to the JEditorPane, the JScrollPane will stop receiving events. This is why you have a problem. It's better to add a listener to the JScrollPane instead.



All Articles