hello everybody ^^
I have to make a calander ,and to let the user to insert any event !
the calander it self is working <<but I don't know how can I write the code to give the user the ability to enter a new event by entering some fields like (title,location,date ..),and when I click on any day >>it should show me what events are in that day !
I've tried different things but it doesn't work <<and I don't know how to solve that<<
plz help me :))

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.GregorianCalendar;

import javax.swing.*;
import javax.swing.border.LineBorder;


public class CalendarPro extends JFrame  {
     private JButton B[],B_Prevoius,B2_Next;
     private String Names_Days[]={"Sat","Sun", "Mon", "Tue", "Wed", "Thu", "Fri"};
     private JLabel L_Days[],L1_HeaderMonth,L2_Time;
     private GridLayout G1,G2;
     private Container C;
     private JPanel P1,P2;
     private JTextArea T1;
     private MyThread th;
     private String[] months =  {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
     private int nod, som;
     private GregorianCalendar cal;
     private int realYear, realMonth, realDay, currentYear, currentMonth;
//Variables 

     public CalendarPro (){
         super ("Calendar");
         setLayout (null);
        // setBorder(BorderFactory.createTitledBorder("Calendar"));

            cal = new GregorianCalendar(); //Create calendar
            realDay = cal.get(GregorianCalendar.DAY_OF_MONTH); //Get day
            realMonth = cal.get(GregorianCalendar.MONTH); //Get month
            realYear = cal.get(GregorianCalendar.YEAR); //Get year
            currentMonth = realMonth; //Match month and year
            currentYear = realYear;
          B=new JButton [43];
          B_Prevoius=new JButton ("<<");
          B2_Next=new JButton (">>");

         L_Days=new JLabel[7];
         L1_HeaderMonth=new JLabel("January");
         L2_Time=new JLabel();
        G1=new GridLayout(1,7);
         G2=new GridLayout(6,7);
         C=getContentPane ();
            C.setBackground(Color.white);

          P1=new JPanel();
          P2=new JPanel();
          P1.setLayout (G1);
          P2.setLayout (G2);
            T1=new JTextArea("Your Events in this day :");
            th=new MyThread  ();
            Button Handler1=new Button  ();   

        for (int i=0;i<L_Days.length;i++){

            L_Days[i]=new  JLabel(Names_Days[i]);
            L_Days[i].setForeground(new Color (255, 175, 175));
            L_Days[i].setFont(new Font ("Monospaced",Font.PLAIN +Font.BOLD,16));

            P1.add(L_Days[i]);

        }

        for (int j=1;j<B.length;j++){

            B[j]=new JButton ("");
            B[j].setForeground(Color.BLACK);
            B[j].setBackground(Color.lightGray);
            B[j].setBorder(new LineBorder(Color.PINK));
            B[j].addActionListener(Handler1);
            P2.add(B[j]);

        }
        L2_Time.setSize(175, 30);
        L2_Time.setLocation(5,5);
        L2_Time.setBorder(new LineBorder(Color.BLACK));
        L2_Time.setForeground(Color.PINK);

        add(L2_Time);
        B_Prevoius.setSize(50, 30);
        B_Prevoius.setLocation(5,65);
        B_Prevoius.setForeground(Color.BLACK);
        B_Prevoius.setBackground(Color.PINK);

        add (B_Prevoius);

        B2_Next.setSize(50, 30);
        B2_Next.setLocation(350,65);
        B2_Next.setForeground(Color.black);
        B2_Next.setBackground(Color.PINK);

        add (B2_Next);


        L1_HeaderMonth.setSize(165, 30);
        L1_HeaderMonth.setLocation(155,65);
        L1_HeaderMonth.setForeground(Color.PINK);
        L1_HeaderMonth.setFont(new Font ("Monospaced",Font.PLAIN +Font.BOLD,18));
        add (L1_HeaderMonth);

        P1.setSize(350, 30);
        P1.setLocation(35,100);
        add (P1);
        P2.setSize(350, 300);
        P2.setLocation(35,135);
        add (P2);

        T1.setSize(350, 100);
        T1.setLocation(35,440);
        T1.setBorder(new LineBorder(Color.BLACK));

        add (T1);



        B_Prevoius.addActionListener(Handler1);
          B2_Next.addActionListener(Handler1);
         refreshCalendar (realMonth, realYear); //Refresh calendar

     }//constructer

private class MyThread extends Thread {

    public MyThread (){
        super ();
        start();


    }
    public void run (){
        while (true){
            try {
            Thread .sleep(1000);
            cal = new GregorianCalendar(); //Create calendar
            L2_Time.setText(cal.getTime().toString());
            }
            catch (InterruptedException E){
                System.err.println (E.toString());
            }}}
}//innerClass


private class Button implements ActionListener{
    public void actionPerformed (ActionEvent e){
        String event = ((JButton) e.getSource()).getText();

        if (e.getSource()==  B2_Next){
        if (currentMonth == 11){ //Foward one year
            currentMonth = 0;
            currentYear += 1;


            L1_HeaderMonth.setText((months[currentMonth]+"/"+currentYear));

        }
        else{ //Foward one month
            currentMonth += 1;
        }
        refreshCalendar(currentMonth, currentYear);
    }
        else if (e.getSource() ==   B_Prevoius ){
            if (currentMonth == 0){ //Back one year
                currentMonth = 11;
                currentYear -= 1;
            }
            else{ //Back one month
                currentMonth -= 1;
            }
            refreshCalendar(currentMonth, currentYear);
        }//else if



    else {
                //Boolean Flag =true;

            JButton object=(JButton)e.getSource();
            Select(object);

    }
    }
    }
 public void refreshCalendar(int month, int year){


   int lDay = cal.get(cal.DAY_OF_MONTH);
   L1_HeaderMonth.setText((months[month]+"/"+year));
//Clear

    for (int m=1;m<B.length;m++){
        B[m].setBackground(Color.lightGray);
        B[m].setText("");

    }
  //Get first day of month and number of days
 GregorianCalendar cal = new GregorianCalendar(year, month, 1);
 nod = cal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
som = cal.get(GregorianCalendar.DAY_OF_WEEK);

if ((realMonth == month) && (realYear == year)) {//today
if ((realMonth== month) && (realYear == year) && (realDay == lDay)) {
     B[realDay+4].setBackground(Color.DARK_GRAY);
}
}
for (int n=1;n<=nod; n++){
    B[som+1].setBackground(new Color (220, 220, 255));
    // B[].setBackground(Color.DARK_GRAY);

    B[som+n].setText(String.valueOf(n));

}
}
 public void Select (JButton o){
     for (int m=1;m<B.length;m++){
            B[m].setBackground(Color.lightGray);
           int lDay = cal.get(cal.DAY_OF_MONTH);
            B[som+1].setBackground(new Color (220, 220, 255));
            if ((realMonth == currentMonth) && (realYear == currentYear)) {//today
                //System.out.println(realMonth+" "+currentMonth+" "+realYear+" "+currentYear);

                if ((realMonth== currentMonth) && (realYear == currentYear) && (realDay ==lDay)) {
                     B[realDay+4].setBackground(Color.DARK_GRAY);
                }
                }

     }
     o.setBackground(Color.YELLOW);



 }

 }

}//OuterClass

