Friday, May 7, 2010

JAVA : Calendar

Creating a Graphical Calendar in Java:-





Graphical Calender


!!!!!!!!!!....Applet may take some time to load....!!!!!!!!!!

Graphical Calendar is a purely JAVA based application.This Java application provides you a fully function Graphical Calendar. Java does not provide a inbuilt Graphical calendar but it has a Calendar class in java.utill package.Java's Calendar class provide you a set of methods for getting date, time and other date-time arithmetics.

import java.util.Calendar;

public class Cal{

  public static void main(String[] args){
     Calendar calendar = Calendar.getInstance ();
  }

}


GregorianCalendar is a concrete subclass of Calendar and provides the standard calendar used by most of the world. A Calendar object can be initialized by GregorianCalendar class.

import java.util.Calendar;

public class Cal{

  public static void main(String[] args){
     Calendar now = GregorianCalendar.getInstance();
  }

}

This Graphical Calendar allows you to choose a year, a month and a day. On loading application uses the current date as of your system. This Graphical Calendar can also be used as a date picker in any of Java application if needed. For example in a Railway reservation system this Calendar can be used as a date picker.

Download Files Here:


Coding for making Grahical Calendar :-


Celendar.java:- In Celendar.java the coding for setting the look and feel and adding the Days panel in the frame is done.


import java.awt.*;
import javax.swing.*;
import javax.swing.UIManager.*;

public class Celendar extends JFrame{

private JPanel p;

Celendar(){
super("Calendar");

try {
   for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
   
    if ("Nimbus".equals(info.getName())) {
           UIManager.setLookAndFeel(info.getClassName());
       
           break;
       }
       else{
                                               UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
       }
   }
} catch (Exception e) {

}
setLayout(new GridLayout(1,1));
setSize(322,223);
setResizable(false);
Days d= new Days();

add(d);
}


public static void main(String aggsp[]){

Celendar c = new Celendar();
c.setVisible(true);
c.setDefaultCloseOperation(EXIT_ON_CLOSE);

}

}



Days.java:- In this Days.java file all the coding of basic calendar is done.


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.*;
import javax.swing.UIManager;
import java.util.*;

public class Days extends JPanel {

//lables use for showing weekday names
private JLabel sun;
private JLabel mon;
private JLabel tue;
private JLabel wed;
private JLabel thu;
private JLabel fri;
private JLabel sat;


