I have been going through Google for two days now, somewhat lost.
I can easily fire an event when a right-click get triggered. That isn’t a problem. My problem is that I also need to fire a similar event if/when that right-click menu gets dismissed without any selection having been made.
This boils down to a business need: there is a list of items in a table, the table has checkboxes in the left most column to show which rows have been selected. Because these checkboxes are meant to be the UI key to determining which rows will be processed, any processing action looks at the state of the checkboxes instead of which rows have been selected.
The users can select checkboxes directly to make an action on one or more rows (either via buttons in the footer of the window or via the right-click menu). However, there also needs to be the option of just making a single active selection via the right-click event itself, without needing to explicitly check off a checkbox.
In this case, triggering the right-click on an entry will auto-check that checkbox (whether or not it has been already selected). However, if the user decides not to conduct any action on that row, that checkbox needs to be brought back to its prior state (either checked or unchecked, brought through from the firing of the right-click).
By only bringing through the original state, I don’t have to care about the original state, only that it is being re-applied when the right-click is abandoned. This allows both checked and unchecked original states to be restored successfully. Essentially, this allows an accidental right-click to be dismissed without leaving that table row with a checkbox checked off each and every time - this would be undesired behaviour.
Unfortunately, I have not found any examples on the Internet that talk about targeting the dismissal of a right-click menu, and how to hook an action into that event.
My code so far is something like this:
private void setListenerForItemsTable() {
tblItems.addMouseListener( new MouseAdapter() {
public void mousePressed( MouseEvent evt ) {
if ( view.showMaybePopup( evt ) ) {
rightClickEvent(); // Fires needed code on right-click popup appearance.
}
}
public void mouseReleased( MouseEvent evt ) {
if ( view.showMaybePopup( evt ) ) {
rightClickEvent();
}
}
} );
}
Which works wonders on the creation of a right-click pop-up. It works wonderfully.
As an FYI, the code also has a listener for the right-click menu itself, which allows the selection of any item in that right-click menu to be handled:
private void setListenerForRightClickMenu() {
// Preview
mnuPreviewItem.addActionListener( (e)
-> {
previewItem();
} );
// Resend Fax
mnuResendItem.addActionListener( (e)
-> {
resendItem();
} );
/// etc...
}
However, any attempt to use something like addFocusListener
on the right-click menu in the same way that addMouseListener
was attached to the table causes a Null Pointer Exception; presumably because the right-click menu isn't available until the right-click is actually triggered.
Suggestions?