Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Please help me.

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Please help me.

    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
    Last edited by sanz; September 22nd, 2010 at 05:32 PM.

  2. #2
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Please help me.

    Please surround your code in [highlight=java][/highlight] tags instead of [code][/code] tags. Here's a problem I noticed right away. It's not a problem with your code, I just believe that it's a bad habit.
    for(;i<pass.firstDayMonth(month,year);i++) //the first part of your for loop is empty
    //instead do:
    for(i=0;i<pass.firstDayMonth(month,year);i++)

    Why is the output not what you desired? Is it throwing an error?
    Last edited by Brt93yoda; September 22nd, 2010 at 11:25 AM.

  3. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please help me.

    actually the variable i is already declared

    [code]
    int i = 0;
    int row = 0;
    int col = 0;
     
    for(;i<pass.firstDayMonth(month,year);i++)
     
    //and if use, 
    for(i=0;i<pass.firstDayMonth(month,year);i++)
     
    //this code will be errored
     
    for(int j=1;(i<42 && j<=pass.monthDays(month,year));i++,j++)
    //it will asked for i,
    [/code]


    Nope, i just need to click into the table in order to display my output, and when it will display its an logical error. The days did match the in the week, like when you choose September in the month, and input 2010 in the year, the days in the month of september 2010, must starts at weds, but in my case it starts at tuesday.

    thanks for responding my problem, n_n

  4. #4
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Please help me.

    Quote Originally Posted by Brt93yoda View Post
    Please surround your code in [highlight=java][/highlight] tags instead of [code][/code] tags. Here's a problem I noticed right away. It's not a problem with your code, I just believe that it's a bad habit.
    for(;i<pass.firstDayMonth(month,year);i++) //the first part of your for loop is empty
    //instead do:
    for(i=0;i<pass.firstDayMonth(month,year);i++)

    Why is the output not what you desired? Is it throwing an error?
    So it's all solved?

  5. #5
    Junior Member
    Join Date
    Apr 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Please help me.

    @Brt93yoda
    an elaboration to what sanz problem...


    The problem to what sanz's code is that

    the display button works, but the days weren't in the proper place.. It skips(leaves blank) in the first Saturday..




    PS: I hope you can help us solve the problem...
    thanks

    @sanz

    In order to display the number of days,
    edit this part here: public void actionPerformed(ActionEvent e)

    and change with this code..but still it skips the first saturday

    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);
    bot_panel.add(pane);
    }
    }

    PS
    -your classmate
    Last edited by asdfg; September 22nd, 2010 at 09:22 PM.

  6. #6
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Please help me.

    Well I just put your code on Eclipse, and added asdfg's changes and a couple of my own, and it works perfectly fine for me.

    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");
     
        private int i,j,row,col;
        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);
     
        		for(i=0;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);
        		bot_panel.add(pane);
        	}
        }
     
     
        public static void main(String[] args) {
        	new Calendar();
        }
    }

  7. #7
    Junior Member
    Join Date
    Sep 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please help me.

    Quote Originally Posted by Brt93yoda View Post
    Well I just put your code on Eclipse, and added asdfg's changes and a couple of my own, and it works perfectly fine for me.

    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");
     
        private int i,j,row,col;
        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);
     
        		for(i=0;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);
        		bot_panel.add(pane);
        	}
        }
     
     
        public static void main(String[] args) {
        	new Calendar();
        }
    }

    thanks for the code, it helps me alot, but why i always came up with this output


    i really hate this, output, it will leave one blank on the first week of september 2010 or every sat in a month, Huhu, does your output is like this? please help me.
    Attached Images Attached Images

  8. #8
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Please help me.

    Now that i look at it again, my output is the same as yours. If you look at the output all of the days after the first saturday are in the right spot. So, move the first week up one day.

  9. #9
    Junior Member
    Join Date
    Sep 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please help me.

    Eh, that's my problem too, i don't know where i should place or replace the code and what code should be replace so that the days will moved at the right side, huhu, T_T