简体   繁体   English

JavaFX-在TableView中更改所选TableRow的TableCell列

[英]JavaFX - Change TableCell column of selected TableRow in a TableView

I have a TableView containing a custom column with an image. 我有一个TableView包含一个带有图像的自定义列。 The other columns get filled by an ObservableList, but not the custom one! 其他列由ObservableList填充,而不是自定义列!

On double-clicking the row, I want to change that image but don´t know how to get that TableCell-object, I only get the ObservableList containing the data of the other columns (look for TODO in my code). 双击该行时,我想更改该图像,但不知道如何获取该TableCell对象,只得到包含其他列数据的ObservableList(在我的代码中查找TODO)。

Here´s some sample code: 这里是一些示例代码:

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);
        }
    }

}

Maybe someone could give me advice how to change the image of the last column? 也许有人可以给我建议如何更改最后一列的图像?

Many thanks and best regards 致以真诚的感谢和诚挚的问候

Wiesi ;) 威西;)

Your question isn't very precise, but maybe this helps. 您的问题不是很精确,但这也许会有所帮助。 This example shows a table of Item s. 此示例显示了Item的表。 It maintains a set of Item s that have been validated successfully. 它维护一组已成功验证的Item (In this example an item is valid if its value is even.) The third column shows an image that is a red rectangle if the item has not been successfully validated (ie is not valid, or validation has not been performed) and a green rectangle if it has been validated (ie it has an even value and validation has been performed). (在此示例中,如果项目的值是偶数,则该项目有效。)第三列显示的图像为红色矩形,如果该项目尚未成功验证(即无效或未执行验证),则绿色矩形(如果已通过验证)(即,其值为偶数且已执行验证)。 Double-clicking a row in the table will perform validation on that item if it has not been successfully validated (so double clicking an even-valued item will turn its image green, double clicking an odd-valued item will have no effect). 如果未成功通过验证,则双击表中的行将对该项目执行验证(因此,双击一个偶数值的项目将使其图像变为绿色,双击一个奇数值的项目将无效)。 (Double-clicking an already-validated item will remove it from the validated set, which makes testing easier.) (双击已验证的项目会将其从验证的集合中删除,这使测试更加容易。)

The way it works is to maintain an ObservableSet<Item> representing the validated items. 它的工作方式是维护一个表示已验证项目的ObservableSet<Item> The cell value factory returns a boolean binding that observes that set and evaluates to true if the item is in the set and false otherwise. 单元格值工厂返回一个布尔绑定,该布尔绑定观察该集合,如果该项在集合中,则评估为true,否则为false。 The cell factory just returns a cell that checks the boolean value and sets the appropriate image. 单元格工厂只返回一个单元格,该单元格检查布尔值并设置适当的图像。

import java.util.Random;
import java.util.function.Function;

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableSet;
import javafx.scene.Scene;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class TableWithValidationColumn extends Application {

    private final Image validImage = createImage(20, 20, Color.GREEN);
    private final Image invalidImage = createImage(20, 20, Color.RED);

    @Override
    public void start(Stage primaryStage) {
        TableView<Item> table = new TableView<>();
        table.getColumns().add(column("Item", Item::nameProperty));
        table.getColumns().add(column("Value", Item::valueProperty));

        TableColumn<Item, Boolean> validColumn = new TableColumn<>("Validated");

        ObservableSet<Item> validItems = FXCollections.observableSet();

        validColumn.setCellValueFactory(cellData -> {
            Item item = cellData.getValue() ;
            return Bindings.createBooleanBinding(() -> validItems.contains(item), validItems);
        });

        validColumn.setCellFactory(col -> new TableCell<Item, Boolean>() {
            private final ImageView imageView = new ImageView();
            @Override
            protected void updateItem(Boolean valid, boolean empty) {
                super.updateItem(valid, empty);
                if (empty) {
                    setGraphic(null);
                } else {
                    if (valid) {
                        imageView.setImage(validImage);
                    } else {
                        imageView.setImage(invalidImage);
                    }
                    setGraphic(imageView);
                }
            }
        });

        table.getColumns().add(validColumn);

        // validate on double-click:

        table.setRowFactory(tv -> {
            TableRow<Item> row = new TableRow<>();
            row.setOnMouseClicked(e -> {
                if ((! row.isEmpty())  && e.getClickCount() == 2) {
                    Item item = row.getItem() ;
                    if (validItems.contains(item)) {
                        validItems.remove(item);
                    } else {
                        if (validate(item)) {
                            validItems.add(item);
                        }
                    }
                }
            });
            return row ;
        });

        Random rng = new Random();
        for (int i = 1 ; i <= 100 ; i++) {
            table.getItems().add(new Item("Item "+i, rng.nextInt(100)));
        }

        BorderPane root = new BorderPane(table);
        Scene scene = new Scene(root, 600 ,600);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private boolean validate(Item item) {
        // items with even values validate:
        return item.getValue() % 2 == 0;
    }

    private static <S,T> TableColumn<S,T> column(String text, Function<S, ObservableValue<T>> property) {
        TableColumn<S,T> col = new TableColumn<>(text);
        col.setCellValueFactory(cellData -> property.apply(cellData.getValue()));
        return col ;
    }

    private final Image createImage(int w, int h, Color color) {
        Rectangle rect = new Rectangle(w, h, color);
        return rect.snapshot(null, null);
    }

    public static void main(String[] args) {
        launch(args);
    }

    public static class Item {
        private final StringProperty name = new SimpleStringProperty();
        private final IntegerProperty value = new SimpleIntegerProperty();

        public Item(String name, int value) {
            setName(name);
            setValue(value);
        }

        public final StringProperty nameProperty() {
            return this.name;
        }


        public final String getName() {
            return this.nameProperty().get();
        }


        public final void setName(final String name) {
            this.nameProperty().set(name);
        }


        public final IntegerProperty valueProperty() {
            return this.value;
        }


        public final int getValue() {
            return this.valueProperty().get();
        }


        public final void setValue(final int value) {
            this.valueProperty().set(value);
        }



    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM