JavaFX 2 TableView如何在更改对象时更新单元格

时间:2014-04-04 14:45:59

标签: javafx-2

我正在创建一个TableView来显示有关自定义对象列表(EntityEvents)的信息。

表格视图必须包含2列。 第一列显示相应的EntityEvent名称。 第二列将显示一个按钮。按钮文本取决于EntityEvent的属性。如果属性为零,则为"创建"否则为"编辑"。

我设法做得很好,除了在相应的EntityEvent对象发生更改时我无法找到更新TableView行的方法。

非常重要:我无法更改EntityEvent类以使用JavaFX属性,因为它们不受我的控制。此类使用PropertyChangeSupport在监视属性发生更改时通知侦听器。

注意: 我意识到向List添加新元素可能会导致TableView重新绘制自己,但这不是我需要的。我说PROBABLY是因为我已经阅读了一些影响这种行为的错误。 我尝试使用这种方法来强制重绘,因为我无法使其发挥作用。

有谁知道怎么做?

非常感谢。

这是一个简化的代码示例,说明了该方案:

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

import javafx.application.Application;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.TableView;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Callback;

public class Main extends Application {

    //=============================================================================================
    public class EntityEvent {
        private String m_Name;
        private PropertyChangeSupport m_NamePCS = new PropertyChangeSupport(this);
        private int m_ActionCounter;
        private PropertyChangeSupport m_ActionCounterPCS = new PropertyChangeSupport(this);

        public EntityEvent(String name, int actionCounter) {
            m_Name = name;
            m_ActionCounter = actionCounter;
        }

        public String getName() {
            return m_Name;
        }

        public void setName(String name) {
            String lastName = m_Name;
            m_Name = name;
            System.out.println("Name changed: " + lastName + " -> " + m_Name);
            m_NamePCS.firePropertyChange("Name", lastName, m_Name);
        }

        public void addNameChangeListener(PropertyChangeListener listener) {
            m_NamePCS.addPropertyChangeListener(listener);
        }   

        public int getActionCounter() {
            return m_ActionCounter;
        }

        public void setActionCounter(int actionCounter) {
            int lastActionCounter = m_ActionCounter;
            m_ActionCounter = actionCounter;
            System.out.println(m_Name + ": ActionCounter changed: " + lastActionCounter + " -> " + m_ActionCounter);
            m_ActionCounterPCS.firePropertyChange("ActionCounter", lastActionCounter, m_ActionCounter);
        }

        public void addActionCounterChangeListener(PropertyChangeListener listener) {
            m_ActionCounterPCS.addPropertyChangeListener(listener);
        }   
    }

    //=============================================================================================
    private class AddPersonCell extends TableCell<EntityEvent, String> {
        Button m_Button = new Button("Undefined");
        StackPane m_Padded = new StackPane();

        AddPersonCell(final TableView<EntityEvent> table) {
            m_Padded.setPadding(new Insets(3));
            m_Padded.getChildren().add(m_Button);

            m_Button.setOnAction(new EventHandler<ActionEvent>() {
                @Override public void handle(ActionEvent actionEvent) {
                    // Do something
                }
            });
        }

        @Override protected void updateItem(String item, boolean empty) {
            super.updateItem(item, empty);
            if (!empty) {
                setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
                setGraphic(m_Padded);
                m_Button.setText(item);
            }
        }
    }

    //=============================================================================================
    private ObservableList<EntityEvent> m_EventList;

    //=============================================================================================
    @SuppressWarnings("unchecked")
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Table View test.");

        VBox container = new VBox();

        m_EventList = FXCollections.observableArrayList(
                new EntityEvent("Event 1", -1),
                new EntityEvent("Event 2", 0),
                new EntityEvent("Event 3", 1)
                );
        final TableView<EntityEvent> table = new TableView<EntityEvent>();
        table.setItems(m_EventList);            

        TableColumn<EntityEvent, String> eventsColumn = new TableColumn<>("Events");
        TableColumn<EntityEvent, String> actionCol = new TableColumn<>("Actions");
        actionCol.setSortable(false);

        eventsColumn.setCellValueFactory(new Callback<CellDataFeatures<EntityEvent, String>, ObservableValue<String>>() {
            public ObservableValue<String> call(CellDataFeatures<EntityEvent, String> p) {
                EntityEvent event = p.getValue();
                event.addActionCounterChangeListener(new PropertyChangeListener() {
                    @Override
                    public void propertyChange(PropertyChangeEvent event) {
                        // TODO: I'd like to update the table cell information.
                    }
                });
                return new ReadOnlyStringWrapper(event.getName());
            }
        });

        actionCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<EntityEvent, String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<EntityEvent, String> ev) {
                String text = "NONE";
                if(ev.getValue() != null) {
                    text = (ev.getValue().getActionCounter() != 0) ? "Edit" : "Create";
                }
                return new ReadOnlyStringWrapper(text);
            }
        });

        // create a cell value factory with an add button for each row in the table.
        actionCol.setCellFactory(new Callback<TableColumn<EntityEvent, String>, TableCell<EntityEvent, String>>() {
            @Override
            public TableCell<EntityEvent, String> call(TableColumn<EntityEvent, String> personBooleanTableColumn) {
                return new AddPersonCell(table);
            }
        });

        table.getColumns().setAll(eventsColumn, actionCol);
        table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);

        // Add Resources Button
        Button btnInc = new Button("+");
        btnInc.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent ev) {
                System.out.println("+ clicked.");
                EntityEvent entityEvent = table.getSelectionModel().getSelectedItem();
                if (entityEvent == null) {
                    System.out.println("No Event selected.");
                    return;
                }
                entityEvent.setActionCounter(entityEvent.getActionCounter() + 1);
                // TODO: I expected the TableView to be updated since I modified the object.
            }
        });

        // Add Resources Button
        Button btnDec = new Button("-");
        btnDec.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent ev) {
                System.out.println("- clicked.");
                EntityEvent entityEvent = table.getSelectionModel().getSelectedItem();
                if (entityEvent == null) {
                    System.out.println("No Event selected.");
                    return;
                }
                entityEvent.setActionCounter(entityEvent.getActionCounter() - 1);
                // TODO: I expected the TableView to be updated since I modified the object.
            }
        });

        container.getChildren().add(table);
        container.getChildren().add(btnInc);
        container.getChildren().add(btnDec);

        Scene scene = new Scene(container, 300, 600, Color.WHITE);        

        primaryStage.setScene(scene);

        primaryStage.show();
    }

    //=============================================================================================
    public Main() {
    }

    //=============================================================================================
    public static void main(String[] args) {
        launch(Main.class, args);
    }

}

1 个答案:

答案 0 :(得分:4)

尝试javafx.beans.property.adapter类,特别是JavaBeanStringProperty和JavaBeanIntegerProperty。我还没有使用过这些,但我认为你可以做类似

的事情
TableColumn<EntityEvent, Integer> actionCol = new TableColumn<>("Actions");
actionCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<EntityEvent, Integer> ev) {
    return new JavaBeanIntegerPropertyBuilder().bean(ev.getValue()).name("actionCounter").build();
});

// ...

public class AddPersonCell extends TableCell<EntityEvent, Integer>() {
    final Button button = new Button();

    public AddPersonCell() {
        setPadding(new Insets(3));
        setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
        button.setOnAction(...);
    }

    @Override
    public void updateItem(Integer actionCounter, boolean empty) {
        if (empty) {
            setGraphic(null);
        } else {
            if (actionCounter.intValue()==0) {
                button.setText("Create");
            } else {
                button.setText("Add");
            }
            setGraphic(button);
        }
    }
}

正如我所说,我还没有使用Java bean属性适配器类,但他们的想法是他们&#34;翻译&#34;属性更改事件到JavaFX更改事件。我只是在这里输入了这个而没有经过测试,但至少应该给你一些东西。

更新:经过一些小试验后,如果您的EntityEvent确实按照您在代码示例中的显示方式进行设置,我认为这种方法无效。标准Java bean绑定属性模式(JavaFX属性适配器所依赖的属性模式)具有单个属性更改侦听器和addPropertyChangeListener(...)方法。 (侦听器可以查询事件以查看更改了哪个属性。)

我想如果你这样做

public class EntityEvent {
    private String m_Name;
    private PropertyChangeSupport pcs = new PropertyChangeSupport(this);
    private int m_ActionCounter;

    public EntityEvent(String name, int actionCounter) {
        m_Name = name;
        m_ActionCounter = actionCounter;
    }

    public String getName() {
        return m_Name;
    }

    public void setName(String name) {
        String lastName = m_Name;
        m_Name = name;
        System.out.println("Name changed: " + lastName + " -> " + m_Name);
        pcs.firePropertyChange("name", lastName, m_Name);
    }

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

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

    public int getActionCounter() {
        return m_ActionCounter;
    }

    public void setActionCounter(int actionCounter) {
        int lastActionCounter = m_ActionCounter;
        m_ActionCounter = actionCounter;
        System.out.println(m_Name + ": ActionCounter changed: " + lastActionCounter + " -> " + m_ActionCounter);
        pcs.firePropertyChange("ActionCounter", lastActionCounter, m_ActionCounter);
    }

}

它将适用于上面的适配器类。显然,如果您现有的代码调用addActionChangeListeneraddNameChangeListener方法,您可能希望保留这些现有方法和现有属性更改侦听器,但我认为没有理由不能同时使用这两种方法。