Datepicker仅标记红色的特定日期

时间:2018-05-27 12:20:34

标签: java javafx datepicker fxml

我想仅用红色标记某些日子。 如果我使用DatePicker选择了一天,然后按“已选择”按钮,则DatePicker中的选定日期应以红色突出显示。

当我按下“区域设置”,然后点击DatePicker中的某一天,然后点击“已选择”按下直到标记的日期,日期标记为红色时,我才能管理。

有人有解决方案吗?

Main.java

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 600, 400));
        primaryStage.show();
    }


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

Controller.java

package sample;


import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.DateCell;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;

import javax.security.auth.callback.Callback;
import java.net.URL;
import java.time.LocalDate;
import java.util.Date;
import java.util.ResourceBundle;

public class Controller implements Initializable {

    @FXML
    private Button button1;

    @FXML
    private Button button2;

    @FXML
    private DatePicker datePicker;

    @FXML
    private Label label;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        button2.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                label.setText(LocalDate.now().toString());
                datePicker.setValue(LocalDate.now());
            }
        });
        button1.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                label.setText(datePicker.getValue().toString());
            }
        });

        javafx.util.Callback<DatePicker, DateCell> set = new javafx.util.Callback<DatePicker, DateCell>() {
            @Override
            public DateCell call(DatePicker param) {
                return new DateCell() {
                    @Override
                    public void updateItem(LocalDate item, boolean empty) {
                        super.updateItem(item, empty);
                        if(item.isBefore(datePicker.getValue().plusDays(1))){
                            setDisable(true);
                            setStyle("-fx-background-color: #ffc0cb;");
                        }
                    }
                };


            }
        };

        datePicker.setDayCellFactory(set);

    }
}

sample.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.DatePicker?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <children>
      <Button fx:id="button1" layoutX="344.0" layoutY="68.0" mnemonicParsing="false" text="selected" />
      <Button fx:id="button2" layoutX="344.0" layoutY="110.0" mnemonicParsing="false" text="local" />
      <DatePicker fx:id="datePicker" layoutX="45.0" layoutY="56.0" />
      <Label fx:id="label" layoutX="461.0" layoutY="60.0" text="Label" />
   </children>
</AnchorPane>

1 个答案:

答案 0 :(得分:0)

使用DateCell可以实现一系列cell factory的样式设置。注意评论:

import java.time.LocalDate;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.DateCell;
import javafx.scene.control.DatePicker;
import javafx.scene.control.ToggleButton;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;

public class FxTest extends Application {

    private ToggleButton button;
    private static final int RANGE =2;

    @Override
    public void start(Stage primaryStage) throws Exception {

        DatePicker datePicker = new DatePicker();

        //construct a cell factory
        final Callback<DatePicker, DateCell> dayCellFactory = new Callback<DatePicker, DateCell>() {
            @Override
            public DateCell call(final DatePicker datePicker) {
                return new DateCell() {
                    @Override public void updateItem(LocalDate item, boolean empty) {
                        super.updateItem(item, empty);

                        //if flag is true and date is within range, set style
                        if ( button.isSelected() &&
                                item.isAfter(LocalDate.now().minusDays(RANGE))  &&
                                    item.isBefore(LocalDate.now().plusDays(RANGE))) {
                            setStyle("-fx-background-color: #ffc0cb;");
                        }
                    }
                };
            }
        };

        datePicker.setDayCellFactory(dayCellFactory); //assign cell factory to picker

        button = new ToggleButton("Select");
        button.setOnAction(e -> {
            button.setText(button.isSelected()? "Selected" : "Select" );
            //reopen date picker to see the button click effect
            datePicker.show();
        });

        VBox vBox = new VBox( button, datePicker);
        Scene scene = new Scene(vBox, 400, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

或者你可以使用css并按下按钮切换PeseudoClass