【问题标题】:Datepicker only mark certain days with the color redDatepicker 只用红色标记某些日子
【发布时间】:2018-05-27 12:20:33
【问题描述】:

我只想用红色标记某些日子。 如果我使用 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>

【问题讨论】:

    标签: java javafx datepicker fxml


    【解决方案1】:

    可以使用cell factory 来设置DateCells 范围的样式。注意 cmets:

    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

    【讨论】:

      猜你喜欢
      • 2011-03-03
      • 1970-01-01
      • 2011-05-27
      • 1970-01-01
      • 1970-01-01
      • 2013-06-15
      • 1970-01-01
      相关资源
      最近更新 更多