Reputation: 185
When I test JavaFx 8 ListView, I have encountered a strange problem. This is code:
Main.java:
package application;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
public class Main extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Test.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
TestController.java:
package application;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.collections.ObservableList;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener.Change;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
public class TestController implements Initializable
{
@FXML
ListView<String> listView;
private ArrayList <String> list;
@Override
public void initialize(URL url, ResourceBundle rb)
{
listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
fillList();
listView.getSelectionModel().getSelectedItems().addListener(
(Change<? extends String> change) ->
{
list.clear();
ObservableList<String> oList = listView.getSelectionModel().getSelectedItems();
System.out.println(oList);
});
}
private void fillList()
{
list = new ArrayList<>();
list.add("1.item");
list.add("2.item");
list.add("3.item");
ObservableList<String> items = FXCollections.observableArrayList(list);
listView.setItems(items);
}
}
Test.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane" prefHeight="399.0" prefWidth="320" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.TestController">
<children>
<ListView fx:id="listView" layoutX="14.0" layoutY="85.0" prefHeight="200.0" prefWidth="200.0" />
</children>
</AnchorPane>
When I selected first 1.item, then 2.item, then deselected 1.item (with ctrl); listView.getSelectionModel().getSelectedItems()
return null, program output is:
[1.item]
[1.item, 2.item]
[null]
but when I selected first 2.item, then 1.item, then deselected 2.item (with ctrl); result is normal, program output is:
[2.item]
[1.item, 2.item]
[1.item]
What's the problem?
Upvotes: 1
Views: 1488
Reputation: 185
Through the problem in my question arise from a bug, I share my tricky way for this situation (I used getSelectedItem()
method when getSelectedItems()
return null). Changed TestController.java:
package application;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.collections.ObservableList;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener.Change;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
public class TestController implements Initializable
{
@FXML
ListView<String> listView;
private ArrayList <String> list;
@Override
public void initialize(URL url, ResourceBundle rb)
{
listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
fillList();
listView.getSelectionModel().getSelectedItems().addListener(
(Change<? extends String> change) ->
{
list.clear();
ObservableList<String> oList = listView.getSelectionModel().getSelectedItems();
if(oList != null)
System.out.println(oList);
else
System.out.println(listView.getSelectionModel().getSelectedItem());
});
}
private void fillList()
{
list = new ArrayList<>();
list.add("1.item");
list.add("2.item");
list.add("3.item");
ObservableList<String> items = FXCollections.observableArrayList(list);
listView.setItems(items);
}
}
Upvotes: 1