How to implement MouseWheelListener for JPanel without violating its default implementation?

Simply; I have JPanelinside JScrollPane;

As expected; JScrollPanelistens by default MouseWheelEvent, so scrolling works well when the wheel spins and when the cursor hovers over JPanel.

But after that; I just updated JPanelit to implement MouseWheelListener, and I added this mouse listener for myself JPanel.

@Override
public void mouseWheelMoved(MouseWheelEvent e) {
    if (e.isControlDown()) {
        if (e.getWheelRotation() < 0) {
            System.out.println("mouse wheel Up");
        } else {
            System.out.println("mouse wheel Down");
        }
    }
}

JPanelresponds to this implementation when they are both; Ctrlpressed, and the wheel rotates, and the cursor - on JPanel. But the default behavior is JScrollPaneunexpectedly lost !!!

When I turn the wheel while the cursor is hovering over JPanel, the scrolls JScrollPanedo not respond !!!

, ; MouseWheelListener JPanel.

, MouseWheelListener JPanel ?

+4
3

MouseWheelEvents, .

, ( , ), , . , , , . - , , , - .

, . , Window Manager Look and Feel, , , , ( , , Windows)!


Swing. , redadatching , MouseWheelEvents , .

, ( , LuxxMiner, ) MouseWheelEvent :

package stackoverflow;

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

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

public class MouseWheelListenerForPanelInScrollpane
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI()
    {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        f.getContentPane().setLayout(new GridLayout(1,2));

        MouseWheelListenerPanel m = new MouseWheelListenerPanel();
        m.setPreferredSize(new Dimension(100,4000));
        JScrollPane scrollPane = new JScrollPane(m);
        f.getContentPane().add(scrollPane);

        f.setSize(500,500);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}

class MouseWheelListenerPanel extends JPanel implements MouseWheelListener
{
    MouseWheelListenerPanel()
    {
        addMouseWheelListener(this);
    }

    @Override
    public void mouseWheelMoved(MouseWheelEvent e)
    {
        if (e.isControlDown())
        {
            if (e.getWheelRotation() < 0)
            {
                System.out.println("mouse wheel Up");
            }
            else
            {
                System.out.println("mouse wheel Down");
            }
        }
        else
        {
            getParent().dispatchEvent(e);
        }

    }
}
+5

else, , ctrl :

@Override
public void mouseWheelMoved(MouseWheelEvent e) {
    if (e.isControlDown()) {
        if (e.getWheelRotation() < 0) {
            System.out.println("mouse wheel Up");
        } else {
            System.out.println("mouse wheel Down");
        }
    } else {
        // pass the event on to the scroll pane
        getParent().dispatchEvent(e);
    }
}
+6

I don’t know if this really matches the correct answer, since this is a kind of workaround, but I came up with the following solution: just call the method mouseWheelMoved scrollPaneonly when it is Ctrlnot pressed:

if (e.isControlDown()) {
    if (e.getWheelRotation() < 0) {
        infoLabel.setText("Mouse Wheel Up");
    } else {
        infoLabel.setText("Mouse Wheel Down");
    }
} else {
    scrollPane.getListeners(MouseWheelListener.class)[0].mouseWheelMoved(e);
}

Full example:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.TitledBorder;

public class Example {
    public Example() {
        JFrame frame = new JFrame();
        frame.setLayout(new BorderLayout());
        frame.add(new ScrollPanePanel());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Example();
            }
        });
    }
}

class ScrollPanePanel extends JPanel implements MouseWheelListener {
    private JLabel infoLabel;
    private JScrollPane scrollPane;

    public ScrollPanePanel() {

        JPanel panel = new JPanel(new GridLayout(0, 1));
        for (int i = 1; i <= 100; i++) {
            panel.add(new JLabel("Label " + i));
        }
        panel.addMouseWheelListener(this);
        scrollPane = new JScrollPane(panel);

        infoLabel = new JLabel(" ");
        JPanel infoPanel = new JPanel();
        infoPanel.add(infoLabel);

        setLayout(new BorderLayout());
        add(scrollPane);
        add(infoPanel, BorderLayout.SOUTH);

    }

    @Override
    public void mouseWheelMoved(MouseWheelEvent e) {
        if (e.isControlDown()) {
            if (e.getWheelRotation() < 0) {
                infoLabel.setText("Mouse Wheel Up");
            } else {
                infoLabel.setText("Mouse Wheel Down");
            }
        } else {
            scrollPane.getListeners(MouseWheelListener.class)[0].mouseWheelMoved(e);
        }
    }
}
+3
source

Source: https://habr.com/ru/post/1627693/


All Articles