Results 1 to 5 of 5
  1. #1
    cuti is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default date limit Java calendar GUI

    Hi guys,
    I'm creating a calendar in java GUI for a task manage program as follow:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    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");
    			[COLOR="#FF0000"]for (int y = 7; y < button.length; y++) {
    				String str = button[x].getText();
    				int m = Integer.parseInt(str);
    				SimpleDateFormat fm = new SimpleDateFormat("MMMM yyyy");
    				String dfm = fm.format(new Date());
    				SimpleDateFormat format = new SimpleDateFormat("dd");
    				String date = format.format(new Date());
    
    				int n = Integer.parseInt(date);
    				if ((m < n) && (dfm.equals(l.getText())))
    					button[x].setEnabled(false);
    
    			}[/COLOR]
    		}
    	}
    
    	/**
    	 * Set the selected date to the Task Time 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:ss");
    		java.util.Calendar cal = java.util.Calendar.getInstance();
    		cal.set(year, month, Integer.parseInt(day));
    		return sdf.format(cal.getTime());
    	}
    }
    I want to restrict the date that user can select, which means they cannot seclect date before today. The code in red is the part I did in order to achieve it. However, when i run the code with the main app, it disabled the same dates in every month. How can I improve it?????
    Thank you very much and sorry about my English its not very good.
    Last edited by Norm; 01-20-2012 at 11:53 PM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default Re: date limit Java calendar GUI

    Do you have code for testing the above?

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default Re: date limit Java calendar GUI

    Please use code tags when posting code.

    What is the code you posted supposed to do?
    How/where is the Cal class's constructor called?
    Last edited by Norm; 01-21-2012 at 12:07 AM.

  4. #4
    cuti is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default Re: date limit Java calendar GUI

    Quote Originally Posted by Norm View Post
    Do you have code for testing the above?
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.EmptyBorder;
    
    
    public class cal extends JFrame {
    
    	private JPanel contentPane;
    
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		 
    		 JLabel label = new JLabel("Selected Date:");
             final JTextField text = new JTextField(20);
             JButton b = new JButton("popup");
             JPanel p = new JPanel();
             p.add(label);
             p.add(text);
             p.add(b);
             
             final JFrame f = new JFrame();
             f.getContentPane().add(p);
             f.pack();
             f.setVisible(true);
             b.addActionListener(new ActionListener() {
                     public void actionPerformed(ActionEvent ae) {
                             text.setText(new DatePicker(f).setPickedDate());
                     }
             });
    	}
    
    	/**
    	 * Create the frame to select a date.
    	 */
    	public cal() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 450, 300);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		contentPane.setLayout(new BorderLayout(0, 0));
    		setContentPane(contentPane);
    	}
    
    }
    Here you go thanks for ur attention
    Last edited by cuti; 01-21-2012 at 12:10 AM.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default Re: date limit Java calendar GUI

    Add some printlns in the code where the decision is made to disable a button. Print out all the values used there.
    The printout should show you what the problem is.

Similar Threads

  1. Help with date compare in calendar
    By aksis in forum New To Java
    Replies: 1
    Last Post: 12-01-2011, 09:48 PM
  2. Manually setting a date using Calendar and Date
    By ravl13 in forum New To Java
    Replies: 3
    Last Post: 10-26-2011, 12:12 AM
  3. Creating a Gregorian Calendar using a Date object gives date - 1
    By prachi_goliwadekar in forum New To Java
    Replies: 1
    Last Post: 05-08-2008, 09:32 PM
  4. java Date and Calendar
    By valoyivd in forum New To Java
    Replies: 1
    Last Post: 03-30-2008, 07:49 PM
  5. Calendar.DATE
    By mew in forum New To Java
    Replies: 1
    Last Post: 01-04-2008, 08:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •