The MindFusion Forums
Scheduling and Gantt Components >> Java Swing >> Jplanner Scheduler java timetable
https://mindfusion.eu/Forum/YaBB.pl?num=1554225172

Message started by robert_v on Apr 2nd, 2019 at 5:12pm

Title: Jplanner Scheduler java timetable
Post by robert_v on Apr 2nd, 2019 at 5:12pm
Hi, I am in pretty urgent need of help to finish my project. I have set out to make a timetable planner for students where the students can add a task to a database and the timetable will take that data and display it to the user. I have managed to do all of this and have used one of the other forum posts to get it to work as a calendar, I have managed to turn it in into a  timetable but I amd having problems with:
1) displaying the tasks
2) whatever day is today is repeated twice in the timetable so there are 8 columns.
3) I also want the user to be able to go backward or forward at least one week.

Here is my code:

Code (java):
public class revisionPlannerUI extends JFrame {
    public static Calendar calendar = new Calendar();
    public static Task task = new Task();
    ///private AwtCalendar calendar;
    ArrayList<Contact> taskList;
    //Calendar calendar;
   
    /**
     * Creates new form revisionPlannerUI
     * @throws java.text.ParseException
     */
    public revisionPlannerUI() throws ParseException {
        initComponents();
        calendar.setTheme(ThemeType.Light);
        calendar.setCurrentView(CalendarView.Timetable);
        //calendar.getTimetableSettings().getVisibleColumns();
        Container cp = jPanel2;
        cp.setLayout(new BorderLayout());
        cp.add(calendar, BorderLayout.CENTER);
        for (int i = 0; i < 7; i++) {
            calendar.getTimetableSettings().getDates().add(DateTime.today().addDays(i-1));
            calendar.getTimetableSettings().setItemOffset(30);
            calendar.getTimetableSettings().setShowItemSpans(false);
            calendar.getTimetableSettings().setSnapInterval(Duration.fromMinutes(1));
            calendar.getTimetableSettings().setVisibleColumns(7);
        }
        getTasks();
       
       
    }
private void getTasks() throws ParseException{
        try{
            String SQL = "SELECT TASKNAME, TASKDATE, STARTTIME, LENGTH, SUBJECTID FROM TIMETABLE WHERE USERID = '"+globals.dbUserID+"'";
            Connection con = DriverManager.getConnection(globals.host);
            Statement statement = con.createStatement();
            ResultSet resSet = statement.executeQuery(SQL);
            while(resSet.next()){
                Date date = resSet.getDate("TASKDATE");
                Time time = resSet.getTime("STARTTIME");
                int length = resSet.getInt("LENGTH");
                DateTime dt = new DateTime(date.getYear(),date.getMonth(),date.getDay(),time.getHours(),time.getMinutes(),0);
                DateTime dt2 = new DateTime(date.getYear(),date.getMonth(),date.getDay(),time.getHours(),time.getMinutes()+length,0);
                System.out.println(date);
                Style style = new Style();
                style.setBrush(new SolidBrush(new Color(102, 255, 102)));
                DateStyle dStyle = new DateStyle();
                dStyle.setStyle(style);
                dStyle.setFrom(dt);
                dStyle.setTo(dt2);
                task.setName(resSet.getString("TASKNAME"));
                DateTime startDateTime = new DateTime(date);
                task.setStartDate(startDateTime);
                task.setActualDuration(resSet.getInt("LENGTH"));
                task.setId(Integer.toString(resSet.getInt("SUBJECTID")));
                calendar.getTasks().add(task);
                calendar.getDayStyles().add(dStyle);
               
            }
        }catch(SQLException err){
            System.out.println(err);
        }
    }


I am in very urgent need of help, your API has already led me to massive progress but I need to over come this last hurdle by tonight. Any help is much appreciated and thanks in advance.

Title: Re: Jplanner Scheduler java timetable
Post by Slavcho on Apr 2nd, 2019 at 5:57pm
1) I feel like you want to create Appointment objects instead of Task ones, and add them to the schedule.Items collection. Tasks in our API are kinds of resources displayed as separate columns when you enable grouping in time table, or as rows in resource view, and then appointments could get associated with them.