        //need to images blackArrowUp.gif and upArrowUp.gif  to put on up and down button 
private Icon pic[]= {new ImageIcon(getClass().getResource("blackArrowUp.gif")),new ImageIcon(getClass().getResource("blackArrowDown.gif"))};

private JPanel mainpanel;   //for add days and weekday
private JPanel p[]= new JPanel[42]; //Array of Panels for Adding Days Buttons
private JButton b[]= new JButton[31]; //Array of 31 Buttons For 31 Days


//for year , month and date (all will be added to Pannel )


private JPanel mainp; //main Panel on which date , year and month will add
private JTextField date; //for showing date in text fields
public JComboBox month; //for months
public JTextField year; //for year
private JButton sb[]= new JButton[2]; // for increasing year by 1 on every click

//string array has name of months for adding to combobox

private String strmon[]= {"January","Febuary","March","April","May","June","July","Augest","September","October","November","December"};

//Constructor Start
Days(){


setLayout(null); //setting layout
setSize(350,228);
setVisible(true);



//first Frame Build

mainp = new JPanel();
mainp.setLayout(null);
mainp.setSize(325,31);
mainp.setLocation(1,0);



date = new JTextField(); //date textField will strore the date
date.setSize(100,30);
date.setLocation(5,0);
date.setBackground(Color.WHITE);
date.setFont(new Font("Arial",Font.PLAIN,14));
date.setEditable(false);

month = new JComboBox(strmon); //month combo box will have all months
month.setSize(90,30);
month.setLocation(110,0);


year = new JTextField("2008"); //year will have year part of date
year.setSize(70,30);
year.setLocation(205,0);
year.setBackground(Color.WHITE);
year.setEditable(false);

sb[0] = new JButton(""); //sb scroll bar increments the year by 1
sb[0].setSize(30,15);
sb[0].setLocation(275,0);
sb[0].setIcon(pic[0]);

sb[1] = new JButton(""); //sb scroll bar increments the year by 1
sb[1].setSize(30,15);
sb[1].setLocation(275,16);
sb[1].setIcon(pic[1]);

mainp.add(date);
mainp.add(month);
mainp.add(year);
mainp.add(sb[0]);
mainp.add(sb[1]);
add(mainp);


//Days Panel

mainpanel = new JPanel();
mainpanel.setLayout(new GridLayout(7,7,1,1));
mainpanel.setSize(315,160);
mainpanel.setLocation(1,31);

sun = new JLabel("Sun");
sun.setForeground(Color.RED);
mon = new JLabel("Mon");
tue = new JLabel("Tue");
wed = new JLabel("Wed");
thu = new JLabel("Thu");
fri = new JLabel("Fri");
sat = new JLabel("Sat");

//add labels to panel
mainpanel.add(sun);
mainpanel.add(mon);
mainpanel.add(tue);
mainpanel.add(wed);
mainpanel.add(thu);
mainpanel.add(fri);
mainpanel.add(sat);

//Initializing memory to Jpanels and add it to mainpanel
for (int x=0;x<42;x++){
p[x]= new JPanel();
p[x].setLayout(new GridLayout(1,1));
p[x].setBackground(Color.WHITE);
mainpanel.add(p[x]);
validate();
}

final HandlerClass handler= new HandlerClass(); // object of handlerclass

for (int i=0;i<31;i++){ //only Initializing memory to buttons not adding them
b[i]= new JButton();
b[i].addActionListener(handler);
b[i].setFont(new Font("Times New Roman",Font.PLAIN,11));
b[i].setText(Integer.toString(i+1));

}


final Calendar now = GregorianCalendar.getInstance(); //create a Calendar object
year.setText(Integer.toString(now.get(Calendar.YEAR))); //get year and month from Calendar object
month.setSelectedIndex(now.get(Calendar.MONTH)); //add values to year and month

validate();

// DAY_OF_WEEK GIVES THE DAY OF THE FROM WIHCH THE MONTH STARTS
int ye=Integer.parseInt(year.getText());
Calendar cal = new GregorianCalendar(ye, month.getSelectedIndex(), 1);

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

for (int i=0;i             //loop running according to the number of days  
p[dayOfWeek-1].add(b[i]);               //adding the days button on pannels according to the dayof week
dayOfWeek++;
}

int bd=now.get(Calendar.DATE); //getting the current date
b[bd-1].setEnabled(false); // sets the current date button enabled false


add(mainpanel); // add main pannel to

validate();

month.addItemListener(
new ItemListener(){

public void itemStateChanged(ItemEvent event){
if (event.getStateChange()==ItemEvent.SELECTED){
mainpanel.removeAll();
validate();

mainpanel.add(sun);
mainpanel.add(mon);
mainpanel.add(tue);
mainpanel.add(wed);
mainpanel.add(thu);
mainpanel.add(fri);
mainpanel.add(sat);

for (int x=0;x<42;x++){

p[x].removeAll();
mainpanel.add(p[x]);
validate();
}


int ye=Integer.parseInt(year.getText());
Calendar cal = new GregorianCalendar(ye, month.getSelectedIndex(), 1);

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

for (int i=0;i

p[dayOfWeek-1].add(b[i]);
dayOfWeek++;
validate();
}
validate();
}
mainp.validate();
validate();

date.setText(Integer.toString(handler.db +1).concat(" / ").concat(Integer.toString(month.getSelectedIndex() + 1)).concat(" / ").concat(year.getText()));
}
}
);


sb[0].addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
int y = Integer.parseInt(year.getText());
y++;
year.setText(Integer.toString(y));



mainpanel.removeAll();
validate();

mainpanel.add(sun);
mainpanel.add(mon);
mainpanel.add(tue);
mainpanel.add(wed);
mainpanel.add(thu);
mainpanel.add(fri);
mainpanel.add(sat);

for (int x=0;x<42;x++){


p[x].removeAll();
mainpanel.add(p[x]);
validate();
}


int ye=Integer.parseInt(year.getText());
Calendar cal = new GregorianCalendar(ye, month.getSelectedIndex(), 1);

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

for (int i=0;i

p[dayOfWeek-1].add(b[i]);
dayOfWeek++;
validate();
}


validate();
//sets the date on Click of year Button
date.setText(Integer.toString(handler.db +1).concat(" / ").concat(Integer.toString(month.getSelectedIndex() + 1)).concat(" / ").concat(year.getText()));

}

}
);



sb[1].addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
int y = Integer.parseInt(year.getText());
y--;
year.setText(Integer.toString(y));



mainpanel.removeAll();
validate();

mainpanel.add(sun);
mainpanel.add(mon);
mainpanel.add(tue);
mainpanel.add(wed);
mainpanel.add(thu);
mainpanel.add(fri);
mainpanel.add(sat);

