JavaFX 8 datepicker function
•
Java
I just started using the new JavaFX 8 control datepicker In datepicker user experience documentation, it claims that it has some cool features I want to use in GUI applications:
>I want to change the format from mm / DD / yyyy to DD / mm / yyyy. > I want to limit the dates I can choose Users can only choose from today until the same day next year. > Display Hijri dates other than the original date:
How to realize these functions? Javadoc doesn't say much about them
Solution
This is a complete implementation:
import java.net.URL;
import java.time.LocalDate;
import java.time.chrono.HijrahChronology;
import java.time.format.DateTimeFormatter;
import java.util.ResourceBundle;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.DateCell;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.util.Callback;
import javafx.util.StringConverter;
/**
*
* @author Fouad
*/
public class FXMLDocumentController implements Initializable
{
@FXML
private DatePicker dpDate;
@Override
public void initialize(URL url,ResourceBundle rb)
{
dpDate.setValue(LocalDate.Now());
dpDate.setChronology(HijrahChronology.INSTANCE);
Callback<DatePicker,DateCell> dayCellFactory = dp -> new DateCell()
{
@Override
public void updateItem(LocalDate item,boolean empty)
{
super.updateItem(item,empty);
if(item.isBefore(LocalDate.Now()) || item.isAfter(LocalDate.Now().plusYears(1)))
{
setStyle("-fx-background-color: #ffc0cb;");
Platform.runLater(() -> setDisable(true));
/* When Hijri Dates are shown,setDisable() doesn't work. Here is a workaround */
//addEventFilter(MouseEvent.MOUSE_CLICKED,e -> e.consume());
}
}
};
StringConverter<LocalDate> converter = new StringConverter<LocalDate>()
{
final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
@Override
public String toString(LocalDate date)
{
if(date != null) return dateFormatter.format(date);
else return "";
}
@Override
public LocalDate fromString(String string)
{
if(string != null && !string.isEmpty())
{
LocalDate date = LocalDate.parse(string,dateFormatter);
if(date.isBefore(LocalDate.Now()) || date.isAfter(LocalDate.Now().plusYears(1)))
{
return dpDate.getValue();
}
else return date;
}
else return null;
}
};
dpDate.setDayCellFactory(dayCellFactory);
dpDate.setConverter(converter);
dpDate.setPromptText("dd/MM/yyyy");
}
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
