我遇到以下主题中描述的问题:
Scroll chart with mouse wheel in TeeChart
我从oficcial网站设置了示例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);
答案 0 :(得分:1)
你是对的。 MouseWheel事件是在TeeChart Java for SWT中触发的,但在TeeChart Java for Swing中没有。
我已将其添加到公共跟踪器(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);
}
}