簡體   English   中英

TableView的JavaFX屬性適配器

[英]JavaFX property adapter for TableView

對於DTO,我使用POJO。 所以為了進行雙向綁定,我創建了適配器。 我的意思是,像這樣:

POJO:

public class Temp{
   private BigDecimal weight;
   private final PropertyChangeSupport propertyChangeSupport;
   public Temp() {
        this.propertyChangeSupport = new PropertyChangeSupport(this);
   }
   public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.addPropertyChangeListener(listener);
   }
   public BigDecimal getWeight() {
     return weight;
   }
   public void setWeight(BigDecimal weight) {
     BigDecimal pv = this.weight;
    this.weight = weight;
    propertyChangeSupport.firePropertyChange("weight", pv, weight);
   }
}

我有以下適配器:

public class TempAdapter {
    private ObjectProperty<BigDecimal> weightProperty;
    public TempAdapter(Temp temp) {
        try {
            weightProperty=new JavaBeanObjectPropertyBuilder<BigDecimal>().bean(temp).name("weight").build();
            weightProperty.addListener(new ChangeListener<BigDecimal>() {
                @Override
                public void changed(ObservableValue<? extends BigDecimal> ov, BigDecimal t, BigDecimal t1) {
                  ....
                }
            });
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
        }
    }
 public ObjectProperty<BigDecimal> getWeightProperty() {
    return weightProperty;
}

但是,我無法理解如何將此適配器與TableView一起使用。 我想為TableView使用適配器的原因是,如果我們使用POJO for DTO和TableView,我們將不得不在TableView中復制適配器的代碼。

據我了解TableView中的每一行,我們必須創建一個新的適配器實例,我無法理解如何做到這一點。

沒有適配器類的解決方案

首先請注意,您不一定需要適配器類; 您可以在需要的地方創建JavaBeanProperty實例:在本例中為表的單元格值工廠。 如果UI中只有一個(或兩個)位置需要直接綁定到與POJO中的屬性對應的JavaFX屬性,那么這可能就是這樣。

以下是使用通常的Oracle Person例的此技術的完整示例。 在此示例中,沒有適配器類:該表只是在單元格值工廠中創建JavaBeanStringProperty適配器。 有一個編輯表單,它直接與POJO類交互。

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

import javafx.application.Application;
import javafx.beans.property.adapter.JavaBeanStringProperty;
import javafx.beans.property.adapter.JavaBeanStringPropertyBuilder;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class PojoTable extends Application {

    @Override
    public void start(Stage primaryStage) {
        TableView<Person> table = new TableView<>();
        table.setEditable(true);

        TableColumn<Person, String> firstNameColumn = createColumn("First Name", "firstName");
        TableColumn<Person, String> lastNameColumn = createColumn("Last Name", "lastName");

        table.getColumns().add(firstNameColumn);
        table.getColumns().add(lastNameColumn);


        Button button = new Button("Show data");
        button.setOnAction(e -> {
            table.getItems().stream().map(person -> person.getFirstName() + " " + person.getLastName())
                .forEach(System.out::println);
            System.out.println();
        });

        Button edit = new Button("Edit");
        edit.disableProperty().bind(table.getSelectionModel().selectedItemProperty().isNull());
        edit.setOnAction(e -> edit(table.getSelectionModel().getSelectedItem(), primaryStage));

        table.getItems().addAll(
                new Person("Jacob", "Smith"),
                new Person("Isabella", "Johnson"),
                new Person("Ethan", "Williams"),
                new Person("Emma", "Jones"),
                new Person("Michael", "Brown")
        );

        HBox buttons = new HBox(10, button, edit);
        buttons.setAlignment(Pos.CENTER);

        BorderPane root = new BorderPane(table, null, null, buttons, null);
        BorderPane.setAlignment(buttons, Pos.CENTER);
        BorderPane.setMargin(buttons, new Insets(10));
        root.setPadding(new Insets(10));
        primaryStage.setScene(new Scene(root, 600, 600));
        primaryStage.show();
    }

    private void edit(Person person, Stage primaryStage) {
        GridPane editPane = new GridPane();
        TextField firstNameField = new TextField(person.getFirstName());
        TextField lastNameField = new TextField(person.getLastName());
        Button okButton = new Button("OK");
        Button cancelButton = new Button("Cancel");
        HBox buttons = new HBox(10, okButton, cancelButton);

        editPane.addRow(0, new Label("First Name:"), firstNameField);
        editPane.addRow(1, new Label("Last Name:"), lastNameField);
        editPane.add(buttons, 0, 2, 2, 1);

        GridPane.setHalignment(buttons, HPos.CENTER);
        GridPane.setMargin(buttons, new Insets(10));

        editPane.setPadding(new Insets(10));

        Scene scene = new Scene(editPane);
        Stage stage = new Stage();
        stage.setScene(scene);

        stage.initOwner(primaryStage);
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.initStyle(StageStyle.UNDECORATED);

        cancelButton.setOnAction(e -> stage.hide());
        okButton.setOnAction(e -> {
            person.setFirstName(firstNameField.getText());
            person.setLastName(lastNameField.getText());
            stage.hide();
        });

        stage.show();
    }

    private TableColumn<Person, String> createColumn(String title, String property) {
        TableColumn<Person, String> col = new TableColumn<>(title);
        col.setCellValueFactory(cellData -> {
            Person p = cellData.getValue();
            try {
                JavaBeanStringProperty prop = new JavaBeanStringPropertyBuilder()
                    .bean(p)
                    .name(property)
                    .build();
                return prop;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
        col.setCellFactory(TextFieldTableCell.forTableColumn());
        return col ;
    }

    public static class Person {
        private String firstName ;
        private String lastName ;

        private PropertyChangeSupport support ;

        public Person(String firstName, String lastName) {
            this.firstName = firstName ;
            this.lastName = lastName ;

            support = new PropertyChangeSupport(this);
        }

        public String getFirstName() {
            return firstName;
        }

        public void setFirstName(String firstName) {
            String previous = this.firstName ;
            this.firstName = firstName;
            support.firePropertyChange("firstName", previous, firstName);
        }

        public String getLastName() {
            return lastName;
        }

        public void setLastName(String lastName) {
            String previous = this.lastName ;
            this.lastName = lastName;
            support.firePropertyChange("lastName", previous, lastName);
        }

        public void addPropertyChangeListener(PropertyChangeListener listener) {
            support.addPropertyChangeListener(listener);
        }

        public void removePropertyChangeListener(PropertyChangeListener listener) {
            support.removePropertyChangeListener(listener);
        }
    }

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

使用適配器類的解決方案

請注意,在上面的示例中,編輯器中的文本字段不能直接與POJO類一起使用綁定(因為它不公開任何JavaFX屬性); 如果你想這樣做,你可以為此目的創建更多的JavaBeanStringProperty ,但這最終會復制代碼。 如果您希望能夠這樣做,那么使用適配器類可能會有所幫助。 以下是使用此解決方案的代碼。 請注意,現在適配器類公開了JavaFX屬性,因此表的單元格值工廠可以直接映射到這些屬性: JavaBeanStringProperty的創建被封裝在一個位置(適配器類):

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;

import javafx.application.Application;
import javafx.beans.property.StringProperty;
import javafx.beans.property.adapter.JavaBeanStringPropertyBuilder;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class PojoTable extends Application {

    @Override
    public void start(Stage primaryStage) {
        TableView<PersonAdapter> table = new TableView<>();
        table.setEditable(true);

        TableColumn<PersonAdapter, String> firstNameColumn = createColumn("First Name", PersonAdapter::firstNameProperty);
        TableColumn<PersonAdapter, String> lastNameColumn = createColumn("Last Name", PersonAdapter::lastNameProperty);

        table.getColumns().add(firstNameColumn);
        table.getColumns().add(lastNameColumn);

        List<Person> data = Arrays.asList(
            new Person("Jacob", "Smith"),
            new Person("Isabella", "Johnson"),
            new Person("Ethan", "Williams"),
            new Person("Emma", "Jones"),
            new Person("Michael", "Brown")
        );


        Button button = new Button("Show data");
        button.setOnAction(e -> {
            data.stream().map(person -> person.getFirstName() + " " + person.getLastName())
                .forEach(System.out::println);
            System.out.println();
        });

        Button edit = new Button("Edit");
        edit.disableProperty().bind(table.getSelectionModel().selectedItemProperty().isNull());
        edit.setOnAction(e -> edit(table.getSelectionModel().getSelectedItem(), primaryStage));

        data.stream().map(PersonAdapter::new).forEach(table.getItems()::add);

        HBox buttons = new HBox(10, button, edit);
        buttons.setAlignment(Pos.CENTER);

        BorderPane root = new BorderPane(table, null, null, buttons, null);
        BorderPane.setAlignment(buttons, Pos.CENTER);
        BorderPane.setMargin(buttons, new Insets(10));
        root.setPadding(new Insets(10));
        primaryStage.setScene(new Scene(root, 600, 600));
        primaryStage.show();
    }

    private void edit(PersonAdapter person, Stage primaryStage) {
        GridPane editPane = new GridPane();
        TextField firstNameField = new TextField();
        firstNameField.textProperty().bindBidirectional(person.firstNameProperty());

        TextField lastNameField = new TextField();
        lastNameField.textProperty().bindBidirectional(person.lastNameProperty());

        Button okButton = new Button("OK");
        HBox buttons = new HBox(10, okButton);

        editPane.addRow(0, new Label("First Name:"), firstNameField);
        editPane.addRow(1, new Label("Last Name:"), lastNameField);
        editPane.add(buttons, 0, 2, 2, 1);

        GridPane.setHalignment(buttons, HPos.CENTER);
        GridPane.setMargin(buttons, new Insets(10));

        editPane.setPadding(new Insets(10));

        Scene scene = new Scene(editPane);
        Stage stage = new Stage();
        stage.setScene(scene);

        stage.initOwner(primaryStage);
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.initStyle(StageStyle.UNDECORATED);

        okButton.setOnAction(e -> {
            stage.hide();
        });

        stage.show();
    }

    private TableColumn<PersonAdapter, String> createColumn(String title, Function<PersonAdapter, StringProperty> property) {
        TableColumn<PersonAdapter, String> col = new TableColumn<>(title);
        col.setCellValueFactory(cellData -> property.apply(cellData.getValue()));
        col.setCellFactory(TextFieldTableCell.forTableColumn());
        return col ;
    }

    public static class Person {
        private String firstName ;
        private String lastName ;

        private PropertyChangeSupport support ;

        public Person(String firstName, String lastName) {
            this.firstName = firstName ;
            this.lastName = lastName ;

            support = new PropertyChangeSupport(this);
        }

        public String getFirstName() {
            return firstName;
        }

        public void setFirstName(String firstName) {
            String previous = this.firstName ;
            this.firstName = firstName;
            support.firePropertyChange("firstName", previous, firstName);
        }

        public String getLastName() {
            return lastName;
        }

        public void setLastName(String lastName) {
            String previous = this.lastName ;
            this.lastName = lastName;
            support.firePropertyChange("lastName", previous, lastName);
        }

        public void addPropertyChangeListener(PropertyChangeListener listener) {
            support.addPropertyChangeListener(listener);
        }

        public void removePropertyChangeListener(PropertyChangeListener listener) {
            support.removePropertyChangeListener(listener);
        }
    }

    public static class PersonAdapter {
        private final Person person ;

        private final StringProperty firstName ;
        private final StringProperty lastName ;

        public PersonAdapter(Person person) {
            this.person = person ;

            try {
                this.firstName = new JavaBeanStringPropertyBuilder()
                    .bean(person)
                    .name("firstName")
                    .build();

                this.lastName = new JavaBeanStringPropertyBuilder()
                    .bean(person)
                    .name("lastName")
                    .build();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

        }

        public Person getPerson() { 
            return person ;
        }

        public final StringProperty firstNameProperty() {
            return this.firstName;
        }

        public final String getFirstName() {
            return this.firstNameProperty().get();
        }

        public final void setFirstName(final String firstName) {
            this.firstNameProperty().set(firstName);
        }

        public final StringProperty lastNameProperty() {
            return this.lastName;
        }

        public final String getLastName() {
            return this.lastNameProperty().get();
        }

        public final void setLastName(final String lastName) {
            this.lastNameProperty().set(lastName);
        }


    }

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

這種方法的一個可能的缺點是底層列表的更改(簡單示例中的data )不會傳播到表(這意味着從data添加或刪除元素不會更改表;在現有元素上調用setFirstNamesetLastName該表將允許更新)。 有關管理此方法的技巧,請參閱裝飾ObservableList並保留更改事件的最佳實踐

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM