TableView绑定和Object HAS-A关系

时间:2017-04-01 12:45:52

标签: java javafx

我有以下课程

public class MulticastReceiver implements Runnable {

    final static String INET_ADDR = "239.192.0.1";

    public void run() {
        // Get the address that we are going to connect to.
        InetAddress address = null;
        try {
            address = InetAddress.getByName(INET_ADDR);
            MulticastSocket clientSocket = new MulticastSocket(8080);

            // if I set it manually it works (here goes the IP of my PC n°2)
            //clientSocket.setInterface(InetAddress.getByName("IP of my second PC"));

            // Create a buffer of bytes, which will be used to store
            // the incoming bytes containing the information from the server.
            // Since the message is small here, 256 bytes should be enough.
            byte[] buf = new byte[256];

            //Join the Multicast group.
            clientSocket.joinGroup(address);

            while (true) {
                // Receive the information and print it.
                DatagramPacket msgPacket = new DatagramPacket(buf, buf.length);
                clientSocket.receive(msgPacket);
                String msg = new String(buf, 0, buf.length);
                System.out.println("Received msg: " + msg);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

我有tableView Class Car{ private StringProperty type; //+setters and getters } Class Person{ private Car car; private StringProperty name; //+setters and getters } 。在这个tableView中我有两列 - 人名和车型。

这是我为人名

建立列的方法
TableView<Person>

你可以看到,类Person中的Car可以有两个变化。首先,一个人可以买另一辆车,其次车可以改变它的类型。

如何构建列车类型并绑定到&#34;这两个更改&#34;

2 个答案:

答案 0 :(得分:3)

Bindings类的select* methods就是这样做的:

carTypeColumn.setCellValueFactory(
    data -> Bindings.selectString(data.getValue(), "car", "type"));

请注意,getter和setter应遵循JavaFX约定,就像所有JavaFX类一样:

class Car {
    private final StringProperty type = new SimpleStringProperty();

    public StringProperty typeProperty() { return type; }
    public String getType() { return type.get(); }
    public void setType(String newType) { type.set(newType); }
}

class Person {
    private final ObjectProperty<Car> car = new SimpleObjectProperty<>();

    private final StringProperty name = new SimpleStringProperty();

    public ObjectProperty<Car> carProperty() { return car; }
    public Car getCar() { return car.get(); }
    public void setCar(Car newCar) { car.set(newCar); }

    public StringProperty nameProperty() { return name; }
    public String getName() { return name.get(); }
    public void setName(String newName) { name.set(newName); }
}

答案 1 :(得分:1)

首先,您应该将ObjectProperty<Car>添加到Person课程中以制作汽车Observable。然后您的绑定将如下所示:

carTypeName.setCellValueFactory(data ->
                Bindings.createStringBinding(
                        () -> data.getValue().getCar().get().getType().get(),
                                data.getValue().getCar(),
                                data.getValue().getCar().get().getType()));

这意味着,每次更改Car.typePerson.car时,您的手机信号都会显示新的Person.car.type值。 (如果Person.car可以是null,则可能需要添加其他null项检查。)