Skip to content

Instantly share code, notes, and snippets.

@Da9el00
Last active September 28, 2024 19:58
Show Gist options
  • Save Da9el00/f4340927b8ba6941eb7562a3306e93b6 to your computer and use it in GitHub Desktop.
Save Da9el00/f4340927b8ba6941eb7562a3306e93b6 to your computer and use it in GitHub Desktop.
JavaFX Calendar View
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.FlowPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane prefHeight="642.0" prefWidth="744.0" style="-fx-background-color: #f2fafc;" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.calendarview.CalendarController">
<FlowPane fx:id="calendar" hgap="10.0" layoutX="14.0" layoutY="116.0" prefHeight="498.0" prefWidth="716.0" vgap="5.0" />
<HBox alignment="CENTER" layoutX="163.0" layoutY="14.0" prefHeight="44.0" prefWidth="419.0" spacing="5.0">
<children>
<Button mnemonicParsing="false" onAction="#backOneMonth" text="&lt;" />
<Text fx:id="year" strokeType="OUTSIDE" strokeWidth="0.0" text="####">
<font>
<Font size="22.0" />
</font>
</Text>
<Text fx:id="month" strokeType="OUTSIDE" strokeWidth="0.0" text="####">
<font>
<Font size="22.0" />
</font>
</Text>
<Button mnemonicParsing="false" onAction="#forwardOneMonth" text="&gt;" />
</children>
</HBox>
<HBox alignment="CENTER" layoutX="14.0" layoutY="78.0" prefHeight="44.0" prefWidth="716.0" spacing="88.75">
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Su" textAlignment="CENTER" />
<Text layoutX="213.0" layoutY="37.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Mo" textAlignment="CENTER" />
<Text layoutX="222.0" layoutY="37.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Tu" textAlignment="CENTER" />
<Text layoutX="232.0" layoutY="37.0" strokeType="OUTSIDE" strokeWidth="0.0" text="We" textAlignment="CENTER" />
<Text layoutX="241.0" layoutY="37.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Th" textAlignment="CENTER" />
<Text layoutX="251.0" layoutY="37.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Fr" textAlignment="CENTER" />
<Text layoutX="266.0" layoutY="37.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Sa" textAlignment="CENTER" />
</children>
<padding>
<Insets right="9.0" />
</padding>
</HBox>
</AnchorPane>
import java.time.ZonedDateTime;
public class CalendarActivity {
private ZonedDateTime date;
private String clientName;
private Integer serviceNo;
public CalendarActivity(ZonedDateTime date, String clientName, Integer serviceNo) {
this.date = date;
this.clientName = clientName;
this.serviceNo = serviceNo;
}
public ZonedDateTime getDate() {
return date;
}
public void setDate(ZonedDateTime date) {
this.date = date;
}
public String getClientName() {
return clientName;
}
public void setClientName(String clientName) {
this.clientName = clientName;
}
public Integer getServiceNo() {
return serviceNo;
}
public void setServiceNo(Integer serviceNo) {
this.serviceNo = serviceNo;
}
@Override
public String toString() {
return "CalenderActivity{" +
"date=" + date +
", clientName='" + clientName + '\'' +
", serviceNo=" + serviceNo +
'}';
}
}
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
import java.time.ZonedDateTime;
import java.util.*;
public class CalendarController implements Initializable {
ZonedDateTime dateFocus;
ZonedDateTime today;
@FXML
private Text year;
@FXML
private Text month;
@FXML
private FlowPane calendar;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
dateFocus = ZonedDateTime.now();
today = ZonedDateTime.now();
drawCalendar();
}
@FXML
void backOneMonth(ActionEvent event) {
dateFocus = dateFocus.minusMonths(1);
calendar.getChildren().clear();
drawCalendar();
}
@FXML
void forwardOneMonth(ActionEvent event) {
dateFocus = dateFocus.plusMonths(1);
calendar.getChildren().clear();
drawCalendar();
}
private void drawCalendar(){
year.setText(String.valueOf(dateFocus.getYear()));
month.setText(String.valueOf(dateFocus.getMonth()));
double calendarWidth = calendar.getPrefWidth();
double calendarHeight = calendar.getPrefHeight();
double strokeWidth = 1;
double spacingH = calendar.getHgap();
double spacingV = calendar.getVgap();
//List of activities for a given month
Map<Integer, List<CalendarActivity>> calendarActivityMap = getCalendarActivitiesMonth(dateFocus);
int monthMaxDate = dateFocus.getMonth().maxLength();
//Check for leap year
if(dateFocus.getYear() % 4 != 0 && monthMaxDate == 29){
monthMaxDate = 28;
}
int dateOffset = ZonedDateTime.of(dateFocus.getYear(), dateFocus.getMonthValue(), 1,0,0,0,0,dateFocus.getZone()).getDayOfWeek().getValue();
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
StackPane stackPane = new StackPane();
Rectangle rectangle = new Rectangle();
rectangle.setFill(Color.TRANSPARENT);
rectangle.setStroke(Color.BLACK);
rectangle.setStrokeWidth(strokeWidth);
double rectangleWidth =(calendarWidth/7) - strokeWidth - spacingH;
rectangle.setWidth(rectangleWidth);
double rectangleHeight = (calendarHeight/6) - strokeWidth - spacingV;
rectangle.setHeight(rectangleHeight);
stackPane.getChildren().add(rectangle);
int calculatedDate = (j+1)+(7*i);
if(calculatedDate > dateOffset){
int currentDate = calculatedDate - dateOffset;
if(currentDate <= monthMaxDate){
Text date = new Text(String.valueOf(currentDate));
double textTranslationY = - (rectangleHeight / 2) * 0.75;
date.setTranslateY(textTranslationY);
stackPane.getChildren().add(date);
List<CalendarActivity> calendarActivities = calendarActivityMap.get(currentDate);
if(calendarActivities != null){
createCalendarActivity(calendarActivities, rectangleHeight, rectangleWidth, stackPane);
}
}
if(today.getYear() == dateFocus.getYear() && today.getMonth() == dateFocus.getMonth() && today.getDayOfMonth() == currentDate){
rectangle.setStroke(Color.BLUE);
}
}
calendar.getChildren().add(stackPane);
}
}
}
private void createCalendarActivity(List<CalendarActivity> calendarActivities, double rectangleHeight, double rectangleWidth, StackPane stackPane) {
VBox calendarActivityBox = new VBox();
for (int k = 0; k < calendarActivities.size(); k++) {
if(k >= 2) {
Text moreActivities = new Text("...");
calendarActivityBox.getChildren().add(moreActivities);
moreActivities.setOnMouseClicked(mouseEvent -> {
//On ... click print all activities for given date
System.out.println(calendarActivities);
});
break;
}
Text text = new Text(calendarActivities.get(k).getClientName() + ", " + calendarActivities.get(k).getDate().toLocalTime());
calendarActivityBox.getChildren().add(text);
text.setOnMouseClicked(mouseEvent -> {
//On Text clicked
System.out.println(text.getText());
});
}
calendarActivityBox.setTranslateY((rectangleHeight / 2) * 0.20);
calendarActivityBox.setMaxWidth(rectangleWidth * 0.8);
calendarActivityBox.setMaxHeight(rectangleHeight * 0.65);
calendarActivityBox.setStyle("-fx-background-color:GRAY");
stackPane.getChildren().add(calendarActivityBox);
}
private Map<Integer, List<CalendarActivity>> createCalendarMap(List<CalendarActivity> calendarActivities) {
Map<Integer, List<CalendarActivity>> calendarActivityMap = new HashMap<>();
for (CalendarActivity activity: calendarActivities) {
int activityDate = activity.getDate().getDayOfMonth();
if(!calendarActivityMap.containsKey(activityDate)){
calendarActivityMap.put(activityDate, List.of(activity));
} else {
List<CalendarActivity> OldListByDate = calendarActivityMap.get(activityDate);
List<CalendarActivity> newList = new ArrayList<>(OldListByDate);
newList.add(activity);
calendarActivityMap.put(activityDate, newList);
}
}
return calendarActivityMap;
}
private Map<Integer, List<CalendarActivity>> getCalendarActivitiesMonth(ZonedDateTime dateFocus) {
List<CalendarActivity> calendarActivities = new ArrayList<>();
int year = dateFocus.getYear();
int month = dateFocus.getMonth().getValue();
Random random = new Random();
for (int i = 0; i < 50; i++) {
ZonedDateTime time = ZonedDateTime.of(year, month, random.nextInt(27)+1, 16,0,0,0,dateFocus.getZone());
calendarActivities.add(new CalendarActivity(time, "Hans", 111111));
}
return createCalendarMap(calendarActivities);
}
}
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class Main extends Application {
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("calendar.fxml"));
Scene scene = new Scene(fxmlLoader.load());
stage.setTitle("Hello!");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
@NiekBoii
Copy link

NiekBoii commented Apr 1, 2023

Ayo thanks ima use for skool project

@schlaggo
Copy link

Thanks Man! By the way.... I really like motorboats

@zeyr1
Copy link

zeyr1 commented Jun 15, 2023

Man I coudn't make it work somebody told me that main method is missing but I can't figure it out what did I do wrong. Or missing

@iskander-rassulov
Copy link

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment