I want to make list selection such that when I first select an item it get selected after that when I select second item then both should get selected that is first selection should remain as it is. I have set list selection mode to multiple selection. But still has to press ctrl key to do the thing. I want to do it without pressing ctrl key.
How to remain list item selected?
Here is the line where I set the selection mode:
list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
Edit:
AS suggested by StanislavL I tried following but it did not worked for me.
DefaultListSelectionModel model = new DefaultListSelectionModel();
model.removeSelectionInterval(0, 2);
user_list.setSelectionModel(model);
It is a real well known convention that multi select requires pressing the Ctrl key. Same with using the Shift key to select a range of values.
Personally I would never spend effort on changing this behaviour, because it would confuse users that are already familiar with other applications.
Try to replace ListSelectionModel in the list. Use
public void setSelectionModel(ListSelectionModel selectionModel)
You can use the DefaultListSelectionModel and override
public void removeSelectionInterval(int index0, int index1)
call super.remove() if the items are already selected.
Related
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.
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.
Could anyone please tell me what i should do programmatically to be able to select an item in a listbox using the keyboard when there are multiple items starting with the same character/s. For eg,
•One
•Two
•Three
•Once
•Orange
If i want to get the focus on "Once" by typing o,n,c what should i do? Instead of jumping from one item to the other as opposed to the default behaviour.
Add a KeyPress event handler to the ListBox and track the keys that are presses. Then compare the complete value that already has been typed to the values from the items in the ListBox. If there's a match, select the item.
I'm new to GWT. I have a simple SuggestBox which is populated using a MultiWordSuggestOracle. Users input their data to this SuggestBox, and if they find any match with the existing Suggestions its well and good. I'm able to retrieve this value in the SelectionHandler code as below.
display.getSuggestBox().addSelectionHandler(new SelectionHandler<Suggestion>() {
public void onSelection(SelectionEvent<Suggestion> event) {
String selectedProperty = ((SuggestBox)event.getSource()).getValue();
// do something with the property value
}
});
But users are allowed to enter values which are not already in the Suggestion oracle, in which case I should read this value and do something with this,may be saving to db as a new data.(The thing which I'm looking for is something like a browsers navigation widget where we show suggestions, users can pick up any suggestion or he can type in his new entry and carry on.) What I needed is a way to retrieve this new text user has entered? Data will be read on a button click. What I tried out is this.
display.getSaveBtn().addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
String selectedProperty = display.getSuggestBox().getValue();
//String selectedProperty2 = display.getSuggestBox().getText();
// Blank in both cases :(
// tried display.getSuggestBox().getTextBox().getValue(),but blank again
}
});
I tried to employ onChange() event handlers (as shown below)
display.getSuggestBox().addValueChangeHandler(new ValueChangeHandler<String>() {
public void onValueChange(ValueChangeEvent<String> event) {
String selectedProperty = ((SuggestBox)event.getSource()).getValue();
Window.alert("on change -- "+selectedProperty);
}
});
This is working fine except one scenario. Suppose there are two suggestions in the oracle,say 'createTicketWsdl' and 'createTicketTimeout'. When the user types in 'cr', he is opted with these two options, and if he selects 'createTicketWsdl' by pressing keyboard ENTER, then my alert is printing 'createTicketWsdl' which is correct. But if he selects 'createTicketWsdl' using mouse, then my alert is printing 'cr' (I tried to post the screenshot to give a better understanding, but being a new user I'm not allowed).(which I wanted to get as 'createTicketWsdl'since thats what he has selected). Soon after printing my alert, the value in the SuggestBox changes to 'createTicketWsdl'.
Is there a way to retrieve the value of the suggest box? I saw a similiar thread GWT SuggestBox + ListBox Widget, where some source code for a custom widget is available. But I didn't take the pain of trying out that, since what I want is simply get the current value from the SuggestBox and I hope there should be some easy way.
Thanks for all your help!
Your question is not very clear. You need to clarify your language a lil' bit. For example - is the following a question or an assertion? I mean, it sounds like an assertion but it has a question mark.
What I needed is a way to retrieve this new text user has entered?
Also, I do not understand what you mean by "he is opted by". Did you mean to say, "he is presented with the options ..." ?
Therefore, I am guessing your situation.
You have a listbox of existing items.
You have a textbox which allows freeform text entry
Any items whose prefix values matches the current textbox entry, the listbox items would be filtered to be limited to the matching items.
Even if the current textbox entry presents matching prefixes to filtering the listbox, the user can still perform freeform text entry. So, there are two possible cases here
4.1 the user clicks on the list box to select one of the filtered items
4.2 the user press enter key, which triggers selection of the current value of the textbox.
However, you find your widget participating in a race condition, so that when you click on the widget, the ValueChangeHandler gets triggered rather than the SelectionHandler. I do not know the structure of your widget so that is my best guess.
The problem is that you are allowing two separate modes of obtaining an outcome and you probably did not have well-defined state machine to handle choosing the appropriate mode. One mode is by the textbox and the other is by selection on the listbox - and you do not have a well-defined way of which would mode would be effective at any moment.
If my guess is accurate, this is what you need to do:
You must restrict your outcome to coming from only the textbox.
Your listbox selection must not trigger any outcome. Any change in listbox selection must propagate back to the textbox - to allow the user the chance of making further freeform entry based on that value.
only the keyboard enter on the textbox will trigger the final outcome.
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?