-----------------------------------
main code

------------------------------------

    import java.awt.Color;
      import javax.swing.JFrame;
    public class MainTest {

public static void main(String[] args) {
    CalendarPro v =new CalendarPro();
    v.setSize(450,600);
    v.setVisible(true);
    v.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

Recommended Answers

All 7 Replies

You should have a class: Year, which has twelve instances of a class Month, which has x instances of class Day. in class Day you keep a List or Set of Appointments.

All you have so far is GUI. You'll need actual logic to go with it, too.

If you need to add events, then the obvious way to start is to create an Event class. It would have start date/time, and date/time, title, location etc as instance variables, with the usual getters and setters and a constructors. You can use GregorianCalendar instances for the start and end dates if you want (I don't agree with stultuske in this particular case).

Now you can do the rest in simple sub-tasks:

Create a simple GUI form to enter the data for a new Event.
Create a list of Events.
Define methods to search the list - eg get the Event(s) for a specified date
Use that to get the events for each day in you Calendar and display them
etc

James ,, the list of events which I have to create >>is it a vector ?? or an array of list ?? or it's just an array of strings ??
I didn't get how can I take the saved event for the day ??
could U tell me more about that ??
the thing that I have to do is that whenever I clicked on any day in the calander (I've created the days as buttons)>>it should show me the events of that day in a textArea which I've put under the calendar in the same frame !
and also >>in your opinion which is easier ! to make a button (addEvent(it should give me the frame that I can insert the event))or once I click to a certain day it should popup a frame (and inside that frame I can insert the event ) but it still I can't do the get event !I don't know how and where can I make it !
and Thank U so much ^_^

All of that actually depends on your needs and requirements.

ok >>so I want to make a button (addevent) and when I click on it >>it should give me a frame where can I insert the event >>so in that frame (there is 2 textFields for location and title and there is 3 combo boxes >> one for days one for years and one for monthes )!
and also 2 textFields for start and end time !
consider that the user entered all these things !!
but actually >>I didn't know where should I save all these information of that event in order to put them in the text area and also whenever I click on that day it should give me the events on that day !! and also I have to highlight the days which have an event !
I don't know how to do that !!
could U help me plz ?! :(

Create the Event class first.
The create a List<Event>. It doesn't matter what kind of List it is (ArrayList, LinkedList, Vector etc).

ok thank U ^_^ I'll try that >> and I hope it will work :/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.