I have created the following pane:
1: <Button id="button" layoutX="126" layoutY="90" text="Click Me!" onAction="#handleButtonAction" fx:id="button" />2: <Label id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" prefHeight="16" prefWidth="69" fx:id="label" />3:4: <ListView fx:id="myList" id="myList" layoutX="200" layoutY="200" minWidth="100" minHeight="140" />5: </Pane>
In the controller I had the following code :
1: import java.net.URL;2: import java.util.ResourceBundle;3: import javafx.collections.FXCollections;4: import javafx.collections.ObservableList;5: import javafx.event.ActionEvent;6: import javafx.fxml.FXML;7: import javafx.fxml.Initializable;8: import javafx.scene.control.Label;9: import javafx.scene.control.ListView;10:11: public class FXMLController implements Initializable {12:13: ObservableList<String> items;14:15: @Override16: public void initialize(URL url, ResourceBundle rb) {17: ListView<String> list = new ListView<String>();18: items =FXCollections.observableArrayList (19: "Cat", "Dog", "Donkey", "Pig");20: myList.setItems(items);21:22: myList.setPrefWidth(100);23: myList.setPrefHeight(70);24:25: }26: @FXML27: private ListView myList;28:29: @FXML30: private Label label;31:32: @FXML33: private void handleButtonAction(ActionEvent event) {34: System.out.println("Remove the fst item !");35: label.setText("Hello World!");36: items.remove(0);37: }38: }
Every click on the button removes the first item from the list behave like ObservationCollection in WPF.
When there are no items left in the list clicking the button prompt an exception to the console :
Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:604)
at java.util.ArrayList.remove(ArrayList.java:445)
at com.sun.javafx.collections.ObservableListWrapper.remove(ObservableListWrapper.java:251)
at javafxapplication1.FXMLController.handleButtonAction(FXMLController.java:46)
... 62 more
But the application doesn’t hung.
אין תגובות:
הוסף רשומת תגובה