JTable: double-click should keep prior selection - java

I have a JTable where I can select one or more cells. I also want to react on double-click for doing some extra action for the selected cells. But the problem is, when the user double-clicks, the selection changes to the clicked cell. But I want to keep the prior selection on double-click, so I can handle the double-click for all selected cells.
EDIT:
Related to this question:
Java : ignore single click on double click?
But I hope, there is a better/easier solution for my case.

The problem is, that on the first click the first event goes out. A bit later the second click might come or not. So the first click event does know nothing. As in the proposed solution a timer might do.
What also might do is on the first click to select nothing, but invoke a special selection event a bit later.
SwingUtilities.invokeLater(myRunnable);
and on handling the double click/myRunnable the true selection. Timing might be unavoidable though.

you can use setClickCountToStart() for XxxCellEditor, I don't know something about your JTable

Related

How can I serialize a JComboBox selection?

I would like it so that if I pick something on the JComboBox, when I close it it's saved, and so when I re-open the program, whatever was last selected is still selected.
I faced this problem before in one of my GUI applications. What I've done is that I saved the JComboBox selection in a variable using getSelectedItem() function. And when I reopen the program, I set it out again at the same index using setSelectedItem(Object a).
I hope it helps you solve your problem.

What event that is generated by clicking once on an item in a list

Which event is generated when clicking once on an item in a list in java
I found something like
selection event for double clicking.
But nothing for single click that selects an item.
If you want to do something with an Item from a JList which is selected, you can try to use
list.getSelectedIndex()
And use an ActionListener with a button to do whatever else you need. Without a peace of code from you and information what exactly you want, I cannot come up with better Idea, sorry.
Else,
You can use MouseEvent and with
MouseClicked(){}
Method and If statement,
if (List.getSelectedIndex() == ...)
To Perform Action.

Stop JSF Event Chain

I'm working on modifying a jsf-1.2 application for use on a tablet pc.
For that purpose I have made the rows of a table clickable to open the detail view of the rows contents, rather than have the user click on a link in that table (which is the case in the Desktop Version).
It works fine so far.
Each row can, however, contain two rich-buttons that open a different link.
The contents of that link is opened in a new tab, like it's supposed to.
My problem is, that the action event of the rowclick is activated in the original tab as well, which is not the intended behaviour.
What I want to do now is stop the event if one of the buttons in the row is clicked.
I know already, that the event of the butons is fired before the rowclick event.
Is there a way to just cut the event chain at this point?
This happens due to Event Bubbling. Try using Event.stop(event); in your commandButton as below.
<a4j:commandButton onclick="Event.stop(event);" action="#{myBean.myMethod())}" value="Show" />
I don't know if you can break the chain - but even if it is possible (probably by throwing an exception) - I would recommend against it.
The proper (sadly) way to do it in an event-driven environment is to set some boolean indicator to be true when a handler for a button fires, and set it back to false when the handler finishes its job.
In the handler for the row-clicked event, you need to check to see if the boolean is true - and only if it is false - continue with handling the row-clicked event.
A suggested name for the boolean: isRowButtownClicked
Disclaimer: I'm basing this answer on my knowledge in C# - I do not have any experience in GUI development in Java and specifically not in JSF.

java - deactivate a listener

I have a general question regarding listeners.
Lets say I have two JTabbedPanes and both have a ChangeListener. They are both displayed and I want them both to show the same pane (index) so when a user changes the selected pane in one the other changes too.
In brief, one JTabbedPane listener changes the other JTabbedPane using setSelectedTab().
Obviously, the first listener will activate the second listener and the second will reactivate the first in an endless operation.
This will be solved with booleans.
Is there a smarter way to do it?
Is there a way to change a tab without triggering the Listener?
Is there a way to activate the listener only when a user changes it and not the code?
Thank you.
BTW: I always have the same questions with buttons. But with buttons I take the code from the listener and put it in a method. when One button needs to activate a button it calls its code. But in JTabbedPane it is different.
The simple solution is to act only when necessary. For example:
if(currentTab != desiredTab) {
// change tab
}
That will prevent an infinite loop.
If you need to be able to switch the behavior on and off, then using a boolean flag isn't a bad way to go about it. The alternative is the remove the listener, using removeChangeListener. The flag may be more performant as it may avoid memory allocation and deallocation, but a lot depends on the other details of your situation.
share the selectionModel, like
secondTabbedPane.setModel(otherTabbedPane.getModel());

Error Dialog Not Being Destroyed

I am working on a project that is using a JTable to display, among other things, a column of dates. We needed validation for the user input for dates, so I have implemented a combination of masking for format validation and parsing for actual date validation. I have done this using a custom CellEditor for the date column.
Inside my MaskedCellEditor, I have a JFormattedTextField. I setup the masking for dates. Then I add an InputVerifier to allow for actual validation. My InputVerifier implements verify() to check: 1. textField.isEditValid() 2. DateValidator.ValidDate(). If either is invalid, verify returns false and the InputVerifier locks the focus into the text field (the cell editor) and a small message dialog is displayed reminding the user of the date format.
The error message is a small, undecorated, non-modal, non-focusable JDialog that pops up underneath the cell being edited. It disappears on a keypress or a successful date verification. It is working great except for a small edge case.
If the user selects a menu button on the top of the application while an invalid edit has popped up the dialog, it switches screens, destroying everything currently on the screen (including the table). However, since the dialog is being shown and a keypress/successful edit has not occurred, the dialog is never hidden. It remains visible in a completely unrelated context on a different screen. Once the user has switched off the screen with the table, there is no way for the user to get rid of the dialog.
I have debated throwing either a Timer and/or a MouseListener on the dialog itself that would cause it to disappear, but I feel that I am ignoring the actual problem. The dialog is never being disposed of and I am pretty sure its because it is still set to be visible and it is preventing the garbage collector from getting rid of it.
I have a Cleanup method on the panel holding the JTable, but I cannot find a good way to reference the dialog (a component of the InputVerifier) in order to get rid of it. The dialog is pretty far removed from the table's parent panel. (Panel -> JTable -> CellEditor -> JFormattedTextField -> InputVerifier -> JDialog)
Any ideas on how to force the dialog to be hidden when the table is destroyed? If you need more details, let me know. I'm trying not to get you guys bogged down in the details, but there is a lot going on.
As a first thought, can you not go down the listener approach. If you have a closeErrorDialog() type method that gets called when upon successful valdiation, then you can also call it when a menu action is selected.
As an alternative, perhaps you could control the transition from menu to menu in some way, and create a "cleanup" method which will close down any exisiting error dialogs. This would allow for any other actions that need to take place when changing menus, to happen in the same place.
Just a couple of quick ideas of the top of my head. Hope they are along the lines of what you meant
Many people will vote me down for saying this, but it sounds like your dialog should be modal so that users can't switch away from it without dismissing it first. Or at least disable the menus that allow people to switch away while this dialog is displayed.

Categories