根据按钮单击初始化表列

时间:2018-06-07 08:15:39

标签: java javafx

我在JavaFX中相当新。我在桌子外面有一个包含多列和两个按钮(btnBuildingbtnBSearch)的表格。在表格中,我有一个列colAction,我希望根据在表格外点击的按钮来设置一些按钮。假设我点击btnBuilding我希望在Save列中有2个按钮RefreshcolAction,并且每当我点击btnBSearch我想要2个按钮{我Edit列中的{1}}和Add。在colAction里面我尝试过如下

initialize()

但问题是我点击的任何按钮我总是在我的操作列中获得colAction.setCellFactory(col -> { Button SaveButton = new Button("Save"); Button AddButton = new Button("Add"); Button RefreshButton = new Button("Refresh"); Button EditButton = new Button("Edit"); HBox hbox = new HBox(5); if(btnBSearch.isFocused()) hbox.getChildren().addAll(AddButton,EditButton); else if(btnBuilding.isFocused()) hbox.getChildren().addAll(SaveButton,RefreshButton); TableCell<ModelBrBuilding, ModelBrBuilding> cell = new TableCell<ModelBrBuilding, ModelBrBuilding>() { @Override //Updating with the number of row public void updateItem(ModelBrBuilding building, boolean empty) { super.updateItem(building, empty); if (empty) { setGraphic(null); } else { setGraphic(hbox); } } }; EditButton.setOnAction((ActionEvent event)->{ }); RefreshButton.setOnAction(event->{ }); SaveButton.setOnAction((ActionEvent event) -> { }); AddButton.setOnAction(event -> { }); return cell ; }); Add。如何根据按钮(位于表格外部)在我的列中添加不同的按钮?我单击?

2 个答案:

答案 0 :(得分:1)

cellFactory每个单元格只运行一次。您需要确保单元格已更新,单击表格外的按钮。

您可以通过创建包含graphic工厂的属性来监听它。

public interface GraphicFactory<T> {

    Node createGraphic();

    void updateGraphic(Node graphic, T item);
}
public class ReplacableGraphicTableCell<S, T> extends TableCell<S, T> {

    private final ChangeListener<GraphicFactory<T>> factoryListener = (o, oldValue, newValue) -> {
        if (newValue == null || isEmpty()) {
            setGraphic(null);
        } else {
            Node n = newValue.createGraphic();
            newValue.updateGraphic(n, getItem());
            setGraphic(n);
        }
    };

    private final ObservableValue<GraphicFactory<T>> factory;

    private ReplacableGraphicTableCell(ObservableValue<GraphicFactory<T>> factory) {
        this.factory = factory;
        factory.addListener(factoryListener);
    }

    public static <E, F> Callback<TableColumn<E, F>, TableCell<E, F>> forTableColumn(ObservableValue<GraphicFactory<F>> factory) {
        if (factory == null) {
            throw new IllegalArgumentException();
        }
        return column -> new ReplacableGraphicTableCell(factory);
    }

    @Override
    protected void updateItem(T item, boolean empty) {
        super.updateItem(item, empty);
        if (empty) {
            setGraphic(null);
        } else {
            GraphicFactory<T> fact = factory.getValue();
            if (fact == null) {
                setGraphic(null);
            } else {
                Node graphic = getGraphic();
                if (graphic == null) {
                    graphic = fact.createGraphic();
                    setGraphic(graphic);
                }

                fact.updateGraphic(graphic, item);
            }
        }
    }

}
final ObjectProperty<GraphicFactory<Item>> graphicFactory = new SimpleObjectProperty<>();
TableColumn<Item, Item> column = new TableColumn<>();
column.setCellValueFactory(cd -> new SimpleObjectProperty<>(cd.getValue()));
column.setCellFactory(ReplacableGraphicTableCell.forTableColumn(graphicFactory));

ToggleGroup tg = new ToggleGroup();
tg.selectedToggleProperty().addListener((o, oldValue, newValue) -> {
    GraphicFactory<Item> factory = null;
    if (newValue != null) {
        factory = (GraphicFactory<Item>) newValue.getUserData();
    }
    graphicFactory.set(factory);
});
RadioButton rb = new RadioButton("Add/Edit");
rb.setUserData(new GraphicFactory<Item>() {

    @Override
    public Node createGraphic() {
        Button add = new Button("Add");
        Button edit = new Button("Edit");

        HBox hbox = new HBox(add, edit);
        add.setOnAction(evt -> {
            System.out.println("Add " + hbox.getUserData());
        });
        edit.setOnAction(evt -> {
            System.out.println("Edit " + hbox.getUserData());
        });
        return hbox;
    }

    @Override
    public void updateGraphic(Node graphic, Item item) {
        graphic.setUserData(item);
    }

});
rb.setToggleGroup(tg);

RadioButton rb2 = new RadioButton("Save/Refresh");
rb2.setUserData(new GraphicFactory<Item>() {

    @Override
    public Node createGraphic() {
        Button save = new Button("Save");
        Button refresh = new Button("Refresh");

        HBox hbox = new HBox(save, refresh);
        save.setOnAction(evt -> {
            System.out.println("Save " + hbox.getUserData());
        });
        refresh.setOnAction(evt -> {
            System.out.println("Refresh " + hbox.getUserData());
        });
        return hbox;
    }

    @Override
    public void updateGraphic(Node graphic, Item item) {
        graphic.setUserData(item);
    }

});
rb2.setToggleGroup(tg);

答案 1 :(得分:0)

它不会以这种方式工作。首先,您需要处理btnBuildingbtnBSearch按钮。按下哪个按钮必须反映在您正在使用的表格中。为此,可以创建一个功能,以反映按下两个按钮中的哪一个。

BooleanProperty showSearch = new SimpleBooleanProperty(false);
...
btnBuilding.setOnAction(e -> showSearch.setValue(false));
btnBSearch.setOnAction(e -> showSearch.setValue(true));

然后,您将colAction列链接到属性的值。

colAction.setCellValueFactory(cdf -> showSearch);

在这种情况下,您可以创建CellFactory来创建动态内容单元

colAction.setCellFactory(col -> {
    return new TableCell<String, Boolean>() {
        Button SaveButton = new Button("Save");
        Button AddButton = new Button("Add");
        Button RefreshButton = new Button("Refresh");
        Button EditButton = new Button("Edit");

        HBox hboxBuilding = new HBox(5);
        HBox hboxSearch = new HBox(5);

        {
            hboxBuilding.getChildren().addAll(AddButton,EditButton);
            hboxSearch.getChildren().addAll(SaveButton,RefreshButton);
        }

        @Override
        protected void updateItem(Boolean item, boolean empty) {
            super.updateItem(item, empty);

            if (empty) {
                setGraphic(null);
            }
            else {
                setGraphic(item ? hboxBuilding : hboxSearch);
            }
        }
    };
});