Loop for getting selected Index - java

i do have a JList and want to check if any Index is selected or not.
I thought it will work with a loop. I tried everything but everytime my whole frame is blank.
The JButton must stay disabled until any Index is selected.
do {
JButton.setEnabled(false);
} while (JList.getSelectedIndex() == -1);

You can do this by adding a ListSelectionListener to your JList. This is done via the addListSelectionListener method.
From the javadoc:
Adds a listener to the list, to be notified each time a change to the
selection occurs; the preferred way of listening for selection state
changes. JList takes care of listening for selection state changes in
the selection model, and notifies the given listener of each change.
ListSelectionEvents sent to the listener have a source property set to
this list.

Related

Java disable mouse click over the jList

I made a game "Who Wants To Be A Millionaire" with jList filed. In the jList I listed the prises see this picture below
The game is started with the prise number 1 and increase the number if the answer was OK. If I move the mouse over the prises I can modify the prise position with the mouse click as well. This is what I want to disable. The jList need to have only for show the prises without modify with the mouse click.
I also try to use disable of the jList but than all the colors are changed and I don't find where can I adjust the disabled colors.
What is the best solution for my need ?
The simplest way to achieve that is to add a ListSelectionListener to restore the proper index into your JList.
Take a look at an example:
list.addListSelectionListener(new ListSelectionListener() {
#Override
public void valueChanged(ListSelectionEvent e) {
list.setSelectedIndex(myIndex);
}
});
list.setSelectedIndex(0); //0 plays no role, since listener will select myIndex
The selected index will always remain my index, no matter what.
Note: If you want to change the index later, you must change the value of myIndex variable without forgetting firing the selection listener as well. More accurately:
myIndex = 15;
list.setSelectedIndex(0); //0 plays no role, since the selection listener uses myIndex
Another way (more complex in my opinion) is to read and follow Disable JList Cell Selection Property.

How can I check to see if a valueChanged event of a Swing JList was changed via click or mouse?

Here's some example code, using Groovy's swingbuilder to create the code for the valueChanged event of a JList:
mainList.valueChanged = { event ->
if (event.isAdjusting) {
index = mainList.selectedIndex
otherList.clearSelection()
otherIndex = otherList.selectedIndex
} else {
mainListSelected = true
clearJList(otherList)
}
}
I have two JList's, and this function kind of controls which list is allowed to be selected via the mainListSelected variable. We also then have to change int eindex we want to use from the selection based on whether or not it's an index from mainList or otherList
I've read about event.isAdjusting, and it only fires twice like this on a mouse click event. With this knowledge, you would think I would just move everything out of there, but I need certain things to happen differently if the mouse is what causes the event as opposed to using arrows. However, with this code, using arrow key navigation prevents the index from ever changing.

Java Swing - make two JLists "siblings" - i.e. only one item in either can be selected

I have a JPanel which contains two JLists - both can have an item in them selected - as expected.
What I'd love to be able to do is have it so that only one item in either to be selected
Can anyone help me with either
a) "marrying" them so this can be the case
b) giving me some tips for the best practice to write listeners which can preside over them both and unselect all the elements of one when the other is selected - I'd rather avoid this if possible as I can see it getting ugly!!
Thanks :)
I think the best solution, also for the user, is putting a radio button next with a category label to each list, so you clearly disable the other each time you select one.
I can imagine the user clicking values on the first list, then clicking on the next one and seeing all the values he clicked are gone, with logical frustration...
Then when you are taking the values from the form, just take the enabled ones
The listener is not that difficult nor ugly to write. I would
make sure the lists only support single selection
add the same selection listener to both lists' selection model
This listener can be implemented as
public void valueChanged(ListSelectionEvent e){
if ( e.isAdjusting()) return;
ListSelectionModel sourceSelectionModel = (ListSelectionModel) e.getSource();
if ( !sourceSelectionModel.isSelectionEmpty() ){
//still need to implement the findOtherSelectionModel method
ListSelectionModel other = findOtherSelectionModel( sourceSelectionModel );
other.clearSelection();
}
}
Note that clearing the selection will trigger the listener again, but due to the isSelectionEmpty check you will not end up with a loop. Another approach would be to disable the listener (e.g. with a boolean flag) right before you call clearSelection on the other list.

JTree selection without generating event

I have a JTree, a JTable and a JList which displays the same set of objects, but in different order and with different information. If an item is selected from one of the Component, I want to select the same object on the other two Components (meaning they should be highlighted). Naturally I monitor the selection events with a Listener. Here is the problem, when a Component retrieves the selected object, I'll have to make sure the object is selected on the other Components by calling selection methods on them. This, will then notify the selection listeners on the other two components. But each of those events will in turn call selection events on components other than itself, causing an infinite loop going among the three Components.
I see one solution is to use a boolean flag, and make the listeners not propagate the selection if the flag is set. However, this seems cumbersome and not elegant. Is there a way to simply tell JTree, JTable and JList to make the selection but not fire any events (as oppose to fire an event and then catching and stopping it with a boolean flag)?
Take a look at SharedModelDemo. I think it does what you're looking for.
I would use a flag indicating whether it's user changes or internal changes but yu can also remove listeners before selection call and add them after to prevent events firing.

How do I make a listener that fires when the USER selects an item in a JComboBox

I'm looking for a listener that fires ONLY when the user, the one who's using the program, selects an item in the JComboBox. I don't want to use ActionListener or ItemListener because those also fire when I select an item through the program. And I can't use MouseListener either because it only fires when I click the JComboBox, not when I select an item.
I was wondering what the easiest way to do this is? Currently, my solution is messy. When I change the selected item of the jcombobox through code, I set a flag to true. And in my action listener, it only executes if the flag is false.
A) I would recommend you to temporarily remove the listener when you perform the selection programatically.
B) If your programatic change is not an effect of another GUI event you could solve it the following ugly/non-robust/error-prone/"hacky" way: Check EventQueue.isEventDispatchThread() to find out if the click was triggered by the GUI thread (the user).
C) (Oops I just reread your question and saw that you've already discovered the method described below. Basically I would say that this (or the the method described above) is your best alternative.)
Another option is to have a boolean flag called something like nonUserSelection which you set to true before you select a value programatically and reset to false afterwards. In the action listener you simply add an
if (nonUserSelection)
return;

Categories