简体   繁体   English

在Matlab的JSPlitPane中添加JscrollPane时,它没有收到mousewheelevent

[英]JscrollPane doesn't receive mousewheelevent when add it in JSPlitPane inside Matlab

I've made a simple GUI in Swing with a big JPanel (displaying a big BufferedImage) inside a JScrollPane, inside a JSPlitPane, inside a JPanel, .... inside a JFrame. 我在Swing中制作了一个简单的GUI,在JScrollPane内,JSPlitPane内,JPanel内,.... JFrame内有一个大JPanel(显示一个大BufferedImage)。

When running/displaying my JFrame directly from Eclipse IDE, the JScrollPane receive correctly the mousewheel event and scroll when I scrolled my mouse wheel. 直接从Eclipse IDE运行/显示JFrame时,JScrollPane正确接收mousewheel事件并在我滚动鼠标滚轮时滚动。

But when running from Matlab (I've build a JAR), the JScrollPane doesn't scroll when I've scroll the mouse wheel. 但是从Matlab(我已经构建了一个JAR)运行时,滚动鼠标滚轮时JScrollPane不会滚动。

I've try to add manually mousewheellistener with some syso to debub/understand the problem, like this: 我尝试使用一些syso手动添加mousewheellistener,以消除/理解问题,如下所示:

jscrollpane.addMouseWheelListener(new MouseWheelListener() {
    @Override
    public void mouseWheelMoved(MouseWheelEvent e) {
        System.err.println("jscrollpane mouse wheel event");
    }
});

jsplitpane.addMouseWheelListener(new MouseWheelListener() {
    @Override
    public void mouseWheelMoved(MouseWheelEvent e) {
        System.err.println("jsplitpane mouse wheel event");
    }
});

When running directly from Eclipse IDE, it's displayed "jscrollpane mouse wheel event" (this is OK). 直接从Eclipse IDE运行时,它会显示“ jscrollpane鼠标滚轮事件”(确定)。 But when running my GUI from Matlab, it's displayed "jsplitpane mouse wheel event". 但是当从Matlab运行我的GUI时,它显示为“ jsplitpane鼠标滚轮事件”。

I've also add this piece of code to be sure that my jscrollpane has a mousewheellistener: 我还添加了以下代码,以确保我的jscrollpane具有mousewheellistener:

for (MouseWheelListener listener : jscrollpane.getMouseWheelListeners()) {
    System.err.println("ONE LISTENER FOR JSCROLLPANE");
}

And it's displayed two times (one time for my syso listener, and one time for the builtin mousewheellistener of the jscrollpane component). 它显示了两次(一次是我的syso侦听器,一次是jscrollpane组件的内置mousewheellistener)。

So what happen in Matlab for the JScrollPane component? 那么在Matlab中JScrollPane组件会发生什么呢? What can I do to force JscrollPane to receive MouseWheel events? 如何强制JscrollPane接收MouseWheel事件?

I think Matlab update the general awt mask events to prevent jscrollpane catching mousewheel event? 我认为Matlab更新了一般的awt掩码事件,以防止jscrollpane捕获鼠标滚轮事件?

again (avoiding any mistake from typo) are 再次(避免打字错误)

  1. mousewheel by usung Matlab accesible, 可以使用usung Matlab的mousewheel,
  2. mouse click to any JButton (in JPanel) selected JButton 鼠标单击选定的JButton中的任何JButton(在JPanel中)
  3. if not then edit your question with Swing & Matlab in SSCCE form 如果没有,请在SSCCE表格中使用Swing&Matlab编辑您的问题

.

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;

public class JScrollBarUnitIncrement {

    private JFrame f = new JFrame("");
    private JPanel panel = new JPanel() {
        private static final long serialVersionUID = 1L;

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(80, 600);
        }
    };
    private JScrollPane sPane = new JScrollPane(panel) {
        private static final long serialVersionUID = 1L;

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(100, 200);
        }
    };
    private final int increment = 8;

    public JScrollBarUnitIncrement() {
        panel.setLayout(new GridLayout(40, 1));
        for (int i = 0; i != 40; i++) {
            JButton btn = new JButton("Button 2");
            panel.add(btn);
        }
        sPane.getVerticalScrollBar().setUnitIncrement(increment);
        KeyStroke kUp = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0);
        KeyStroke kDown = KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0);
        sPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(kUp, "actionWhenKeyUp");
        sPane.getActionMap().put("actionWhenKeyUp", new AbstractAction("keyUpAction") {
            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                final JScrollBar bar = sPane.getVerticalScrollBar();
                int currentValue = bar.getValue();
                bar.setValue(currentValue - increment);
            }
        });
        sPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(kDown, "actionWhenKeyDown");
        sPane.getActionMap().put("actionWhenKeyDown", new AbstractAction("keyDownAction") {
            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                final JScrollBar bar = sPane.getVerticalScrollBar();
                int currentValue = bar.getValue();
                bar.setValue(currentValue + increment);
            }
        });
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(sPane);
        f.pack();
        f.setVisible(true);
    }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM