My program has the ability to change the selected item of my combobox. But how can I know if the item change has been caused by an human mouse click on the item itself or by my program.
I'm pretty much looking for a MouseListener that can be added to the items of the JComboBox and not the JComboBox itself.
A JComboBox is a compound component, and it is highly recommended that you avoid using low-level listeners such as a MouseListener with it. Instead why don't you disable your selection listener (perhaps you're using an ActionListener) when the code selects an item, and then re-enable it after the selection is done. Thus you'll know that any activity by the ActionListener is from a user's choice. You can disable and enable the listener by either removing and re-adding it, or by giving using a boolean variable that allows the listener to react only when the boolean is true.
Related
I have a form type application in which the JComboBox I use to represent the title(Mr/Mrs/Ms/etc.) of the client. I want to trigger an event when the combo box has changed values due to the user clicking it to open the drop-down list, and then choosing a new value. I also have the client's names in a left panel. Clicking on a client's name will change the JComboBox to the value stored for that client, but I don't want to trigger the event when the value of combo box changes this way.
Both ActionListener and ItemChangeListener will trigger an event for both of these cases
The MousePressed method from the MouseListener will trigger the event for when the JComboBox is clicked, but not when the arrow next to the JComboBox is clicked. Also, the MousePressed event will trigger regardless of whether or not the selected item in the combo box has changed.
Before you trigger the event when the client's name changes remove the ItemChangeListener and add it back afterwards. That way it will "skip" the notification
I have set up a mouse dragged listener. I trying to set it up where you can click one button then drag your mouse over others to click the other ones. The problem I am having is when you click the first button it turns grey like its waiting for you to release the mouse button. When you move your mouse off the button (still holding the left mouse button) it returns back to its normal color but you cant highlight anything until you let go. Is there anyway to simulated letting the mouse go and "unclicking" the button so you can highlight other things?
What you observe is the typical behavior of the ButtonModel used by Swing buttons. A complete example is examined here, but note how the effect depends on the chosen Look & Feel's ButtonUI delegate.
To get the effect you want, you would have to create buttons using your own variation of BasicButtonUI and a custom ButtonModel that uses isRollover() to add buttons to your program's notion of a selection model.
As an alternative, consider JList, which contains a ListSelectionModel that allows MULTIPLE_INTERVAL_SELECTION. A compete example is shown here.
What is the difference between ChangeListener and ItemListener for JCheckBox and JRadioButton? Both of them work fine when they are selected/deselected.
I know that some components doesn't support ChangeListener like the JComboBox. Other than the reason that ChangeListener or ItemListener work for only some components. Is there any difference between them like when are they generated?
Any answer is appreciated. Thanks in advance.
both listeners for JCheckBox work similarly in that both will fire event upon change in state, whether by clicking or toggle by spacebar or programmatically through doClick() method (Similar to mouse click). One major difference though is that JCheckBox’s itemListener can be fired through setSelected(boolean) method which allows one to fire the event based on desired state whereas others will act only after the state is altered. So why is it important ? Consider when application startup, the GUI needed to configure for defined state, and using setSelected will trigger ItemListener. Note that setSelected is exclusive to ItemListener and has no effect on ActionListener. Do not register both ActionListener and ItemListener as both will be fired, landing the component in a random state
ChangeListener is notyfied when there's any change to the button state. ChangeListener is not notified of what has changed, only that the object has changed. Item listener is only notyfied when an item is selected; by user or setSelected method. It's also not true that ChangeListener is not notyfied when setSelected method is invoked. It is the change of the object state.
I have a UI with a Combo box. The list of items, which can be chosen, has to be refreshed every time the combo is about to open the list.
Is there any way - i.e. to add a listener which will inform UI that Combo is about to open?
Unfortunately I am not able to observe model to update the list when it changes.
Unfortunatelly there is no such method for SWT Components. In Swing it would be easy with the help of the PopupMenuListener Interface.
A workaround I can think of would be to implement a MouseListener and a KeyboardListener (As Comboboxes can be opened by pressing 'space') so you can at least update your Combobox List when those two Events take place.
My custom component is composed of three JTrees inside a JPanel. Only one JTree should be selected at a time, so I've added a TreeSelectionListener to each of them that calls clearSelection() on the previously selected JTree. (See here for more details).
That works fine, but I need to prevent the TreeSelectionListeners to trigger when a JTree is deselected. A simple way to distinguish a selection event from a deselection one would be more than enough.
Thanks in advance!
Just get the current selection from the tree and if it's empty, return.