2) Today is displayed by default. Either skip it when looping over your dates, or remove the first instance.

3) Add some buttons and load new weeks from click handlers?

Regards,
Slavcho
Mindfusion

Title: Re: Jplanner Scheduler java timetable
Post by robert_v on Apr 2nd, 2019 at 11:51pm
Could I get some examples, please? I am on a very tight deadline with other features to program as well.

Very appreciated, Robert

Title: Re: Jplanner Scheduler java timetable
Post by robert_v on Apr 3rd, 2019 at 10:00am
I managed to solve issue no.2

Title: Re: Jplanner Scheduler java timetable
Post by Iva_P on Apr 3rd, 2019 at 12:55pm
Hi,
Here is code that connects to MS SQL Server database table called appointments and reads the data for events stored there. It creates MindFusion.Scheduling Appointment instances and renders a weekly calendar with them:

private void readAppointments() throws ClassNotFoundException
    {

        Connection conn = null;
        PreparedStatement pst;

        try {
            // db parameters

            String sql = "SELECT start_time, end_time, header_text, recurrence_interval, description FROM appointments";

            String userName = "mindfusion";
            String password = "mf1234";

            String url = "jdbc:sqlserver://DESKTOP-NV9S0TU\\SQLEXPRESS;databaseName=bookings;";

            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            conn = DriverManager.getConnection(url, userName, password);

            pst    = conn.prepareStatement(sql);
            ResultSet rs = pst.executeQuery();
            while (rs.next()) {
                System.out.println(rs.getDate("start_time").toString());
                Date start = rs.getDate("start_time");

                Date end = rs.getDate("end_time");
                String itemText = rs.getString("header_text");
                String description = rs.getString("header_text");
                int recurrInterval = rs.getInt("recurrence_interval");

                Appointment appointment = new Appointment();
                appointment.setStartTime(new DateTime(start));
                appointment.setEndTime(new DateTime(end));
                appointment.setHeaderText(itemText);
                appointment.setDescriptionText(description);

               calendar.getSchedule().getItems().add(appointment);



            }

        } catch(SQLException e) {
            System.out.println(e.getMessage());
        } finally {
            try{
                if(conn != null)
                    conn.close();
            }catch(SQLException ex){
                System.out.println(ex.getMessage());
            }
        }

    }

For your convenience I have taken a screenshot of the DB table with the data:


Title: Re: Jplanner Scheduler java timetable
Post by robert_v on Apr 3rd, 2019 at 1:47pm
Hi Iva_P,

Thanks for your detailed reply. I have tried to adapt my code to yours using appointments however nothing is still displayed in the time table.

Here is my code:


Code (java):
public class revisionPlannerUI extends JFrame {

    public static Calendar calendar = new Calendar();

    /**
     * Creates new form revisionPlannerUI
     *
     * @throws java.text.ParseException
     */
    public revisionPlannerUI() throws ParseException {
        initComponents();
        calendar.setTheme(ThemeType.Light);
        calendar.setCurrentView(CalendarView.Timetable);
        //calendar.getTimetableSettings().getVisibleColumns();
        Container cp = jPanel2;
        cp.setLayout(new BorderLayout());
        cp.add(calendar, BorderLayout.CENTER);
        for (int i = 0; i < 7; i++) {
            calendar.getTimetableSettings().getDates().add(DateTime.today().addDays(i - 1));
        }
        calendar.getTimetableSettings().setStartTime(8 * 60);
        calendar.getTimetableSettings().setItemOffset(30);
        calendar.getTimetableSettings().setShowItemSpans(false);
        calendar.getTimetableSettings().setSnapInterval(Duration.fromMinutes(1));
        calendar.getTimetableSettings().setVisibleColumns(7);
        getTasks();

    }
    public static ArrayList<Item> items = new ArrayList<Item>();

