Reputation: 169
// I am creating customize jcalender in swing
//I am facing problem regarding dates set //i.e actual month is start from tuesday and it is starting from wednesday // I am finding problem regarding setting values in calender-model
//Above code is running fine but problem in showing actual dates //please suggest me to make necessary changes
import java.awt.Color;
public class Calendar extends JFrame {
private JPanel contentPane;
String[] years = { "2008", "2009", "2010" ,"2011","2012","2013","2014"};
JComboBox comboBox = new JComboBox(years);
String[] months = { "January", "February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December" };
JList list = new JList(months);
JScrollPane scrollPane = new JScrollPane(list);
CalendarModel model = new CalendarModel();
JTable table = new JTable(model);
public Calendar() {
super();
getContentPane().setLayout(null);
comboBox.setBounds(10, 10, 100, 30);
comboBox.setSelectedIndex(5);
comboBox.addItemListener(new ComboHandler());
scrollPane.setBounds(200, 10, 150, 100);
list.setSelectedIndex(10);
list.addListSelectionListener(new ListHandler());
table.setBounds(10, 150, 550, 200);
model.setMonth(comboBox.getSelectedIndex() + 1998, list.getSelectedIndex());
getContentPane().add(comboBox);
getContentPane().add(scrollPane);
table.setGridColor(Color.black);
table.setShowGrid(true);
getContentPane().add(table);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(530,300);
setVisible(true);
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Calendar frame = new Calendar();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public class ComboHandler implements ItemListener {
public void itemStateChanged(ItemEvent e) {
System.out.println("ItemState change Method called ");
model.setMonth(comboBox.getSelectedIndex() + 1998, list.getSelectedIndex());
table.repaint();
}
}
public class ListHandler implements ListSelectionListener {
public void valueChanged(ListSelectionEvent e) {
model.setMonth(comboBox.getSelectedIndex() + 1998, list.getSelectedIndex());
table.repaint();
}
}
}
class CalendarModel extends AbstractTableModel {
String[] days = { "Sun" , "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
int[] numDays = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
String[][] calendar = new String[7][7];
public CalendarModel() {
//setting name of days (mon,tues)
for (int i = 0; i < days.length; ++i)
{
System.out.println("day[i] *** "+days[i]);
calendar[0][i] = days[i];
}
for (int i = 1; i < 7; ++i){
for (int j = 0; j < 7; ++j)
{
System.out.println("calendar[i][j] value i ** "+i +" j value "+ j );
calendar[i][j] = " ";
}
}
}
public int getRowCount() {
return 7;
}
public int getColumnCount() {
return 7;
}
public Object getValueAt(int row, int column) {
System.out.println("get Value at ** "+calendar[row][column]);
return calendar[row][column];
}
/*
public void setValueAt(Object value, int row, int column) {
System.out.println("set Value at ** "+(String) value);
calendar[row][column] = (String) value;
}*/
public void setMonth(int year, int month) {
for (int i = 1; i < 7; ++i)
{
for (int j = 0; j < 7; ++j)
{
calendar[i][j] = " ";
}
}
java.util.GregorianCalendar cal = new java.util.GregorianCalendar();
cal.set(year, month, 1);
int offset = cal.get(java.util.GregorianCalendar.DAY_OF_WEEK) - 1;
offset += 7;
int num = daysInMonth(year, month);
for (int i = 0; i < num; ++i) {
System.out.println("offset *** "+Integer.toString(i+1));
calendar[offset / 7][offset % 7] = Integer.toString(i+1);
++offset;
}
}
public boolean isLeapYear(int year) {
System.out.println("Is Leap Year *** ");
if (year % 4 == 0)
return true;
return false;
}
public int daysInMonth(int year, int month) {
System.out.println("day In month*** ");
int days = numDays[month];
if (month == 1 && isLeapYear(year))
++days;
System.out.println("days *** "+days);
return days;
}
}
Upvotes: 0
Views: 747
Reputation: 347314
I don't know if these are related but...
public void itemStateChanged(ItemEvent e) {
int index = comboBox.getSelectedIndex();
System.out.println("index = " + index + "; " + (index + 1998));
model.setMonth(comboBox.getSelectedIndex() + 1998, list.getSelectedIndex());
table.repaint();
}
Doesn't produce results which match those that are listed within the combo box. For example, if you select the first value, it will equal 1998
, not 2008
So, if I select 2013
from the combo box, I'm actually getting 2003
.
When I changed it to
public void itemStateChanged(ItemEvent e) {
int index = comboBox.getSelectedIndex();
System.out.println("index = " + index + "; " + (index + 2008));
model.setMonth(comboBox.getSelectedIndex() + 2008, list.getSelectedIndex());
table.repaint();
}
It gave me better results.
I also changed
java.util.GregorianCalendar cal = new java.util.GregorianCalendar();
to
java.util.Calendar cal = java.util.Calendar.getInstance();
You should avoid making assumptions about the users calendar system if possible...
I'd also avoid null
layouts as well ;)
You should also avoid calling table.repaint
when you change the table model, instead try using fireTableDataChanged
from within the setMonth
method
I would, personally, add the listeners to the combobox and JList
before setting there selected indexes, this will allow the listeners to update the model for you...
You'll also need to fix you ListHandler
so that it uses 2008
instead 1998
Upvotes: 2
Reputation: 3140
try this.. use - 2 instead of - 1 in offset calculation.
java.util.GregorianCalendar cal = new java.util.GregorianCalendar();
cal.set(year, month, 1);
int offset = cal.get(java.util.GregorianCalendar.DAY_OF_WEEK)- 2 ;
offset += 7;
int num = daysInMonth(year, month);
for (int i = 0; i < num; ++i) {
System.out.println("offset *** "+Integer.toString(i+1));
calendar[offset / 7][offset % 7] = Integer.toString(i+1);
++offset;
Upvotes: 0
Reputation: 3197
Have a try with this in setMonth method
public void setMonth(int year, int month)
Change
calendar[offset / 7][offset % 7] = Integer.toString(i+1);
To
calendar[offset / 7][offset % 7] = Integer.toString(i+2);
Upvotes: 1