开发者

Google Calendar like Swing application [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.

Closed 9 years ago.

Improve this question

Context

I am working on a java based desktop application that makes class-room reservation for a high-school. Each reservation is kept in an object of class RoomBooking whose content is as follows (simplified):

public class RoomBooking implements java.io.Serializable {

    private int id;
    private String roomName, userName;
    private Date bookDate;
    private int beginTime;  
    // represents the reservation begin time (i.e. 8 means 08.00) 
    private int endTime; 
    // represents the reservation end time (i.e. 9 means 09.00) 
    private int  roomId;

//Constructor and setters-getters follow the above code
.....
}

I retrieve the reservataions from the database and put them into an ArrayList object which keeps an instance list of class RoomBooking (i.e. ArrayList). The order of the objects in the list is based on the reservation date in increasing order.

Problem Definition

Now I try to create a swing based UI (i.e. JFrame) which shows a 5 by 7 grid view using JTable (It should look like a tabular calendar just like google calendar). The columns of the Jtable should be like Monday, Tuesday....Sunday and the cells in the Jtable will keep the days of a specific month from 1 to 28, 30 or 31 depending on the month (e.g. 28 for February). The purpose here is to color a cell for which a reservation is present and place some informative text in it (e.g. reservation time interval like 08.00 - 09.00)

Considering that the data in the previously mentioned ArrayList is linear and ordered based on the reservation date, I need map each reservation to a specific cell in the so mentioned JTable.

For this purpose, I tried to write a method that receives the date for a reservation and returns the corresponding row and column number of the related cell in the Jtable. This introduces several side problems. For example, if the first day of the month does not start on Monday, the above mentioned method should be able to shift row-column indexes it will return accordingly.

Question

i) Do you know if there is a freely available java bean component that will provide a tabular calendar view whose columns are the days of the weeks and the cells are days of the month (cells should be able to take additional data in text like 08.00-09.00)?

I found some commercial solutions in my google search but I am interested in open-source solutions/proposals. I am under kinda pressure and a ready-开发者_StackOverflowto-use component would be highly appreciated.

ii) Has anoyone faced an issue similar to the one described in problem definition? If so, could you please provide me some guidelines on writing the so mentioned mapper method.

Any suggestion/help would be highy apreciated.

Thanks in advance.

Best Regards, Farda


not an answer to your question, but you have to building three view

1/ you can put a grid for CalendarView to the JTable (best way for quarter/half/year CalendarView),

but part of them are based on JButton/JTextField/JLabels placed into JPanel with GridLaout/GridBagLayout(7x7),

easiest way is if you placed here JButtons, for nice output disable days for prevoius/next Month

2/ for WeekView shedules are used JTable with one TableColumn, there put JPanel that contains JLabel, because is possible that some shedules are penetrated (or for other JComponents#setOpaque(false)),

or by using GridLayout(1, 7, 5, 5); with 7 x JPanel for DayViewShedule(s)

3/ for DayView with GridLayout(1, 24, 5, 5); with 24 x JPanel for HourShedule(s)


Check out JCalendar by Kai Toedter http://www.toedter.com/en/jcalendar/


I came up with a mapper method that I mentioned in the problem description section. Below you can find the content of this method. There might be better ways to do this but this perfectly meets my need. I utilized two things to calculate the corresponding row and column indexes in the JTable as follows:

int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
int weekOfMonth = cal.get(Calendar.WEEK_OF_MONTH);


 private int[] getCalendarIndexNumbers(
            int dayOfTheMonth,
            int year,
            int month) {

        int[] retArray = new int[2];
        Calendar cal = Calendar.getInstance();
        cal.set(year, month - 1, dayOfTheMonth);
        int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
        int weekOfMonth = cal.get(Calendar.WEEK_OF_MONTH);

        int rowIndex = weekOfMonth-1;
        if (rowIndex < 0) {
            rowIndex = 0;
        }

        int columnIndex;
        switch (dayOfWeek) {
            case 1:
                columnIndex = 6;
                break;
            case 2:
                columnIndex = 0;
                break;
            case 3:
                columnIndex = 1;
                break;
            case 4:
                columnIndex = 2;
                break;
            case 5:
                columnIndex = 3;
                break;
            case 6:
                columnIndex = 4;
                break;
            case 7:
                columnIndex = 5;
                break;
            default:
                columnIndex = 6;
                break;
        }

        retArray[0] = rowIndex;
        retArray[1] = columnIndex;

        return retArray;

    }

In order to fill the contents of the cells in the JTable, I had to write a custom renderer that implements TableCellRenderer and extends JTextArea. An example for this is available at: http://archive.devx.com/java/free/articles/kabutz07/Kabutz07-2.asp

Hope this helps those who needs to develop a similar application.

Thanks for the previous responses.

Best Regards

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