Task: There is a large application in which new jComponent are periodically created. Need to add processing for all of them by mouse clicking on them with the "Alt" key pressed.
It is clear that this needs to be done using component.addMouseListener(). Is there a section of the JComponent creation code where we can insert our code? Or are there other ways to solve the problem?
My code based on Rjelinek's suggestion:
public static void registrationMouseAction() {
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
@Override
public void eventDispatched(final AWTEvent event) {
if (event instanceof MouseEvent) {
MouseEvent mouseEvent = (MouseEvent) event;
if (mouseEvent.getID() == MOUSE_CLICKED) {
// # left prev
if (mouseEvent.getButton() == 4) {
new BackRecentDefinitionAction().performed();
}
// # right next
if (mouseEvent.getButton() == 5) {
new BackRecentDefinitionAction().performed();
}
// # view modal editor
if (mouseEvent.getButton() == 1 && mouseEvent.isAltDown()) {
new OpenModalEditorRadixObjectsAction().performed();
}
}
}
}
}, AWTEvent.MOUSE_EVENT_MASK);
}
Correction: I need to have the processing of "Alt+MouseButton1" for all components in the application (for example, JButton, JPanel, etc.).