1. TableView Examples
TableItem.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package javafxutility; import javafx.beans.property.IntegerProperty; import javafx.beans.property.SimpleIntegerProperty; import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.StringProperty; public class TableItem { public StringProperty name; public IntegerProperty size; public TableItem(String name, Integer size) { this.name = new SimpleStringProperty(name); this.size = new SimpleIntegerProperty(size); } } |
Controller.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | void initTable() { ObservableList<TableItem> list = FXCollections.observableArrayList( new TableItem( "name_1", 100), new TableItem( "name_2", 200), new TableItem( "name_3", 300) ); tableColName.setCellValueFactory(cellData -> cellData.getValue().name); tableColSize.setCellValueFactory(cellData -> cellData.getValue().size.asObject()); tableView.setItems(list); tableView.getSelectionModel().selectFirst(); tableView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<TableItem>() { @Override public void changed(ObservableValue<? extends TableItem> observable, TableItem oldValue, TableItem newValue) { Alert alert = new Alert(AlertType.INFORMATION); alert.setTitle("JavaFXUtility"); alert.setHeaderText(null); alert.setContentText(String.format("name=%s\nsize=%s", newValue.name.get(), newValue.size.get())); alert.showAndWait(); } }); } |
Result:
2. Handle Item Click Event with Selected Item
1 2 3 4 5 6 7 8 9 10 | tableView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<TableItem>() { @Override public void changed(ObservableValue<? extends TableItem> observable, TableItem oldValue, TableItem newValue) { Alert alert = new Alert(AlertType.INFORMATION); alert.setTitle("JavaFXUtility"); alert.setHeaderText(null); alert.setContentText(String.format("name=%s\nsize=%s", newValue.name.get(), newValue.size.get())); alert.showAndWait(); } }); |
3. Handle Item Click Event with Index
1 2 3 4 5 6 7 8 | tableView.getSelectionModel().selectedIndexProperty().addListener( (ObservableValue<? extends Number> observable, Number oldValue, Number newValue) -> { Alert alert = new Alert(AlertType.INFORMATION); alert.setTitle("JavaFXUtility"); alert.setHeaderText(null); alert.setContentText(String.format("old=%d\nnew=%d", oldValue, newValue)); alert.showAndWait(); }); |
댓글 없음:
댓글 쓰기