问题
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.
回答1:
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).
来源:https://stackoverflow.com/questions/7567305/mousewheel-event-doesnt-fire-in-swt-awt-component