【问题标题】:mouse wheel moved not fired in TeeChart鼠标滚轮移动未在 TeeChart 中触发
【发布时间】:2016-06-06 12:40:47
【问题描述】:

我遇到了以下线程中描述的问题:

Scroll chart with mouse wheel in TeeChart

我从官方站点设置了示例 Java 演示并正常运行,我可以看到不同类型的图形等。

问题是我无法将 MouseWheelListener 添加到 TChart。在上面的帖子中,有人回答如下: “以下代码在 Eclipse 中使用 TeeChart Java SWT 对我来说效果很好:” 另一位用户评论为:
“确实是这样。我有完全相同的代码,但是直到我手动将焦点设置在图表上之后它才起作用。”

首先答案中给出的代码在 Eclipse Indigo 中不起作用。第二,答案已经有一段时间了,lib已经发生了一定程度的变化,没有更多的“mouseScrolled”事件。 有一个名为“mouseWheelMoved”的事件。对于我的一生,我无法让这个事件被解雇。

非常感谢任何建议、示例、意见。

谢谢。

示例代码:

JFrame frame = new Jframe();
JPanel contentPane; 
TChart tChart2 = new TChart();
tChart2.setGraphics3D(null);
tChart2.setBounds(new Rectangle(6, 71, 572, 268));
// Mouse Wheel Listener
tChart2.addMouseWheelListener(new MouseWheelListener() {
        public void mouseWheelMoved(MouseWheelEvent e) {
        // TODO Auto-generated method stub
           System.out.println("mouseWheelMoved worked");
        }
});
contentPane = (JPanel) frame.getContentPane();
contentPane.add(tChart2);

【问题讨论】:

  • 寻求帮助的问题必须包括期望的行为特定问题或错误以及重现该问题所需的最短代码 在问题本身。没有明确问题陈述的问题对其他读者没有用处。请参阅:How to create a Minimal, Complete, and Verifiable example
  • 期望行为:触发 MouseWheelMoved 事件所以我的问题很清楚。我将很快添加示例可重现代码。
  • @ShouRisha contentPane 是什么类型的?看起来您没有使用 SWT,而是使用 Swing
  • @Baz 道歉。我更新了代码。是的,我正在使用摇摆。我为示例代码创建了 JFrame,我在实际代码中使用了类似的设置。我从 Jframe 对象获取 contentPane 并将图表添加到它。我现在将更新标签。感谢您指出。
  • @ShouRisha 在这种情况下我帮不了你。使用 SWT 时效果很好。

标签: java swing teechart


【解决方案1】:

你是对的。 MouseWheel 事件在 SWT 的 TeeChart Java 中被触发,但在 Swing 的 TeeChart Java 中却没有。

我已将它添加到公共跟踪器 (here) 并已修复它以供下一个维护版本使用。

对于那些有源代码的人,修复包括对 TChart.java 的几个小改动:

--- a/Swing/src/com/steema/teechart/TChart.java
+++ b/Swing/src/com/steema/teechart/TChart.java
@@ -21,6 +21,8 @@ import java.awt.Toolkit;
 import java.awt.datatransfer.*;

 import com.steema.teechart.events.FrameworkMouseEvent;
+import com.steema.teechart.events.FrameworkMouseWheelEvent;
+
 import java.awt.event.*;
 import java.beans.*;
 import java.io.*;
@@ -1405,7 +1407,16 @@ public class TChart extends JComponent implements Serializable, IChart,
                                       e.getX(),e.getY(),e.getClickCount(),
                                       true,e.getButton());
     }
-
+    
+    private FrameworkMouseWheelEvent getFrameworkEvent(MouseWheelEvent e)
+    {
+      return new FrameworkMouseWheelEvent(e.getComponent(), e.getID(),
+                                           e.getWhen(), e.getModifiers(),
+                                           e.getX(), e.getY(), e.getClickCount(),
+                                           true, e.getScrollType(),
+                                           e.getScrollAmount(), e.getWheelRotation());
+    }
+    
     protected void processMouseEvent(MouseEvent e) {
         chart.cancelMouse = false;

@@ -1439,6 +1450,15 @@ public class TChart extends JComponent implements Serializable, IChart,


     protected void processMouseWheelEvent(MouseWheelEvent e) {
+       chart.cancelMouse = false;
+       if (_allowMouse) {
+           FrameworkMouseWheelEvent fwe = getFrameworkEvent(e);
+           
+           if (!chart.cancelMouse) {
+                super.processMouseWheelEvent(fwe);
+            }
+        }
     }

     private boolean _allowMouse = true;
-- 

还有一个新类:FrameworkMouseWheelEvent.java:

package com.steema.teechart.events;

import java.awt.Component;

public class FrameworkMouseWheelEvent extends java.awt.event.MouseWheelEvent {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public FrameworkMouseWheelEvent(Component source, int id, long when, int modifiers, int x, int y, int clickCount,
            boolean popupTrigger, int scrollType, int scrollAmount, int wheelRotation) {
        super(source, id, when, modifiers, x, y, clickCount, popupTrigger, scrollType, scrollAmount, wheelRotation);
    }

    public FrameworkMouseWheelEvent(Component source, int id, long when, int modifiers, int x, int y, int xAbs,
            int yAbs, int clickCount, boolean popupTrigger, int scrollType, int scrollAmount, int wheelRotation) {
        super(source, id, when, modifiers, x, y, xAbs, yAbs, clickCount, popupTrigger, scrollType, scrollAmount, wheelRotation);
    }

    public FrameworkMouseWheelEvent(Component source, int id, long when, int modifiers, int x, int y, int xAbs,
            int yAbs, int clickCount, boolean popupTrigger, int scrollType, int scrollAmount, int wheelRotation,
            double preciseWheelRotation) {
        super(source, id, when, modifiers, x, y, xAbs, yAbs, clickCount, popupTrigger, scrollType, scrollAmount, wheelRotation,
                preciseWheelRotation);
    }
}

【讨论】:

  • 你好@Yeray 感谢发布解决方案。我只有罐子里的演示类。我将查找带有源文件的库。再次非常感谢。
  • @ShouRisha 联系“steema dot com 的销售人员”,我们将尝试提供补丁或测试版本。
  • 谢谢。我会联系steema。最好的问候。
猜你喜欢
  • 1970-01-01
  • 2012-11-26
  • 1970-01-01
  • 1970-01-01
  • 2011-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-01
相关资源
最近更新 更多