I've tried to generate a listener for a list view which gets the string stored in the item selected, gets an integer for it and then calls a method. However I keep getting a
Error:(37, 67) java: no suitable method found for addListener(<anonymous javafx.beans.value.ChangeListener<java.lang.String>>)
method javafx.beans.Observable.addListener(javafx.beans.InvalidationListener) is not applicable
(argument mismatch; <anonymous javafx.beans.value.ChangeListener<java.lang.String>> cannot be converted to javafx.beans.InvalidationListener)
method javafx.collections.ObservableList.addListener(javafx.collections.ListChangeListener<? super java.lang.String>) is not applicable
(argument mismatch; <anonymous javafx.beans.value.ChangeListener<java.lang.String>> cannot be converted to javafx.collections.ListChangeListener<? super java.lang.String>)
error. I have a similar listener but it only reads an object from the list not the text.
My listener is as follows:
lstRequests.getSelectionModel().getSelectedItems().addListener(new ChangeListener<String>(){
@Override
public void changed(ObservableValue<? extends String> observable,
String oldValue, String newValue) {
int requestID;
int endIndex = 5;
String requestIDSubString =
newValue.substring(5,endIndex);
boolean isADigit;
do{
isADigit =
isDigit(requestIDSubString.charAt(endIndex));
endIndex++;
requestIDSubString =
newValue.substring(5,endIndex);
} while (isADigit);
endIndex--;
requestID =
Integer.parseInt(newValue.substring(5
,endIndex));
Request selectedRequest = null;
Boolean requestFound = false;
int checkIndex = 0;
do {
Request checkRequest =
requests.get(checkIndex);
if (checkRequest.getRequestID() == requestID){
selectedRequest = checkRequest;
requestFound = true;
}
} while (!requestFound);
populateItems(selectedRequest); //Calls the populate
// items method.
}
});
And my imports are:
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.scene.control.ListView;
import java.util.ArrayList;
import static java.lang.Character.isDigit;
Any ideas how to fix this?
Currently you're trying to add a ChangeListener
to a ObservableList
. getSelectedItems()
returns an ObservableList
containing all the selected items. ObservableList
s don't allow you to add a ChangeListener
.
If you want to use a listener for a single selection, you should add the listener to the selectedItem
property:
lstRequests.getSelectionModel().selectedItemProperty().addListener(...);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.