Swing Dynamical JCombobox detect selection change by user - java

I would like to know if there is a way to detect if changes in the selection of a item in a swing JCombobox is done by a user (actively) or is causes by repopulating the Jcombobox.
I have to dynamically repopulate the items of the combobox based on other selection, this also invokes the actionPerformed event
so actionPerformed is invoked by:
selection changed by user
repopulating the jcombobox items.
how to tell the difference?
Thanks of helping !

No, not really.
A possible solution is to disable event notification while the combo box is updated. This can be done in (at least) one of two ways...
Firstly, you could physically remove the listener from the combo box, if you have a reference to it.
Secondly, you set a boolean flag, which when true, the listener would ignore the event.
For example...

Related

Enable a Jbutton after all Jtable fields are filled

I have a Jtable with editable fields and a Jbutton. I want my button to be disabled until all table fields are filled. After all fields are filled I want the button to be enabled. How can I achieve this?
Thanks for the help.
two options:
implement a TableModelListener which checks if the condition is met, and if so enabled/disables the button
implement a custom TableModel which does the check itself and fires a PropertyChange if not/filled. Listen to that property and enable/disable as appropriate
The latter is the better option, because it's really up to the model to decide about its own state.

How to avoid infinite update loops in Swing?

I have a JPanel with a set of items (for example combo boxes and text fields). Some action listeners are implemented on those items to register user updates.
If the user selects a value in a JComboBox (for example), the action listener captures the event. The corresponding underlying bean method is called and the panel is refreshed. Changing can have an impact on other fields displayed in the pane.
The problem is that when the panel is refreshed, all listeners are triggered, and they call for a refresh themselves. This leads to an infinite loop.
How can I avoid this? I can't get rid of the listeners, because I need to capture user updates, but I don't want these to fire when I am only refreshing the panel content.
One option is to have a central boolean value or some indicator that each listener can check to prevent the chaining of events.
Another option is to not refresh the field if the value does not change. That way each component is updated at most once per refresh.
I can't get rid of the listeners, because I need to capture user updates, but I don't want these to fire when I am only refreshing the pane content
Then remove the listeners, refresh the pane content and then restore the listeners. This way the listeners only fire when a user change is made.
I think that if your problem is in combobox it just points to a bug. Really, if user changes the value of the combobox, that somehow triggers refresh of the pane the value of the combo box should not be changed second time! So if it is onValueChanged() (or something like this) it should not be called at all when pane is being refreshed.
But if for some reason it happens you can verify whether the old and new values are the same and exit the listener.
If this still does not help I'd suggest you some non-standard solution: try to investigate the stack trace into the listener. Can you identify whether the listener was called as a direct reaction to user's action or after the pane refresh? In this case you can create utility method and put it in the beginning of all relevant listeners.
My applications also suffered from this problem, and solution with the flag, that I should check in every listener and enable/disable in code, feels not very good for me. I always forgot to set this flag to true/false in necessary places.
That is why I decide to implement another solution.
I just subclass all default swing components that I am using often, and implemented custom ValueChanged event that I fire after mouse/keyboard/clipboard/etc events. Now I am always know, that if ValueChanged event is fired, it means, that value was issued by user, not by code. Event handling in this way much more cleaner. This solution solves my problem.

Java Swing and JComboBox Events

I have a JComboBox with multiple values in it. I need to be able to detect when the user clicks the JComboBox but does not change the currently selected item.
Neither itemStateChanged nor actionPerformed fire when this happens.
What event should I be using?
How about recording the combo box's state when the mouse button is pressed, and comparing it to the value the box has when the mouse button is released?
attach a MouseListener to the JComboBox
override the mousePressed() method to record the box's state to a temp variable
override the mouseReleased() method to compare the box's value to the temp variable's value
At this point, it's a simple equality check.
Add a MouseListener to the JComboBox using its addMouseListener method. You will want to extend MouseAdapter and override only the mouseClicked method.
What if the user users the keyboard to open the popup and then uses the escape key to close the popup?
I would use a PopupMenuListener. This should handle both mouse and keyboard actions. The concept would be the same as other suggestions. When the popup is displayed you save the selected index. When is closes you compare the selected index to see it it has changed.
I was looking specifically at right mouse click on items, so it's a slightly different problem.
But the solution for me was to
Subclass JComboBox substituting getCellRenderer() with a subclassed DefaultListCellRenderer.
In the cell renderer intercept getListCellRendererComponent() which has boolean isSelected, boolean cellHasFocus parameters and can be used to watch for mouse events and do list.setToolTipText().
I'm sure the non-final selection change will get there, where it can be intercepted.

JTable cell loses focus after run an action on selected cells if use keyboard shortcut and temporaily disable the JTable

In my JTable I have a number of actions that can accessed via popup menu or by selecting the configured shortcut. Selecting the action from the popup using mouse or keyboard is fine, and I can use the cursor keys to move to a field next to the original selection no problem. However if I use the shortcutkey instead it performs the action okay but I cannnot exit the selected fields afterwards using the cursor keys, because for some reason the focus is now with a component outside of the JTable.
EDIT:When I start the task I change the cursor and disable the JTable, when I complete the task I renable the table and reset the cursor. If I remove the disable code it works, but this then allow the user to make changes to the table which I dont want, and I cannot understand why it only fails when using keyboard shortcut.
Fixed the problem, after renabling the Jtable I needed to call requestFocusInWindow()

Disabling JComboBox and retaining original item list

My action listener on a JComboBox invokes a thread. I would like the component to be disabled until the thread completes.
I have tried calling seEnabled(false) when the thread start and setEnabled(true) when it completes. Unfortunately setEnabled(false) clears the combo box list as well.
Is there a way of disabling the component but retain the original list?
setEnabled(false) definitely doesn't clear the contents of the combo box. Something else must be going on.
Posting your code might help.

Categories

Resources