MouseWheel 事件在 SWT-AWT 组件中不触发
我陷入了无法触发的鼠标滚轮事件。我使用 swt_awt 桥,因此我能够在我的 RCP 应用程序中使用 swing 组件。我测试了我能找到的所有东西,但没有成功。因为我的应用程序非常复杂,所以我创建了问题的简化版本,如果您想帮助我,它也应该可以帮助您确定方向。
现在这个简化的问题是,我希望我的文本区域(或滚动窗格)捕获鼠标滚轮事件。但这些事件都丢失了。
import java.awt.Frame;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
public class Main {
protected Shell shell;
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
Main window = new Main();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(450, 300);
shell.setText("SWT Application");
shell.setLayout(new GridLayout(3, false));
List list = new List(shell, SWT.BORDER);
list.setLayoutData(new GridData(GridData.FILL_VERTICAL));
list.add("Something");
Composite composite = new Composite(shell, SWT.EMBEDDED);
composite.setLayoutData(new GridData(GridData.FILL_BOTH));
Frame graphFrame = SWT_AWT.new_Frame(composite);
final JTextArea textarea = new JTextArea(60, 80);
JScrollPane scrollpane = new JScrollPane(textarea);
graphFrame.add(scrollpane);
textarea.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent arg0) {
textarea.setText(textarea.getText() + "Mouse clicked.\n\r");
}
@Override
public void mouseEntered(MouseEvent arg0) {
textarea.setText(textarea.getText() + "Mouse is in.\n\r");
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
});
textarea.addMouseWheelListener(new MouseWheelListener() {
@Override
public void mouseWheelMoved(MouseWheelEvent arg0) {
textarea.setText(textarea.getText() + "Mouse wheel activated.\n\r");
}
});
}
}
编辑:我发现,Panel 能够捕获 awt 事件,但是任何扩展 JComponent 的类(就像我的组件一样)都会以某种方式丢失这些事件。 我还设法创建了一个丑陋的 hack,在单击 JComponent 后,我强制将焦点放在 swt 复合材料上。然后我至少可以监听 swt 事件并直接从 swt 事件监听器使用我的 JComponent。我仍然希望能够直接在 JComponent 上捕获 awt 鼠标事件的解决方案。
I am stuck at mousewheel event that just doesn't fire. I use swt_awt bridge, so I am able to use swing component in my RCP application. I tested everything I was able to find, but with no success. Because my application is awfully complex, I created a simplified version of my problem, which should also help you to orientate if you would like to help me.
Now this simplified problem is, that I want my textarea (or scrollpane) to catch mousewheel events. But these events are lost.
import java.awt.Frame;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
public class Main {
protected Shell shell;
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
Main window = new Main();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(450, 300);
shell.setText("SWT Application");
shell.setLayout(new GridLayout(3, false));
List list = new List(shell, SWT.BORDER);
list.setLayoutData(new GridData(GridData.FILL_VERTICAL));
list.add("Something");
Composite composite = new Composite(shell, SWT.EMBEDDED);
composite.setLayoutData(new GridData(GridData.FILL_BOTH));
Frame graphFrame = SWT_AWT.new_Frame(composite);
final JTextArea textarea = new JTextArea(60, 80);
JScrollPane scrollpane = new JScrollPane(textarea);
graphFrame.add(scrollpane);
textarea.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent arg0) {
textarea.setText(textarea.getText() + "Mouse clicked.\n\r");
}
@Override
public void mouseEntered(MouseEvent arg0) {
textarea.setText(textarea.getText() + "Mouse is in.\n\r");
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
});
textarea.addMouseWheelListener(new MouseWheelListener() {
@Override
public void mouseWheelMoved(MouseWheelEvent arg0) {
textarea.setText(textarea.getText() + "Mouse wheel activated.\n\r");
}
});
}
}
EDIT: I found out, that Panel is able to catch awt events, however any class, that extends JComponent (just like my component do) somehow loose these events.
I also managed to create an ugly hack, where I forceFocus on my swt composite after I click on my JComponent. Then I can at least listen to swt events and work with my JComponent directly from swt event listener. I would still aprreciate the solution where I will be able to catch awt mouse events directly on JComponent.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能会遇到以下问题:http://code.google .com/p/gama-platform/issues/detail?id=84。 http://code.google 中介绍了解决方法。 com/p/gama-platform/issues/detail?id=84#c13(链接到这个问题)。
You may be running into this issue: http://code.google.com/p/gama-platform/issues/detail?id=84. A workaround is described in http://code.google.com/p/gama-platform/issues/detail?id=84#c13 (linked to this very question).