Overriding JList's mutual exclusivity - java

For some reason, even though JList is, by default, a multi-select list, when one clicks another option, any choices you previously clicked get erased. Also, for some reason, one would have to hold down Ctrl and click, to make otherSelection
I tried to override this, by making custom ListModel that has selectedIndices stored to it, and adding MouseListener to my JList that, when activated, will append list.getSelectedIndices() to listModel.selectedIndices. However, there is unexpected problem with this approach: when stress-testing my solution (I made damn sure I was working out all of the inevitable programming mistakes before proceeding!), I found out that, for example, I could have the second list option selected, but click on the first, and list.getSelectedIndices() would return the index for the second option (and thus, my algorithm would fail).
Is there any way to make it so that every click would actually add (or remove) the index of the clicked list option to list.getSelectedIndices(), without my workaround?
Here is JAR file for testing (DEBUG == true so debug output is visible from command line).

I use JCheckBox as the ListCellRenderer
Then maybe you should be using a single column JTable with Boolean values. The state of the checkbox will be toggled every time you click on the cell.

Related

Vaadin - adding components as elements of another components

I've been using a ComboBox to store some values and make a selection from those values, but the problem is, ComboBox, as it is, only allows one selection at the time and I need multiple selections, ie checkboxes, but that cannot be done via Vaadin. I figured if I could present checkboxes as the elements of the ComboBox, that would solve the issue, except adding components to a component that is not a layout doesn't seem to be possible.
I've done this tutorial https://vaadin.com/docs/-/part/framework/components/components-customcomponent.html
Basically it combines two Vaadin components into one panel and displays them together, but that's not what I need, as I need certain components to be placed inside a parent component.
So what are my options if I'm to do this?
This is not an answer to the question that you are asking (component within a component), but rather the underlying problem that you present. In other words, I believe your question is an example of an XY problem.
I think you want to use a Grid with multi-select turned on. In this mode, check boxes are automatically added to each row and there is a checkbox in the header to allow toggling all on/off, ability to filter, ability to sort columns, etc. See the documentation for more details.

Java JComboBox selection checked but the combobox doesn't show the selected item

I have a situation that I want some advice on. The listeners of a JComboBox all work as I'd expect.
The JComboBox is created using the constructor that takes an array as argument. The argument is an ArrayList which is converted using the toArray method. The JComboBox is setEditable(false) and setEnabled(false). I'm not sure what else to tell you. JDK 1.8, Mac OS X 10.9.5 and (mis)using NetBeans 8.0.2
What is wrong is that although the underlying values and computations happen when you select, say "-4", i.e. all listeners get the value correctly, the GUI does not display "-4" but rather, it displays the previous selection.
This does not happen every time. When it does happen, if I (change the focus?) Cmd-Tab (on a Mac) away or click on another window (any window) and come back, the value displays correctly.
If I click on the drop-down and see the list, the check mark is definitely on the selected item, not the one currently displayed. And when I let go (without selecting) the screen is corrected.
I've done some things to try to get it to update, most are grasping at straws (ItemListener that sets the selected item to the one it just got; repaint() here and there...).
This is annoying to the users. They doubt they have selected, so they select twice. Or they select and move. All logs and debug show the selected item is the one they intended.
One of these JComboBox (dropdowns) has a button beside it, that takes the name selected and loads a set of values from a setup file. One can watch the displayed (and not selected) name change to the selected name when the button is pressed.
Thanks for any advice.

Showing a counter dynamically for JList items highlighted

I have a program that I created for work. This program takes an uploaded file, reads it, and puts the data into a JList in the GUI. The GUI actually has two lists and the user is able to move items between the left and right list by highlighting them like usual with a JList and then hitting an arrow to move the items. The lists are multiple-interval selection.
One small addition I would like to add is some type of counter that shows the user how many items they have selected before they actually move them between lists. This would need to be dynamic so if the user holds control down and begins clicking the counter will continue to update the number of highlighted items.
As the lists are often quite large and a user might need to move an odd number of transactions between the lists (Think 300 transactions in left list and the user needs to move exactly 50) it would be beneficial to have this counter.
Can anyone think of how this could be done? I'm not sure how to add an action listener to just clicking on the items. Please also let me know if I need to elaborate any more.
Generally my question is can I create an action listener just for when a user clicks a item in a JList that updates a counter for the current selected indices? Also it would need to change when they no longer have selected an indice.
Register a ListSelectionListener with your JList.
The listener could simply query how many rows are selected and update the number in the panel to that. Perhaps use getSelectedValues().size().
http://docs.oracle.com/javase/8/docs/api/javax/swing/JList.html#addListSelectionListener-javax.swing.event.ListSelectionListener-

Changing the contents of a JComboBox during runtime

Alright so I'm trying to create a combo box that will update it's contents during runtime except I have no clue how to do this without receiving a bunch of errors. Is there some sort of method that I can use in order to accomplish this? For example, I have a vector that might start out with the name in drawers 1 and 2 be hi and bye. Then during runtime the program will change drawer one and two to eggs, sausage and add a third drawer with the name being computer. How can I go about changing the name on a JComboBox during runtime?
You want to clear the combobox of all entries using removeAllItems(), then re-add the items from the Vector using addItem().
The data shown in the ComboBox actually lives in its model - some subclass of ComboBoxModel.
DefaultComboBoxModel has methods for adding and removing elements. If you want to completely replace the combo box's contents at runtime, the simplest way might be to just build a new model and call theComboBox.setModel(theNewModel) with it. Also see setSelectedItem() for setting the selection.

multiple instance of a JButton listener event in JLists

I have created a dialog with two JLists displayed and a button that takes the selected value from the second JList and does something (say it outputs the selected value). The list of values from the second JList is updated according to the selection of the value from the first JList.
When the dialog is displayed initially, the button is disabled. When a value is selected from the first and then from the second JList, the button is enabled an the required selection listener is added to it.
The problem is that every time the button is clicked the number of output messages is equal to the time a value of the second JList is selected. For example if I select a value from the second JList, then I change my mind and select another value, the click of the button will output the message two times.
Does anyone know a method to prevent such a thing?
Your ListSelectionListener should check for (e.getValueIsAdjusting() == false) otherwise you'll respond to all of user's selections and not just the final one.
Yes: don't cache the selections, just process the actual selection in your second list.
If possible, post the code that is executed once your button is pressed. I guess, you have some sort of collection (a list or queue) that stores all the selections you do on the dialog and when you press the button, each stored selection is processed.
This looks like an intended behaviour, because you usually don't code this by accident ;)
If it is intended and you just want to eliminate duplicates, consider using a Set instead of a list, as a Set will only contain unique values.
It does perfectly what it should do.
It fires two events,
1> Selection is removed from first item.
2> Selection is done to second item.
So as fbcocq said, you should check for getValueIsAdjusting(). Check this out, it'll help.
Are you adding an ActionListener to the button every time you enable it?

Categories