• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help with JTable

 
john mattucci
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im designing a program , which is my first by the way using swing; anyways I would like to design a program which allows a user to enter data into a JTable. My big problem is that I would like a JTable to represent a day, so for example today being Dec 06 2000 once I enter the program it should come up as Dec 06 2000. In other words what class do I need to use Date, Calendar or GregorianCalendar and how do I tie this to the JTable. And be able to navigate forward and backward in the calendar and in the end save it to a file. Can someone please guide me in the right direction even by simply showing me which site to search for further information.
All help is greatly appreciated
 
Vinod Venugopal
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
This is a sample code of a Calender app I was trying a few months back..it works upto a certain extent :-), there r totally 3 files, I have given here the code for the Calender part..You got to use java.util's classes for the calender stuff..
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.net.*;
import java.lang.reflect.*;

public class CalenderModel extends AbstractTableModel
{

String[] days = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
public static int[] numDays={31,28,31,30,31,30,31,31,30,31,30,31};
String[][] calender = new String[7][7];
public static int num;
public static int offset;

public CalenderModel()
{
for(int i=0;i<days.length;i++)>
{
calender[0][i] = days[i];
}
for(int j=1;j<7;j++)
{
for(int k=0;k<7;k++)
{
calender[j][k] = "";//days[i];
}
}
}//end of constructor CalenderModel()



public void setYearMonth(int year,int month)
{
System.out.println("Entered setYearMonth("+year+","+month+")");
//make blank table first...
for(int i =1;i< 7;i++)
{
for(int j = 0; j<7;j++ )
{
calender[i][j] = "";

}

}

java.util.GregorianCalendar cal = new java.util.GregorianCalendar();
cal.set(year,month,1); // set start date
offset = cal.get(java.util.GregorianCalendar.DAY_OF_WEEK); // this tells the date where to start in table...
System.out.println("OFFSET = " + offset);
System.out.println("offset/7 = " + offset/7);
System.out.println("offset%7 = " + offset%7);

num = daysInMonth(year,month);
System.out.println("NUM = " + num);
for(int i = 1;i <= num; ++i)
{
calender[offset/7][offset%7] = Integer.toString(i);
offset++;

}

}//end of method setYearMonth(int,int)

public int daysInMonth(int year, int month)
{
int days = numDays[month];

return days;

}//endof method daysInMonth(int,int)
//default methods of AbstractTableModel..
public String getColumnName(int col)
{
return days[col];
}//endof method getColumnName(int)

public int getRowCount()
{
return 7;

}//end of method getRowCount()

public int getColumnCount()
{
return 7;

}// endof method getColumnCount()

public Object getValueAt(int row, int col)
{
return calender[row][col];

}//endof method getValueAt()

public void setValue(Object value, int row, int col)
{
calender[row][col] = (String)value;

}//endof method setValue(Object,int,int)

}//endof class CalenderModel{}
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John, you can review the API for research you were talking about doing.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!