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

Thread: Disable past dates in datepicker

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Location
    Nairobi
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Disable past dates in datepicker

    I have a class datePicker which works fine, but I want to disable the past date in it. Thanks


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Disable past dates in datepicker

    Post moved from Member Introduction section.

    Can you post the code you are having problems with?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Location
    Nairobi
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Disable past dates in datepicker

    Here is my code
     
     
    package milimani;
     
     
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.Date;
    import java.text.DateFormat;
     
     
    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];
     
     
            public DatePicker() {
                    //par = new JFrame();
                    java.util.Calendar cal = java.util.Calendar.getInstance();
                    java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
                                    "MMMM yyyy");
                    Date today = cal.getTime();
                    d = new JDialog();
                    d.setModal(true);
                    String[] header = { "Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat" };
                    JPanel p1 = new javax.swing.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.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/back.png")));
                    previous.addActionListener(new ActionListener() {
                            public void actionPerformed(ActionEvent ae) {
                                    /*month ;
                                    displayDate();*/
                            }
                    });
                    p2.add(previous);
                    p2.add(l);
                    JButton next = new JButton();
                    next.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/next.png")));
                    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(par);
                    displayDate();
                    d.setVisible(true);
            }
     
     
            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()));
                    Date date = new Date();
                    Date today = cal.getTime();
                    if (date.before(today) == true) {
                       button[x].disable();
                     }
                    int minDate = cal.getActualMinimum(java.util.Calendar.DAY_OF_MONTH);
                     //date = sdf.format(cal.getTime());
                    //today.equals(minDate);              
                    //td.equals(dt);
                    d.setTitle("Calender");
     
            }
     
            public String setPickedDate() {
                    if (day.equals(""))
                            return day;
                    java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
                                    "yyyy-MM-dd");
                    java.util.Calendar cal = java.util.Calendar.getInstance();
                    cal.set(year, month, Integer.parseInt(day));
                    return sdf.format(cal.getTime());
            }
           }
    Thanks
    Last edited by Norm; March 25th, 2013 at 05:57 AM. Reason: Fixed code tags

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Disable past dates in datepicker

    Where is the main() method so the code can be executed for testing?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Location
    Nairobi
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Disable past dates in datepicker

    I actually created a actionlistener for a button to display the datePicker class.[[code]]

    private class DateListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
    returndatetext.setText(new DatePicker().setPickedDate());

    }
    }
    [[code]]
    thanks

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Disable past dates in datepicker

    Do you have code with a main() method for testing the class?

    What is the [[code]] for?


    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Button disable
    By deependeroracle in forum AWT / Java Swing
    Replies: 6
    Last Post: April 22nd, 2012, 09:06 AM
  2. disable a button ?
    By dime111 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 28th, 2011, 02:58 PM
  3. how to disable forward and backward buttons using js
    By Raneesh in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: February 3rd, 2011, 04:40 AM
  4. Insert master or gloabal java script to disable globally onclick
    By karnesb in forum Java Theory & Questions
    Replies: 2
    Last Post: October 12th, 2008, 04:10 PM