I'm working on an application running on Java in a headless environment. I'm handling all input and output through custom classes; my root JPanel
is drawing to a BufferedImage
which is then presented to the user. However, I'm having trouble passing MouseEvents
to allow the JPanel
and its children to handle them.
Currently, the constructor of my main class, a child of JPanel
, uses:
this.enableInputMethods(true);
this.enableEvents(~0);
Then, in onMouseDown(MouseEvent e)
and friends (which do get executed):
dispatchEvent(e);
I've also tried processEvent(e)
and processMouseEvent(e)
, but to no avail.
I'm generating the MouseEvent
using the JPanel
subclass as the source, and MOUSE_PRESSED
and its friends as the ID.
Is there anything else I can do either in the constructor or the event handler that will allow the JPanel
to process the event as a normal event, passing it off to its children and firing any ActionEvents
and focus changes?
I've got a workaround that I'm currently implementing, manually creating the ActionEvents
and keeping track of the currently focused component. I'm working on passing MouseEvents
down to child components at the moment but here's what I've got.
Component c = findComponentAt(e.getX(), e.getY());
if(c!=null){
ActionEvent ae = new ActionEvent(c,ActionEvent.ACTION_PERFORMED,"");
ActionListener[] listeners = c.getListeners(ActionListener.class);
for(ActionListener l:listeners){
l.actionPerformed(ae);
}
if(c.isFocusable()) setFocusedComponent(c);
}
Admittedly, it's pretty messy, but in absence of a better path to pursue I have to implement it this way.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.