    private void getTasks() throws ParseException {
        try {
            String SQL = "SELECT TASKNAME, TASKDATE, STARTTIME, LENGTH, SUBJECTID FROM TIMETABLE WHERE USERID = '" + globals.dbUserID + "'";
            Connection con = DriverManager.getConnection(globals.host);
            Statement statement = con.createStatement();
            ResultSet resSet = statement.executeQuery(SQL);
            while (resSet.next()) {
                Date date = resSet.getDate("TASKDATE");
                Time time = resSet.getTime("STARTTIME");
                int length = resSet.getInt("LENGTH");
                DateTime dt = new DateTime(date.getYear(), date.getMonth(), date.getDay(), time.getHours(), time.getMinutes(), 0);
                DateTime dt2 = new DateTime(date.getYear(), date.getMonth(), date.getDay(), time.getHours(), time.getMinutes() + length, 0);
                System.out.println(date);
                String taskname = resSet.getString("TASKNAME");
                Appointment app = new Appointment();
                app.setStartTime(dt);
                app.setEndTime(dt2);
                app.setHeaderText(taskname);
                calendar.getSchedule().getItems().add(app);
            }
        } catch (SQLException err) {
            System.out.println(err);
        }
    }


If you could, could you also show me a sample for how to change between weeks when the user click son a button?

Title: Re: Jplanner Scheduler java timetable
Post by Iva_P on Apr 3rd, 2019 at 3:07pm
Could you print in the console the days that you have added to the timetable, the start and end date time of the appointments to make sure that they match. Also, are there any errors printed in the console?

For the scrolling you can use the setScrollStep method https://www.mindfusion.eu/onlinehelp/jplanner/index.htm?M_com_mindfusion_scheduling_TimetableSettings_setScrollStep_1_Integer.htm of the timetableSettings object to specify the desired step. Also check setShowNavigationButtons if you have none.

Title: Re: Jplanner Scheduler java timetable
Post by robert_v on Apr 3rd, 2019 at 3:13pm
This is what the console printed for the 3 tasks:
It is int the format of:
Date
StartDateTime
EndDateTime
2019-04-04
05/03/19 00:00:00 o'clock GMT
05/03/19 00:16:00 o'clock GMT
2019-04-03
04/03/19 10:17:00 o'clock GMT
04/03/19 10:31:00 o'clock GMT
2019-04-04
05/03/19 10:10:00 o'clock GMT
05/03/19 10:31:00 o'clock GMT

No errors are given at all :c.

I'll take a look at the scrollStep method in a bit.

Thanks,
Robert

Title: Re: Jplanner Scheduler java timetable
Post by robert_v on Apr 3rd, 2019 at 4:14pm
SetShowNavigatioButtons works just fine so thanks for that. Just the main bit now!

Thanks,
Robert

Title: Re: Jplanner Scheduler java timetable
Post by robert_v on Apr 4th, 2019 at 2:13am
My DateTimes were off by a bit but even after amending them, there is still no cell being coloured for that time period. If you can get back to me with a solution by 1pm tomorrow GMT then it is possible for me to fix my code.

Thanks,
Robert

Title: Re: Jplanner Scheduler java timetable
Post by Slavcho on Apr 4th, 2019 at 8:41am
Aren't these printed times in March? they would not show in current week... Add some println's for the dates you add to the view (getTimetableSettings().getDates().add...) - what do they show?

Title: Re: Jplanner Scheduler java timetable
Post by robert_v on Apr 4th, 2019 at 8:48am
as I said in the above I fixed this:

Code (java):
DateTime dt = new DateTime(date.getYear(), date.getMonth()+1, date.getDay()-1, time.getHours(), time.getMinutes(), 0);
                DateTime dt2 = new DateTime(date.getYear(), date.getMonth()+1, date.getDay()-1, time.getHours(), time.getMinutes() + length, 0);


doing this returns
2019-04-04
04/04/19 00:00:00 o'clock GMT
04/04/19 00:16:00 o'clock GMT
2019-04-03
03/04/19 10:17:00 o'clock GMT
03/04/19 10:31:00 o'clock GMT
2019-04-04
04/04/19 10:10:00 o'clock GMT
04/04/19 10:31:00 o'clock GMT

Title: Re: Jplanner Scheduler java timetable
Post by robert_v on Apr 4th, 2019 at 8:56am
wait this time it worked?
Adding this
Code (java):
System.out.println(calendar.getTimetableSettings().getDates().add(dt));
                System.out.println(calendar.getTimetableSettings().getDates().add(dt2));
added some tasksbut dupelicated them many times. Thats odd, is that supposed to happen?

I've attached a picture of what it looks like.

You may still be able to save me :D
timetable.PNG ( 19 KB | 220 Downloads )

Title: Re: Jplanner Scheduler java timetable
Post by Slavcho on Apr 4th, 2019 at 9:11am
This is what I'm seeing if I add your last three printed times to the first tutorial (Tutorial 1 sample project) using your timetable settings -



[code]
calendar.beginInit();
calendar.setCurrentView(CalendarView.Timetable);
calendar.getTimetableSettings().setShowDayHeader(true);
calendar.getTimetableSettings().setVisibleColumns(3);
calendar.getTimetableSettings().getDates().clear();
for (int i = 0; i < 5; i++)
     calendar.getTimetableSettings().getDates().add(DateTime.today().addDays(i));
calendar.getTimetableSettings().setStartTime(8 * 60);
     calendar.getTimetableSettings().setItemOffset(30);
     calendar.getTimetableSettings().setShowItemSpans(false);
     calendar.getTimetableSettings().setSnapInterval(Duration.fromMinutes(1));
calendar.endInit();

DateTime dt = new DateTime(2019, 4, 4, 10, 10, 0);
DateTime dt2 = new DateTime(2019, 4, 4, 10, 31, 0);
Appointment app = new Appointment();
app.setStartTime(dt);
app.setEndTime(dt2);
app.setHeaderText("taskname");
calendar.getSchedule().getItems().add(app);

dt = new DateTime(2019, 4, 3, 10, 17, 0);
dt2 = new DateTime(2019, 4, 3, 10, 31, 0);
app = new Appointment();
app.setStartTime(dt);
app.setEndTime(dt2);
app.setHeaderText("taskname");
calendar.getSchedule().getItems().add(app);

dt = new DateTime(2019, 4, 4, 00, 0, 0);
dt2 = new DateTime(2019, 4, 4, 00, 16, 0);
app = new Appointment();
app.setStartTime(dt);
app.setEndTime(dt2);
app.setHeaderText("taskname");
calendar.getSchedule().getItems().add(app);
[/code]

The second item is not visible because it's from yesterday. The third one is not visible because it's before the timetable's StartTime. If you aren't seeing even one item, make sure you aren't clearing the schedule later (assigning a new Schedule instance to the calendar or calling clear()).

Regards,
Slavcho

Title: Re: Jplanner Scheduler java timetable
Post by Slavcho on Apr 4th, 2019 at 9:19am

Quote:
System.out.println(calendar.getTimetableSettings().getDates().add(dt));             System.out.println(calendar.getTimetableSettings().getDates().add(dt2))


This line not only prints but also adds a column to the view (getDates().add) so it triples the columns. When I said to print them I meant not literally copying the add code inside print, but only the values to verify. In case you missed it, also check above post showing why some items are not visible (one being before the hour you pass to setStartTime, the other one being from yesterday).

Title: Re: Jplanner Scheduler java timetable
Post by robert_v on Apr 4th, 2019 at 10:36am
so what is wrong with this?


Code (java):
public class revisionPlannerUI extends JFrame {//extends JFrame creates new window, extends inherits from the parent class

