了JavaFx:节目的DatePicker(JavaFx: show DatePicker)

2019-10-29 05:31发布

我有一个ComboBox<MyItem>我想表现出DatePicker当我选择从一个特殊的项目ComboBox 。 我创建了一个扩展组合框类,和我有该类一个datepicker。 我添加了一个监听器,它的selectedItemProperty

public class CustomComboBox extends ComboBox<MyItem>{

    private DatePicker datePicker;

    public CustomComboBox(){
        getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
                if (MyItem.DATE.equals(newValue)) {
                    initDatePicker();
                    datePicker.show();
                    datePicker.requestFocus();
                }
        });
    }

    private void initDatePicker() {
        if (datePicker == null) {
            datePicker = new DatePicker();
            datePicker.setFocusTraversable(false);
        }
    }
}

所以,如果我选择DATE项目的DatePicker应该弹出,如果我选择我要添加的值的日期ComboBox ,首先为什么不日期选择器弹出? 第二个问题是这更多钞票到所选择的日期添加到组合框为值。

Answer 1:

我想你需要的东西是这样的:

我用从弹出类做了ControlsFX库 。

这个演示程序播放理解中心思想。

import org.controlsfx.control.PopOver;
// here all other needed dependencies

public class Main extends Application {
    private static final String DATE_TYPE = "DATE";

    private class ComboBoxNode {
        private Object value;
        private String type;

        private ComboBoxNode(final Object value, final String type) {
            this.value = value;
            this.type = type;
        }

        @Override
        public String toString() {
            return Objects.toString(value);
        }
    }

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

    @Override
    public void start(Stage primaryStage) {
        final ObservableList<ComboBoxNode> items =
                FXCollections.observableArrayList(
                        new ComboBoxNode(LocalDate.now(), DATE_TYPE),
                        new ComboBoxNode("11:35AM", "TIME"));

        final PopOver datePopOver = new PopOver();
        datePopOver.setTitle("Enter new date");
        datePopOver.setCornerRadius(10);
        datePopOver.setHeaderAlwaysVisible(true);
        datePopOver.setCloseButtonEnabled(true);
        datePopOver.setAutoHide(true);

        final ComboBox<ComboBoxNode> customComboBox = new ComboBox<>(items);
        customComboBox.getSelectionModel().selectedItemProperty().addListener((o, old, newNode) -> {
            if (newNode != null) {
                if (newNode.type.equals(DATE_TYPE)) {
                    final DatePicker datePicker = new DatePicker((LocalDate) newNode.value);
                    datePicker.valueProperty().addListener((obs, oldDate, newDate) -> {
                        items.set(customComboBox.getSelectionModel().getSelectedInde‌​x(), new ComboBoxNode(newDate, DATE_TYPE));
                        datePopOver.hide();
                    });

                    final StackPane stackPane = new StackPane(datePicker);
                    stackPane.setPadding(new Insets(10, 10, 10, 10));

                    datePopOver.setContentNode(stackPane);
                    datePopOver.show(customComboBox);
                } else {
                    datePopOver.hide();
                }
            }
        });

        final FlowPane pane = new FlowPane(customComboBox);
        pane.setPadding(new Insets(10, 10, 10, 10));
        pane.setPrefWidth(400);
        pane.setPrefHeight(300);

        // Show Scene
        final Scene scene = new Scene(pane);
        primaryStage.setTitle("Popup calendar");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}


文章来源: JavaFx: show DatePicker