Please run the code.
Select the first item.
Click the Remove button, which removes the first item.
Click the Show button, which says, that the selected item is "String1", although it is no longer in the list and cannot be the selected item. (The real selected item is in fact "String2")
This only works, if you removed the first item in the list.
This bug might be related toRT-27820 !?
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.layout.VBoxBuilder;
import javafx.stage.Stage;
public class TestApp2 extends Application {
public static void main(String[] args) {
Application.launch();
}
private ObservableList<String> items = FXCollections.observableArrayList("String1", "String2", "String3", "String4");
@Override
public void start(Stage stage) throws Exception {
final ListView<String> listView = new ListView<String>();
listView.setItems(items);
Button button = new Button("Remove");
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
items.remove(listView.getSelectionModel().getSelectedItem());
}
});
Button btnShow = new Button("Show");
btnShow.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
System.out.println(listView.getSelectionModel().getSelectedItem());
}
});
Scene scene = new Scene(VBoxBuilder.create().children(listView, button, btnShow).build());
stage.setScene(scene);
stage.show();
}
}
Select the first item.
Click the Remove button, which removes the first item.
Click the Show button, which says, that the selected item is "String1", although it is no longer in the list and cannot be the selected item. (The real selected item is in fact "String2")
This only works, if you removed the first item in the list.
This bug might be related to
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.layout.VBoxBuilder;
import javafx.stage.Stage;
public class TestApp2 extends Application {
public static void main(String[] args) {
Application.launch();
}
private ObservableList<String> items = FXCollections.observableArrayList("String1", "String2", "String3", "String4");
@Override
public void start(Stage stage) throws Exception {
final ListView<String> listView = new ListView<String>();
listView.setItems(items);
Button button = new Button("Remove");
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
items.remove(listView.getSelectionModel().getSelectedItem());
}
});
Button btnShow = new Button("Show");
btnShow.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
System.out.println(listView.getSelectionModel().getSelectedItem());
}
});
Scene scene = new Scene(VBoxBuilder.create().children(listView, button, btnShow).build());
stage.setScene(scene);
stage.show();
}
}
- relates to
-
JDK-8096530 [TreeView] No selection list update after TreeItem is removed from TreeView.
-
- Resolved
-