I m giving u the code which i have written 2 years back for my college project.
This is not exactly using table but calendar is there.
package developmentkit.mylabels;
import javax.swing.*;
import java.awt.*;
public class CalLabel extends JLabel{
private Color cl;
private boolean blank;
private int date,month,year,day;
public CalLabel(String string) {
super(string);
}
public CalLabel() {
super();
}
public void setColor(Color cl){
this.cl=cl;
}
public Color getColor(){
return cl;
}
public void setBlank(boolean blank){
this.blank = blank;
}
public boolean getBlank(){
return blank;
}
public void setDate(int date){
this.date = date;
}
public void setMonth(int month){
this.month = month;
}
public void setYear(int year){
this.year = year;
}
public void setDay(int day){
this.day = day;
}
public int getDate(){
return date;
}
public int getMonth(){
return month;
}
public int getYear(){
return year;
}
public int getDay(){
return day;
}
}
package developmentkit.calendar;
import developmentkit.mylabels.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.*;
import java.util.Date;
import java.awt.event.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.*;
public class Calendar extends JDialog implements MouseListener,ChangeListener,ActionListener{
private CalLabel lbl[][];
private CalLabel lblDays[];
private JButton btnMonth;
private JButton btn[];
private int tDate,tMonth,tYear,tDay,sDate,sMonth,sYear,sDay;
private final String day[]={"Mon","Tue","Wed ","Thu","Fri","Sat","Sun"};
private final String months[]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug", "Sep","Oct","Nov","Dec"};
private Color clTemp,colorHoliday;
private Date dObj;
private int currYear,currMonth,currDate,currDay;
private JPanel pnlTop,pnlBottom,pnlMonth;
private JButton btnMonths[][];
private final int days[][]={
{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30 ,31,30,31,31,30,31,30,31}
};
private Color colorCurrentDate,colorSelectedDate,colorBackground ,colorText;
private JSpinner spinnerYear;
private int x,y;
public Calendar(){
this(10,10);
}
public Calendar(int x,int y){
dObj= new Date();
this.x = x;
this.y=y;
this.currDate = dObj.getDate();
this.currMonth = (dObj.getMonth()+1);
this.currYear = (dObj.getYear()+1900);
setCalendar();
}
public Calendar(int currDate,int currMonth,int currYear,int x,int y){
this.currDate=currDate;
this.currMonth=currMonth;
this.currYear=currYear;
this.x=x;
this.y=y;
setCalendar();
}
public Calendar(int currDate,int currMonth,int currYear) {
this(currDate,currMonth,currYear,10,10);
}
public void setCalendarColor(){
colorCurrentDate=Color.blue;
colorSelectedDate=Color.orange;
colorBackground=Color.white;
colorText=Color.black;
colorHoliday=Color.red;
}
public void setCurrentDateColor(Color colorCurrentDate){
this.colorCurrentDate=colorCurrentDate;
}
public void setSelectedDateColor(Color colorSelectedDate){
this.colorSelectedDate=colorSelectedDate;
}
public void setCalendarBackgroundColor(Color colorBackground){
this.colorBackground=colorBackground;
}
public void setCalendarTextColor(Color colorText){
this.colorText=colorText;
}
public void setHolidayColor(Color colorHoliday){
this.colorHoliday=colorHoliday;
}
public Color getCurrentDateColor(){
return this.colorCurrentDate;
}
public Color getSelectedDateColor(){
return this.colorSelectedDate;
}
public Color getCalendarBackgroundColor(){
return this.colorBackground;
}
public Color getCalendarTextColor(){
return this.colorText;
}
public Color getHolidayColor(){
return this.colorHoliday;
}
public void actionPerformed(ActionEvent ae){
int index=0;
if(ae.getSource()==btnMonth){
pnlMonth.setVisible(true);
pnlBottom.setVisible(false);
printCalendar(tMonth,tYear);
}
else{
if(ae.getSource()==btnMonths[0][0]){
index=1;
}
else if(ae.getSource()==btnMonths[0][1]){
index=2;
}
else if(ae.getSource()==btnMonths[0][2]){
index=3;
}
else if(ae.getSource()==btnMonths[0][3]){
index=4;
}
else if(ae.getSource()==btnMonths[1][0]){
index=5;
}
else if(ae.getSource()==btnMonths[1][1]){
index=6;
}
else if(ae.getSource()==btnMonths[1][2]){
index=7;
}
else if(ae.getSource()==btnMonths[1][3]){
index=8;
}
else if(ae.getSource()==btnMonths[2][0]){
index=9;
}
else if(ae.getSource()==btnMonths[2][1]){
index=10;
}
else if(ae.getSource()==btnMonths[2][2]){
index=11;
}
else if(ae.getSource()==btnMonths[2][3]){
index=12;
}
tMonth=index;
pnlMonth.setVisible(false);
pnlBottom.setVisible(true);
printCalendar(tMonth,tYear);
btnMonth.setText(months[tMonth-1]);
}//else
}
final public void setResizable(boolean b){
super.setResizable(false);
}
private void setCalendar(){
getContentPane().setSize(210,190);
setResizable(false);
getContentPane().setLayout(new FlowLayout());
getContentPane().setFont(new Font(Font.SANS_SERIF,Font.PLAIN,10));
pnlTop = new JPanel();
pnlTop.setLayout(new FlowLayout());
pnlBottom = new JPanel();
pnlBottom.setLayout(new GridLayout(7,7));
pnlBottom.setSize(170,170);
pnlMonth = new JPanel(new GridLayout(4,3));
pnlMonth.setSize(200,110);
add(pnlTop);
add(pnlMonth);
add(pnlBottom);
pnlMonth.setVisible(false);
lblDays=new CalLabel[7];
btnMonth = new JButton();
btnMonth.addActionListener(this);
spinnerYear = new JSpinner();
spinnerYear.addChangeListener(this);
spinnerYear.setSize(100,100);
lbl=new CalLabel[6][7];
pnlTop.add(btnMonth);
pnlTop.add(spinnerYear);
btnMonths=new JButton[3][4];
for(int i=0,k=0;i<3;i++){
for(int j=0;j<4;j++){
btnMonths[i][j] = new JButton(months[k]);
btnMonths[i][j].addActionListener(this);
pnlMonth.add(btnMonths[i][j]);
k++;
}
}
for(int i=0;i<7;i++){
lblDays[i]=new CalLabel(day[i]);
lblDays[i].setHorizontalAlignment(0);
pnlBottom.add(lblDays[i]);
}
for(int i=0;i<6;i++){
for(int j=0;j<7;j++){
lbl[i][j] = new CalLabel();
lbl[i][j].addMouseListener(this);
}//inner for
}//outer for
setCalendarColor();
}
public final void showCalendar(){
showCalendar(this.x,this.y);
}
public final void showCalendar(int x, int y){
this.x=x;
this.y=y;
setLocation(x, y);
setModal(true);
setAlwaysOnTop(true);
for(int i=0;i<6;i++){
for(int j=0;j<7;j++){
lbl[i][j].setOpaque(true);
lbl[i][j].setHorizontalAlignment(0);
lbl[i][j].setForeground(colorText);
lbl[i][j].setBackground(colorBackground);
lbl[i][j].setColor(colorBackground);
lbl[i][j].setBlank(true);
lbl[i][j].setEnabled(false);
pnlBottom.add(lbl[i][j]);
}//inner for
}//outer for
printCalendar(currMonth,currYear);
spinnerYear.setValue(currYear);
btnMonth.setText(months[currMonth-1]);
setVisible(true);
}
public final void printCalendar(int month,int year){
int baseYear = 1;
int exDays=0;
exDays = year - baseYear;
for(int i=baseYear;i<year;i++){
if(isLeap(i)==1)
exDays++;
}//for
int currLeap=isLeap(year);
for(int i=0;i<month-1;i++){
exDays+=days[currLeap][i];
}//for
exDays=exDays%7;
tMonth = month;
tYear = year;
for(int i=0;i<6;i++){
for(int j=0;j<7;j++){
lbl[i][j].setBlank(true);
lbl[i][j].setText("");
lbl[i][j].setForeground(colorText);
lbl[i][j].setBackground(colorBackground);
lbl[i][j].setColor(colorBackground);
lbl[i][j].setMonth(month);
lbl[i][j].setYear(year);
lbl[i][j].setDay(0);
lbl[i][j].setEnabled(false);
}//inner for
}//outer for
int k=1,j=0;
for(int i=0;i<6;i++){
for(j=0;k<=days[currLeap][month-1] && j<7;j++){
if(k==currDate && month==currMonth && year==currYear){
lbl[i][j].setForeground(colorCurrentDate);
currDay=j;
}//if
if(exDays!=0){
lbl[i][j].setText("");
exDays--;
}//if
else{
lbl[i][j].setText(String.valueOf(k));
lbl[i][j].setDate(k);
lbl[i][j].setMonth(tMonth);
lbl[i][j].setYear(tYear);
lbl[i][j].setDay(j);
lbl[i][j].setEnabled(true);
if(currDate==k)
tDay=j;
lbl[i][j].setToolTipText(lbl[i][j].getText()+months[lbl[i][j].getMonth()-1]+lbl[i][j].getYear());
lbl[i][j].setBlank(false);
k++;
}//else
}//inner for
lbl[i][6].setForeground(colorHoliday);
}//outer for
}//function print calender
public void mouseClicked(MouseEvent e) {
CalLabel temp=new CalLabel();
temp=(CalLabel)e.getSource();
if(temp.getBlank()==false){
setSelectedDate(temp.getDate());
setSelectedDay(temp.getDay());
setSelectedMonth(temp.getMonth());
setSelectedYear(temp.getYear());
temp.setColor(clTemp);
temp.setBackground(clTemp);
dispose();
}
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
CalLabel temp=new CalLabel();
temp=(CalLabel)e.getSource();
if(temp.getBlank()==false) {
clTemp = temp.getColor();
temp.setColor(colorSelectedDate);
temp.setBackground(colorSelectedDate);
}
}
public void stateChanged(ChangeEvent e) {
if(e.getSource()==spinnerYear){
tYear=Integer.parseInt(String.valueOf(spinnerYear. getValue()));
printCalendar(tMonth,tYear);
}
}
public void mouseExited(MouseEvent e) {
CalLabel temp=new CalLabel();
temp=(CalLabel)e.getSource();
if(temp.getBlank()==false){
temp.setColor(clTemp);
temp.setBackground(clTemp);
}
}
public int isLeap(int y){
if(y%100==0){
if(y%400==0)
return 1;
else
return 0;
}
else if(y%4==0)
return 1;
else
return 0;
}
public void setCurrentDate(int currDate)
{
this.currDate=currDate;
}
public void setCurrentDay(int currDay)
{
this.currDay=currDay;
}
public void setCurrentMonth(int currMonth)
{
this.currMonth=currMonth;
}
public void setCurrentYear(int currYear)
{
this.currYear=currYear;
}
public int getCurrentDate()
{
return currDate;
}
public int getCurrentDay()
{
return currDay;
}
public int getCurrentMonth()
{
return currMonth;
}
public int getCurrentYear()
{
return currYear;
}
public void setSelectedDate(int sDate)
{
this.sDate=sDate;
}
public void setSelectedDay(int sDay)
{
this.sDay=sDay;
}
public void setSelectedMonth(int sMonth)
{
this.sMonth=sMonth;
}
public void setSelectedYear(int sYear)
{
this.sYear=sYear;
}
public int getSelectedDate()
{
return sDate;
}
public int getSelectedDay()
{
return sDay;
}
public int getSelectedMonth()
{
return sMonth;
}
public int getSelectedYear()
{
return sYear;
}
}//Calendar