如何双向绑定到设置为表视图的选定项属性的对象的属性?

时间:2015-09-02 09:37:54

标签: java binding javafx javafx-8

我有一个如下所示的用户界面:

Example User Interface

而我想要的是当你点击表格中的1时,文本框显示表格中的2和3,它显示4,当你在文本框中输入时,所选行的对象上的属性必将得到更新。

所以我有一组具有两个属性的对象(value1 - 在表中,value2在文本框中)。

现在的复杂性:我希望使用双向绑定,而不是事件监听器

基本上,文本框的绑定可以这样描述:

  

将text属性双向绑定到表视图的选定项属性的value2属性

这有可能吗?

我正在摆弄,我得到了这个,但它不是双向的,当然(我希望可能必须提供第二个回调以反向):

this.textField.textProperty().bind(         
        Bindings.createStringBinding(() -> {

            if (this.tableView.getSelectionModel().getSelectedItem() == null) {
                return "";
            }

            return String.valueOf(this.tableView.getSelectionModel().getSelectedItem().value2Property().get());
        }, 
        this.tableView.getSelectionModel().selectedItemProperty()));

以下是我所谈论的一个完整的工作示例,如果它更清楚:

import java.net.URL;
import java.util.ResourceBundle;

import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

public final class Program extends Application {

    private static final class SomeSortOfController implements Initializable {

        @FXML private TableView<SomeSortOfObject> tableView;
        @FXML private TableColumn<SomeSortOfObject, Number> value1TableColumn;
        @FXML private TextField textField;

        @Override
        public void initialize(final URL url, final ResourceBundle resourceBundle) {

            this.tableView.setItems(FXCollections.observableArrayList(
                    new SomeSortOfObject(1,2),
                    new SomeSortOfObject(3,4)));

            this.value1TableColumn.setCellValueFactory(c -> c.getValue().value1Property());
            this.value1TableColumn.setEditable(false);

            // Here I need to come up with some sort of binding from the selected item
            // in the table view to the value2 property in the SomeSortOfObject (and
            // vice versa)
        }
    }

    private static final class SomeSortOfObject {

        private final IntegerProperty value1;
        private final IntegerProperty value2;

        public SomeSortOfObject(int value1, int value2) {
            this.value1 = new SimpleIntegerProperty(value1);
            this.value2 = new SimpleIntegerProperty(value2);
        }

        public IntegerProperty value1Property() {
            return this.value1;
        }

        public IntegerProperty value2Property() {
            return this.value2;
        }
    }

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

    @Override
    public void start(final Stage stage) throws Exception {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("/main.fxml"));
        loader.setController(new SomeSortOfController());   
        stage.setScene(new Scene(loader.load(), 200, 100));     
        stage.show();
    }
}

为了完全完成,这是main.fxml文件的内容:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<HBox xmlns="http://javafx.com/javafx/8.0.45" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <TableView fx:id="tableView">
        <columns>
          <TableColumn text="value1" fx:id="value1TableColumn" />
        </columns>
      </TableView>
      <TextField fx:id="textField" />
   </children>
</HBox>

1 个答案:

答案 0 :(得分:1)

没有标准的API方法可以做到这一点。有一些第三方库提供此功能。可能最着名的是EasyBind,这个库可能会在版本9中合并到JavaFX中。

这是使用EasyBind的SSCCE:

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

import org.fxmisc.easybind.EasyBind;
import org.fxmisc.easybind.monadic.PropertyBinding;

public class BidirectionalBindingToNestedProperty extends Application {

    private PropertyBinding<String> selectedValue2;

    @Override
    public void start(Stage primaryStage) {
        TableView<Item> table = new TableView<>();
        TableColumn<Item, String> col1 = new TableColumn<>("Value 1");
        col1.setCellValueFactory(cellData -> cellData.getValue().value1Property());

        table.getColumns().add(col1);

        TextField textField = new TextField();
        selectedValue2 = EasyBind
                .monadic(table.getSelectionModel().selectedItemProperty())
                .selectProperty(Item::value2Property);
        textField.textProperty().bindBidirectional(selectedValue2);

        for (int i = 1 ; i <= 40; i+=2) {
            Item item = new Item(String.valueOf(i), String.valueOf(i + 1));
            item.value2Property().addListener((obs, oldValue, newValue) -> 
                System.out.println("Item with value1 = "+item.getValue1() + " changed value2 from "+oldValue+" to "+newValue));
            table.getItems().add(item);
        }

        BorderPane root = new BorderPane(table, null, null, null, textField);

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

    public static class Item {
        private final StringProperty value1 = new SimpleStringProperty();
        private final StringProperty value2 = new SimpleStringProperty();

        public Item(String value1, String value2) {
            setValue1(value1);
            setValue2(value2);
        }

        public final StringProperty value1Property() {
            return this.value1;
        }

        public final java.lang.String getValue1() {
            return this.value1Property().get();
        }

        public final void setValue1(final java.lang.String value1) {
            this.value1Property().set(value1);
        }

        public final StringProperty value2Property() {
            return this.value2;
        }

        public final java.lang.String getValue2() {
            return this.value2Property().get();
        }

        public final void setValue2(final java.lang.String value2) {
            this.value2Property().set(value2);
        }


    }

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