JavaFx:显示DatePicker [英] JavaFx: show DatePicker

查看:452
本文介绍了JavaFx:显示DatePicker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ComboBox< MyItem> ,当我从中选择一个特殊项目时,我想显示一个 DatePicker ComboBox 。我创建了一个扩展ComboBox的类,我在该类中有一个DatePicker。我已经为其 selectedItemProperty 添加了一个监听器:

I have a ComboBox<MyItem> and I want to show a DatePicker when I select a special item from the ComboBox. I created a class that extends ComboBox, and I have a DatePicker in that class. I have added a listener to its 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的值
首先为什么datePicker没有弹出?第二个问题是可以将所选日期添加到comboBox作为值。

So if I select the DATE item the DatePicker should pop up and If I select a date I want to add as the value of the ComboBox First of all why the datePicker not pops up? The second question is this posible to add the selected date to comboBox as value.

推荐答案

我假设你需要这样的东西:

I assume you need something like this:

我是通过使用 ControlsFX库

使用此演示应用程序来理解主要想法。

Play with this demo app to understand the main idea.

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:显示DatePicker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