    public static Calendar calendar = new Calendar();//declare new calendar

    public revisionPlannerUI() throws ParseException, ClassNotFoundException {
        initComponents();//initialise NetBeans drawn components
        calendar.setTheme(ThemeType.Light);//set calendar theme
        calendar.setCurrentView(CalendarView.Timetable);//set calendar type
        Container cp = jPanel2;//set container to the Jpanel I drew in NetBeans design
        cp.setLayout(new BorderLayout());
        cp.add(calendar, BorderLayout.CENTER);
        for (int i = 0; i < 7; i++) {
            calendar.getTimetableSettings().getDates().add(DateTime.today().addDays(i - 1));//display 7 columns to show 1 week
        }
        calendar.getTimetableSettings().setStartTime(8 * 60); //start timetable from 8am
        calendar.getTimetableSettings().setItemOffset(30);//30 mins between each interval
        calendar.getTimetableSettings().setShowItemSpans(true);
        calendar.getTimetableSettings().setSnapInterval(Duration.fromMinutes(1));//interval at which
        calendar.getTimetableSettings().setVisibleColumns(7);
        calendar.getTimetableSettings().setShowNavigationButtons(true);
        calendar.getTimetableSettings().setScrollStep(7);
        getAppointments();
        Style style = new Style();
        style.setBrush(new SolidBrush(new Color(102, 255, 102)));
        DateStyle dStyle = new DateStyle();
        calendar.getDayStyles().add(dStyle);
       
       
    }
    public static ArrayList<Item> items = new ArrayList<Item>();