for (int x=0;x<42;x++){


p[x].removeAll();
mainpanel.add(p[x]);
validate();
}


int ye=Integer.parseInt(year.getText());
Calendar cal = new GregorianCalendar(ye, month.getSelectedIndex(), 1);

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

for (int i=0;i

p[dayOfWeek-1].add(b[i]);
dayOfWeek++;
validate();
}


validate();
//sets the date on Click of year Button
date.setText(Integer.toString(handler.db +1).concat(" / ").concat(Integer.toString(month.getSelectedIndex() + 1)).concat(" / ").concat(year.getText()));

}

}
);



//sets the date on form load
date.setText(Integer.toString(handler.db +1).concat(" / ").concat(Integer.toString(month.getSelectedIndex() + 1)).concat(" / ").concat(year.getText()));
}





//returns no. of days in a week
public int getDaysNo(){
int no = 31;
if (month.getSelectedIndex()==1){
no=28;
if (Integer.parseInt(year.getText())%4==0){
no=29;

}
month.validate();
}

if (month.getSelectedIndex()==3 || month.getSelectedIndex()==5 || month.getSelectedIndex()==8 || month.getSelectedIndex()==10)
{
no=30;
}
return no;

}


// handler class set which date is selected

class HandlerClass implements ActionListener{


Calendar now = Calendar.getInstance();
public int db=(now.get(Calendar.DATE))-1;

public void actionPerformed(ActionEvent e){

for (int k=0;k<31;k++){

if (e.getSource()==b[k])
{
b[k].setEnabled(false);
validate();
db=k;

}
else{
b[k].setEnabled(true);
validate();
}
month.validate();

}


date.setText(Integer.toString(db +1).concat(" / ").concat(Integer.toString(month.getSelectedIndex()+1)).concat(" / ").concat(year.getText()));
}


}

}


21 comments:

  1. Fine Work... Superb!!! My heartly thanks for your service.

    ReplyDelete
  2. superb!!great work..

    ReplyDelete
  3. The MVC of JCalendar shows me why that is a better approach over trying to re-invent the model in the GUI. What if you wanted to show just 5 days a week? Or what if the Calendar widget should be international in its functionality?

    ReplyDelete
  4. hey can any1 help me hw 2 work dis...plsss

    ReplyDelete
  5. wow!! great! thanks :D

    ReplyDelete
  6. i don't know how to you use the source code.
    They are many problems and errors, when I bind it in my projekt as class. It would take a lot of package, i didn't have.

    ReplyDelete
  7. how to open this using JDK please? thanks!!

    ReplyDelete
  8. thanks for posting

    ReplyDelete
  9. I really wish this code was not so flawed. I created both classes via copy and paste. The days class has so many red lines under the code I'm not sure how to fix it.

    ReplyDelete
    Replies
    1. Copy and paste from this text did not work, however, I downloaded the source code and opened the days class in netbeans and copied that to my days class...and now it works. Thanks for the code.

      Delete
    2. i copied exactly the same but its not work. My program shown "Class "Days" does not have a main method."
      do you have any idea?

      Delete
  10. how super sir.... u really done a good job sir

    ReplyDelete
  11. Hello there I am so thrilled I found your site, I really found you by
    error, while I was searching on Google for something else, Regardless I am here now and would just like to say thank you for a
    fantastic post and a all round interesting blog
    (I also love the theme/design), I don’t have time to look over it all
    at the minute but I have book-marked it and also added in your RSS feeds, so when I have time I will be back to read a great deal more, Please do keep up the fantastic b.


    My web page - personal bad credit loans

    ReplyDelete
  12. My program shown "Class "Days" does not have a main method."
    anyone know the problem?

    ReplyDelete
  13. Your cuггent pоst featuгes verified
    bеneficiаl to me pеrsonаlly.
    It’s quite useful аnd you аre cleагly гeаlly eԁuсаteԁ in this rеgion.
    You get exposed оur faсe tο ѵarying vieωs on thiѕ pаrticulaг ѕubjeсt along with inteгеstіng аnd strong
    articles.
    Here is my page ; Xanax

    ReplyDelete
  14. Youг current repoгt has confirmed necessary to mysеlf.
    Іt’ѕ extremely educаtiοnal аnd уou're obviously really experienced in this region. You have got opened my personal eye to numerous views on this specific matter together with interesting and strong content material.
    My blog - Cialis Online

    ReplyDelete
  15. Replies
    1. agreed. fake coding. i copied it for the exams and i failed fuck you . son of bich. i am @Ashvin Dussoneah Facebook

      Delete