Hi guys, i got a problem with my Calendar Gui, uhmm, i can't display right the days in the right week of it, also i need to click the cell table in order to me to display the output, please help me with this guys, please.
This is my Gui codes for my Gui Calendar, with a problem during displaying the output
[code] import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Calendar implements ActionListener { private int year = 0; private int month = 0; JFrame frame = new JFrame ("CALENDAR"); JPanel bot_panel = new JPanel(); JPanel top_panel = new JPanel (new GridLayout(2,4,5,5)); JTable table; JScrollPane pane; JComboBox cbo_month = new JComboBox(); JLabel lbl_year = new JLabel ("YEAR"); JLabel lbl_month = new JLabel ("MONTH"); JLabel lbl_null = new JLabel (""); JTextField txt_year = new JTextField(9); JButton btn_display = new JButton ("DISPLAY"); String [] header = {"SUN","MON","TUE","WED","THU","FRI","SAT"}; Object [] [] day = new Object [7] [header.length]; public Calendar() { cbo_month.addItem("-Select Month-"); cbo_month.addItem("January"); cbo_month.addItem("February"); cbo_month.addItem("March"); cbo_month.addItem("April"); cbo_month.addItem("May"); cbo_month.addItem("June"); cbo_month.addItem("July"); cbo_month.addItem("August"); cbo_month.addItem("September"); cbo_month.addItem("October"); cbo_month.addItem("November"); cbo_month.addItem("December"); top_panel.add(lbl_month); top_panel.add(cbo_month); top_panel.add(new JLabel("")); top_panel.add(new JLabel("")); top_panel.add(lbl_year); top_panel.add(txt_year); top_panel.add(new JLabel("")); top_panel.add(btn_display); table = new JTable(day, header); pane = new JScrollPane(table); bot_panel.add(pane); btn_display.addActionListener(this); frame.pack(); frame.add(top_panel, BorderLayout.CENTER); frame.add(bot_panel, BorderLayout.SOUTH); frame.setSize(500,350); frame.setLayout(new FlowLayout()); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public void actionPerformed(ActionEvent e) { year = Integer.parseInt(txt_year.getText()); month = cbo_month.getSelectedIndex(); if(e.getSource()==btn_display) { MyCalendar pass = new MyCalendar(month, year); int i = 0; int row = 0; int col = 0; for(;i<pass.firstDayMonth(month,year);i++) { day[0][i] = ""; col = i; } for(int j=1;(i<42 && j<=pass.monthDays(month,year));i++,j++) { if((i%7)==0) { row++; col = 0; } day[row][col]=j; col++; } //table = new JTable(day,header); //JScrollPane pane = new J //ScrollPane(table); //bot_panel.add(pane); } } public static void main(String[]args) { new Calendar(); } }//end of class [/code]
and this is the class which i use to call the methods in my gui, and this program of mine its seem running correctly in a console type not in a gui type.
[CODE]public class MyCalendar { private final int newYear1901=2;//Jan. 1, 1901, Tue(2) private int month; private int year; public MyCalendar(int month, int year) { this.month=month; this.year=year; } public MyCalendar() { this(1,2010); } public int newYearDay(int year) { int elapseYear=year-1901; int countLeapYear=(elapseYear)/4; return(newYear1901+elapseYear+countLeapYear)%7; } public boolean isLeapYear(int year) { return (year%4)==0; } public int monthDays(int month, int year) { int days=-1; switch(month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: days=31;break; case 4: case 6: case 9: case 11: days=30;break; case 2: days=(isLeapYear(year))?29:28; } return days; } public int firstDayMonth(int month, int year) { int newYear=newYearDay(year); int totalDays=0; for(int i=1;i<month;i++) { totalDays+=monthDays(i,year); } return(newYear+totalDays)%7; } public String monthName(int month) { String name=""; switch(month) { case 1:name="January"; break; case 2:name="February"; break; case 3:name="March"; break; case 4:name="April"; break; case 5:name="May"; break; case 6:name="June"; break; case 7:name="July"; break; case 8:name="August"; break; case 9:name="September"; break; case 10:name="Ocotber"; break; case 11:name="November"; break; case 12:name="December"; break; } return name; } public void displayChart() { String[]dayNames={"SUN","MON","TUE","WED","THU","FRI","SAT"}; System.out.printf("\n\n%s\t%d\n", monthName(month),year); System.out.println("----------------------------------------------------"); for(String d:dayNames) System.out.print(d+"\t"); System.out.println("\n----------------------------------------------------"); int i=0; for(;i<firstDayMonth(month,year);i++) System.out.print("\t"); for(int j=1;(i<42 && j<=monthDays(month,year));i++,j++) { if((i%7)==0) System.out.printf("\n%3d\t",j); else System.out.printf("%3d\t",j); } System.out.println("\n----------------------------------------------------"); } public static void main(String[]args) { try { System.out.print("YEAR: "); int year=new java.util.Scanner(System.in).nextInt(); System.out.print("MONTH: "); int month=new java.util.Scanner(System.in).nextInt(); MyCalendar m= new MyCalendar(month,year); m.displayChart(); } catch(Exception e) { System.out.println("INVALID INPUT..Enter Number"); System.out.print("YEAR: "); int year=new java.util.Scanner(System.in).nextInt(); System.out.print("MONTH: "); int month=new java.util.Scanner(System.in).nextInt(); MyCalendar m= new MyCalendar(month,year); m.displayChart(); } } }//end of class[/CODE]
need help, thanks in advance guys