    private void getAppointments() throws ParseException,ClassNotFoundException {
        try {
            String SQL = "SELECT TASKNAME, TASKDATE, STARTTIME, LENGTH, SUBJECTID FROM TIMETABLE WHERE USERID = '" + globals.dbUserID + "'";
            Connection con = DriverManager.getConnection(globals.host);
            Statement statement = con.createStatement();
            ResultSet resSet = statement.executeQuery(SQL);
            Style style = new Style();
               style.setBrush(new SolidBrush(new Color(102, 255, 102)));
            while (resSet.next()) {
                Date date = resSet.getDate("TASKDATE");
                Time time = resSet.getTime("STARTTIME");
                int length = resSet.getInt("LENGTH");
                DateTime dt = new DateTime(date.getYear(), date.getMonth()+1, date.getDay()-1, time.getHours(), time.getMinutes(), 0);
                DateTime dt2 = new DateTime(date.getYear(), date.getMonth()+1, date.getDay()-1, time.getHours(), time.getMinutes() + length, 0);
                System.out.println(date);
                System.out.println(dt);
                System.out.println(dt2);
                System.out.println(calendar.getTimetableSettings().getDates().add(dt));
                System.out.println(calendar.getTimetableSettings().getDates().add(dt2));
                String taskname = resSet.getString("TASKNAME");
                Appointment app = new Appointment();
                app.setStartTime(dt);
                app.setEndTime(dt2);
                app.setHeaderText(taskname);
                calendar.getSchedule().getItems().add(app);
               
            }
        } catch (SQLException err) {
            System.out.println(err);
        }


I am pretty sure thats exactly what you have.
Unless I do the
System.out.println(calendar.getTimetableSettings().getDates().add(dt));
                System.out.println(calendar.getTimetableSettings().getDates().add(dt2));
then I get nothing in my timetable

even with the above I get one task repeated over 7 days

I added attachments, one with sout and one without

I am approaching near to the final final deadline now so if we cant get it to work I suppose I have to hand this in with it faulty.

without_sout.PNG ( 57 KB | 214 Downloads )
with_sout.PNG ( 14 KB | 179 Downloads )

Title: Re: Jplanner Scheduler java timetable
Post by Slavcho on Apr 4th, 2019 at 11:07am
See the difference in day names in header - that's either a different month or a different year. Leave it displaying the full date as in tutorial to verify. Our DateTime constructor type expects the natural date values and not zero-based ones, so make sure you adjust values from your DB if they are zero-based.

Title: Re: Jplanner Scheduler java timetable
Post by robert_v on Apr 4th, 2019 at 11:16am
what do you mean by zero based?

Title: Re: Jplanner Scheduler java timetable
Post by robert_v on Apr 4th, 2019 at 11:16am
as in 04/04/2019?

Title: Re: Jplanner Scheduler java timetable
Post by Slavcho on Apr 4th, 2019 at 11:22am
Zero based would be January = 0, or first day of month = 0, or year counting starting from 0, while DateTime constructor expects counts starting from 1. So new DateTime(2019,4,4) is today's date and equals (DateTime.today()) should give you true. So check if your recordset is actually returning today's or this week's values (judging by day names in second screenshot they aren't from this month at all), and if some of your DB columns is zero-based (maybe month), make sure you add 1 to the value before passing to DateTime.

Title: Re: Jplanner Scheduler java timetable
Post by robert_v on Apr 4th, 2019 at 11:27am
They returned as true, they dont start from zero but it does go 01 for january for example, that should be fine right?

Title: Re: Jplanner Scheduler java timetable
Post by robert_v on Apr 4th, 2019 at 11:29am
Do I even need an Appointment then? it doesnt seem like it does anything.

Title: Re: Jplanner Scheduler java timetable
Post by robert_v on Apr 4th, 2019 at 11:36am
I tried to adapt mine to tutorial 1 but it still does nothing ;/

Title: Re: Jplanner Scheduler java timetable
Post by Slavcho on Apr 4th, 2019 at 11:37am
Appointment is the green box from your last screenshot, you decide if you want it or not. However the day names there show the dates you add aren't from same month or year (e.g. 4th = Monday, 3rd = Wednesday obviously aren't dates from same month). You will need to add appointments on same dates as the dates added to timetable settings if you want to see them.

Title: Re: Jplanner Scheduler java timetable
Post by robert_v on Apr 4th, 2019 at 11:42am
I've gotten somewhere, it's now correctly showing the appointment but still showing the day twice, how do I do this please? I literally have 10 mins to work on this.
progress.PNG ( 28 KB | 178 Downloads )

Title: Re: Jplanner Scheduler java timetable
Post by Slavcho on Apr 4th, 2019 at 11:44am
try calling timetableSettings.setHeaderDateFormat("MMM dd, yyyy") to show full dates in header, while keeping your calendar.getTimetableSettings().getDates().add(dt) calls - this should show the full dates of appointments you create. If they are not in current week, it should explain why you are not seeing them when add(dt) is missing. Otherwise it's shown twice because you also add dt2 as a column, which is same date.

Title: Re: Jplanner Scheduler java timetable
Post by robert_v on Apr 4th, 2019 at 11:52am
Hi, this is what I go when I put it in the format "dd MM yyy"
result.PNG ( 6 KB | 175 Downloads )

Title: Re: Jplanner Scheduler java timetable
Post by robert_v on Apr 4th, 2019 at 11:55am
I need to have d2 to declare end time dont I?

Title: Re: Jplanner Scheduler java timetable
Post by Slavcho on Apr 4th, 2019 at 11:56am
So now make sure you have yesterday in the dates you add to timetable settings, remove the add(dt/dt2)  lines, and you should see this appointment show correctly once. Otherwise since the dates you initially add start from today, you won't see yesterday's appointment when add(dt) is missing.

Title: Re: Jplanner Scheduler java timetable
Post by robert_v on Apr 4th, 2019 at 12:00pm
It actually worked?!!?! I was so close to this to begin with fml. Thanks so much, with 1 min to spare as well.

Absolute legend.

Title: Re: Jplanner Scheduler java timetable
Post by Slavcho on Apr 4th, 2019 at 12:02pm
If you won't know what dates you have in the database but want to make sure some of them is initially visible, maybe first create all appointments, and while the loop runs also collect the min and max dates of the appointments. Then decide whether you want to show the first or last of the appointments, and add some range around the min / max to timetable.dates.

The MindFusion Forums » Powered by YaBB 2.6.11!
YaBB Forum Software © 2000-2024. All Rights Reserved.