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 2 of 2

Thread: What's wrong with my code??? I have been struggling for 2 days !!!!

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What's wrong with my code??? I have been struggling for 2 days !!!!

    Hi guys, I'm working on a java swing calendar and I want to set different colour for current(today) date, here is what I have tried so far and its seems to be fine:

     
    import java.awt.*;
    import java.awt.event.*;
    import java.text.SimpleDateFormat;
    import java.util.*;
     
    import javax.swing.*;
     
    public class DatePicker {
    	int month = java.util.Calendar.getInstance().get(java.util.Calendar.MONTH);
    	int year = java.util.Calendar.getInstance().get(java.util.Calendar.YEAR);;
    	JLabel l = new JLabel("", JLabel.CENTER);
    	String day = "";
    	JDialog d;
    	JButton[] button = new JButton[49];
     
    	/**
    	 * Create a calendar to select a date.
    	 * 
    	 */
    	public DatePicker(JFrame parent) {
    		d = new JDialog();
    		d.setModal(true);
    		String[] header = { "Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat" };
    		JPanel p1 = new JPanel(new GridLayout(7, 7));
    		p1.setPreferredSize(new Dimension(430, 120));
     
    		for (int x = 0; x < button.length; x++) {
    			final int selection = x;
    			button[x] = new JButton();
    			button[x].setFocusPainted(false);
    			button[x].setBackground(Color.white);
    			if (x > 6) {
    				button[x].addActionListener(new ActionListener() {
    					public void actionPerformed(ActionEvent ae) {
    						day = button[selection].getActionCommand();
    						d.dispose();
    					}
    				});
     
    			}
    			if (x < 7) {
    				button[x].setText(header[x]);
    				button[x].setForeground(Color.red);
    			}
    			p1.add(button[x]);
     
    		}
     
    		JPanel p2 = new JPanel(new GridLayout(1, 3));
    		JButton previous = new JButton("<< Previous");
    		previous.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent ae) {
    				month--;
    				displayDate();
     
    			}
    		});
    		p2.add(previous);
    		p2.add(l);
    		JButton next = new JButton("Next >>");
    		next.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent ae) {
    				month++;
    				displayDate();
    			}
    		});
    		p2.add(next);
    		d.add(p1, BorderLayout.CENTER);
    		d.add(p2, BorderLayout.SOUTH);
    		d.pack();
    		d.setLocationRelativeTo(parent);
    		displayDate();
    		d.setVisible(true);
    	}
     
    	/**
    	 * Display the dates
    	 * 
    	 * @return void
    	 */
    	public void displayDate() {
    		for (int x = 7; x < button.length; x++)
    			button[x].setText("");
    		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
    				"MMMM yyyy");
    		java.util.Calendar cal = java.util.Calendar.getInstance();
    		cal.set(year, month, 1);
    		int dayOfWeek = cal.get(java.util.Calendar.DAY_OF_WEEK);
    		int daysInMonth = cal.getActualMaximum(java.util.Calendar.DAY_OF_MONTH);
    		for (int x = 6 + dayOfWeek, day = 1; day <= daysInMonth; x++, day++) {
    			button[x].setText("" + day);
    			l.setText(sdf.format(cal.getTime()));
    			d.setTitle("Date Picker");
    			String str =button[x].getText();
    			SimpleDateFormat format = new SimpleDateFormat("dd");
    			String timenow = format.format(new Date());
    			SimpleDateFormat df = new SimpleDateFormat("MMMM yyyy");
    			String month = df.format(new Date());
    			System.out.println(str);
    			System.out.println(l.getText());
     
    			if(l.getText().equals(month) &&(str.equals(timenow))) button[x].setForeground(Color.RED);
     
    		}
    	}
     
    	/**
    	 * Set the selected date to the plan area.
    	 * 
    	 * @return a String of date and time
    	 */
     
    	public String setPickedDate() {
    		if (day.equals(""))
    			return day;
    		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
    				"  dd/MM/yyyy   hh:mm");
    		java.util.Calendar cal = java.util.Calendar.getInstance();
    		cal.set(year, month, Integer.parseInt(day));
    		return sdf.format(cal.getTime());
    	}
    }

    The code I used to set the colour is in the displayDate() method. It worked for this month as the date today(21 January) is set to red. However went I go to next month the 18th of February is red as well and I have absolutely no idea why is that. I have been struggle for so long and I decided to post it here and hope some of you can find what I have done wrong.
    Any comment is appreciated.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,937
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: What's wrong with my code??? I have been struggling for 2 days !!!!

    Do you reset the coloring as you change months?

Similar Threads

  1. Struggling with if and if else
    By spongyballs in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 21st, 2011, 10:09 PM
  2. Struggling with Java :o
    By Jabdennel in forum Java Theory & Questions
    Replies: 1
    Last Post: May 30th, 2011, 10:08 PM
  3. 12 days of xmas??? not showing
    By chonch in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 9th, 2011, 09:45 PM
  4. Hi from UK, Struggling with java!
    By Sneak in forum Member Introductions
    Replies: 6
    Last Post: December 14th, 2009, 09:39 AM
  5. Replies: 3
    Last Post: June 26th, 2009, 12:06 PM