我有一個包含具有圖像的自定義列的TableView。其他列由ObservableList填充,但不是自定義填充!JavaFX - 在TableView中更改所選TableRow的TableCell列
在雙擊該行時,我想更改該圖像,但不知道如何獲取TableCell對象,我只獲取包含其他列數據的ObservableList(在我的代碼中查找TODO) 。
Here's一些示例代碼:
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.Callback;
public class TableViewSample extends Application {
private TableView<Person> table = new TableView<>();
private final ObservableList<Person> data =
FXCollections.observableArrayList(
new Person("Jacob", "Smith"),
new Person("Isabella", "Johnson"),
new Person("Ethan", "Williams"),
new Person("Emma", "Jones"),
new Person("Michael", "Brown")
);
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
Scene scene = new Scene(new Group());
stage.setTitle("Table View Sample");
stage.setWidth(450);
stage.setHeight(500);
Label label = new Label("Address Book");
label.setFont(new Font("Arial", 20));
table.setEditable(true);
TableColumn<Person, String> firstNameCol = new TableColumn<>("Name");
firstNameCol.setMinWidth(100);
firstNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName"));
TableColumn<Person, String> lastNameCol = new TableColumn<>("Last Name");
lastNameCol.setMinWidth(100);
lastNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("lastName"));
TableColumn<Person, String> customCol = new TableColumn<>("Status");
customCol.setMinWidth(200);
// Install custom cell renderer
customCol.setCellFactory(new Callback<TableColumn<Person, String>, TableCell<Person, String>>() {
@Override
public TableCell<Person, String> call(TableColumn<Person, String> param) {
TableCell<Person, String> cell = new TableCell<Person, String>() {
ImageView image = new ImageView (new Image ("grey.png"));
@Override
public void updateItem (String item, boolean empty) {
super.updateItem (item, empty);
if (empty) {
setGraphic (null);
setText (null);
}
else {
setGraphic (image);
setText (null);
}
}
};
return cell;
}
});
table.setItems(data);
table.getColumns().addAll(firstNameCol, lastNameCol, customCol);
table.setOnMousePressed ((event) -> {
if (event.getClickCount() == 2) {
System.out.println("double clicked, but dunno know how to change last columns image??");
// TODO Only returns data of the other columns, but nothing from my custom column ...
System.out.println(this.table.getSelectionModel().getSelectedItems());
}
});
VBox vbox = new VBox();
vbox.setSpacing(5);
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll(label, table);
((Group) scene.getRoot()).getChildren().addAll(vbox);
stage.setScene(scene);
stage.show();
}
public static class Person {
private final SimpleStringProperty firstName;
private final SimpleStringProperty lastName;
private Person(String fName, String lName) {
this.firstName = new SimpleStringProperty(fName);
this.lastName = new SimpleStringProperty(lName);
}
public String getFirstName() {
return firstName.get();
}
public void setFirstName(String fName) {
firstName.set(fName);
}
public String getLastName() {
return lastName.get();
}
public void setLastName(String fName) {
lastName.set(fName);
}
}
}
也許有人可以給我意見如何更改的最後一列的形象?
非常感謝和問候
Wiesi;)
如果你想改變圖像,爲什麼不添加一個新的屬性到'Person'? – fabian
我想過,但我不知道如何在我的doubleclick-listener和我的cellFactory裏的updateItem-method中引用這個值?你能否提供一個有效的例子? – Wiesi
您需要一個單元格值工廠以及一個單元工廠。列中的單元格需要某種數據才能顯示。你將該列定義爲一個'TableColumn',所以你需要提供一些字符串數據(雖然我不知道你在這裏設計什麼)。那麼具體是,你想要做什麼?你如何提出從「人」對象獲取圖像?當該行被雙擊時,你想要顯示什麼圖像? –