How to add a calendar in Frame or Panel in Java

Asked

Viewed 385 times

1

I’m developing a Java project that deals with dates. The main idea is to put a medium-sized calendar on the main screen, dates that already have an event scheduled should have a color (blue, orange, etc.) and when hovering over the menu you should show something like a pop-up and show a brief description of the event.

The project is being developed using Javafx combined with elements of Swing. My research only gave me the Jcalendar that does not meet my needs.

Any hint is welcome.

  • About date/calendar related components: Javafx has the DatePicker, do not need to use any third party. You can use the methods to color days with scheduled events and the "click" event to find the scheduled event on a given day.

  • I will try to use Datepicker, but could you point me some links with good examples, or could you elaborate some?

1 answer

0


Basically you will have to implement a dayCellFactory, this will allow you to manipulate how calendar cells will be rendered. Example of commented documentation:

DatePicker dp = new DatePicker();

dp.setDayCellFactory(new Callback<DatePicker, DateCell>() {

    @Override
    public DateCell call(DatePicker arg0) {
        return new DateCell() {
            @Override
            public void updateItem(LocalDate item, boolean empty) {
                // Chamada obrigatória ao renderizador da superclasse
                super.updateItem(item, empty);

                // Aqui estamos trocando a cor do background de um dia específico
                if(MonthDay.from(item).equals(MonthDay.of(9, 15))) {
                    setStyle("-fx-background-color: #ff4444;");
                }
                // Aqui estamos desabilitando a data do dia seguinte
                if(item.equals(LocalDate.now().plusDays(1))) {
                    setDisable(true);
                }
            }
        };
    }
});

It stays like this after the execution:

inserir a descrição da imagem aqui

Using this logic you can implement a class called Event, with a Localdate and a description as attributes, and traverse an array of events within the Factory by painting them accordingly.

Already the question of Popup you can use the method setTooltip that this component inherits from the Control class (The tooltip is a bit buggy). Or you can use the Popover Controlsfx, a well-known library of custom components for Javafx.

  • Thank you Gustavo. I will read the documentation of the elements you mentioned and in case of problems I will leave my comment. And I still won’t mark your answer as the best answer for more people to answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.