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: Disable past dates in datepicker

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Location
    Nairobi
    Posts
    5
    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
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 Posts

    Default Re: Disable past dates in datepicker

    Post moved from Member Introduction section.

    Can you post the code you are having problems with?

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Location
    Nairobi
    Posts
    5
    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 06:57 AM. Reason: Fixed code tags

  4. #4
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 Posts

    Default Re: Disable past dates in datepicker

    Where is the main() method so the code can be executed for testing?

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Location
    Nairobi
    Posts
    5
    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
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 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.

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

    Default Re: Disable past dates in datepicker

    I have the form that has the main method[[code]]
    package milimani;

    /**
    *
    * @author develop
    */
    import javax.swing.*;
    import java.awt.*;
    import java.sql.Connection;
    import java.sql.Statement;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.awt.event.*;
    import java.util.Date;
    import java.util.Calendar;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;

    public class Issuance {
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    JTextField librarianidtext,memberidtext,bookcodetext ;
    JLabel librarianid,memberid,bookcode,issuedate,returndate ,issuedatelbl;
    JButton button,button1,button2;
    JFormattedTextField returndatetext;
    Container c;
    JFrame f;
    ResultSet rs;

    Issuance() {
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Calendar cal = Calendar.getInstance();

    bookcodetext=new JTextField(15);
    memberidtext=new JTextField(30);
    librarianidtext=new JTextField(30);
    returndatetext=new JFormattedTextField();

    bookcode=new JLabel("Book Code");
    librarianid=new JLabel("Librarian ID");
    memberid=new JLabel("Member NationalID");
    issuedate=new JLabel("Issue Date");
    returndate=new JLabel("Return Date");
    issuedatelbl= new JLabel(dateFormat.format(cal.getTime()));



    button=new JButton("Save");
    button1=new JButton("Reset");
    button2=new JButton();
    button2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/calender.png")));



    f=new JFrame();
    f.getContentPane().setLayout(null);
    bookcode.setBounds(100,100,200,30);
    issuedate.setBounds(100,150,200,30);
    returndate.setBounds(100,200,200,30);
    memberid.setBounds(100,250,200,30);
    librarianid.setBounds(100, 300, 200, 30);

    bookcodetext.setBounds(210,100,300,30);
    issuedatelbl.setBounds(210,150,300,30);
    returndatetext.setBounds(210,200,300, 30);
    memberidtext.setBounds(210,250,300,30);
    librarianidtext.setBounds(210,300,300,30);

    button.setBounds(310,600,250,30);
    button1.setBounds(100,600,250,30);
    button2.setBounds(530,200,30,30);

    f.add(bookcode);
    f.add(bookcodetext);
    f.add(issuedate);
    f.add(issuedatelbl);
    f.add(returndate);
    f.add(returndatetext);
    f.add(memberid);
    f.add(memberidtext);
    f.add(librarianid);
    f.add(librarianidtext);
    f.add(button);
    f.add(button1);
    f.add(button2);
    DateListener datepic = new DateListener();
    button2.addActionListener(datepic);
    button2.setToolTipText("Please click to get date");
    ResetListener reset = new ResetListener();
    button1.addActionListener(reset);
    ButtonListener Listener = new ButtonListener();
    button.addActionListener(Listener);
    //add(f);
    f.setSize(1024,768);
    f.setVisible(true);
    }

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

    }
    }

    private class ResetListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
    bookcodetext.setText("");
    memberidtext.setText("");
    librarianidtext.setText("");

    }
    }

    private class ButtonListener implements ActionListener{

    public void actionPerformed(ActionEvent ae){
    String value1=bookcodetext.getText();
    String value2=memberidtext.getText();
    String value3=librarianidtext.getText();
    String value4=issuedatelbl.getText();
    String value5=returndatetext.getText();


    try{
    String host = "jdbc:mysql://localhost:3306/library";
    String uname = "root";
    String pass = "";
    Connection con = DriverManager.getConnection(host, uname, pass);
    Statement st = con.createStatement();
    String sql ="insert into book(lib_id,member_ID,issue_date,return_date,book_ code)"
    + " values('"+value3+"','"+value2+"','"+value4+"','"+v alue5+"','"+value1+"')";

    st.execute(sql);
    JOptionPane.showMessageDialog(null, "Data is successfully inserted");
    }
    catch(SQLException e){
    System.out.println(e.getMessage());
    }
    }
    }

    public static void main (String[]args){
    Issuance issue = new Issuance();
    }
    }









    [[code]]

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

    Default Re: Disable past dates in datepicker

    I have the main method in the form. [[code]]
    package milimani;

    /**
    *
    * @author develop
    */
    import javax.swing.*;
    import java.awt.*;
    import java.sql.Connection;
    import java.sql.Statement;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.awt.event.*;
    import java.util.Date;
    import java.util.Calendar;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;

    public class Issuance {
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    JTextField librarianidtext,memberidtext,bookcodetext ;
    JLabel librarianid,memberid,bookcode,issuedate,returndate ,issuedatelbl;
    JButton button,button1,button2;
    JFormattedTextField returndatetext;
    Container c;
    JFrame f;
    ResultSet rs;

    Issuance() {
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Calendar cal = Calendar.getInstance();

    bookcodetext=new JTextField(15);
    memberidtext=new JTextField(30);
    librarianidtext=new JTextField(30);
    returndatetext=new JFormattedTextField();

    bookcode=new JLabel("Book Code");
    librarianid=new JLabel("Librarian ID");
    memberid=new JLabel("Member NationalID");
    issuedate=new JLabel("Issue Date");
    returndate=new JLabel("Return Date");
    issuedatelbl= new JLabel(dateFormat.format(cal.getTime()));



    button=new JButton("Save");
    button1=new JButton("Reset");
    button2=new JButton();
    button2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/calender.png")));



    f=new JFrame();
    f.getContentPane().setLayout(null);
    bookcode.setBounds(100,100,200,30);
    issuedate.setBounds(100,150,200,30);
    returndate.setBounds(100,200,200,30);
    memberid.setBounds(100,250,200,30);
    librarianid.setBounds(100, 300, 200, 30);

    bookcodetext.setBounds(210,100,300,30);
    issuedatelbl.setBounds(210,150,300,30);
    returndatetext.setBounds(210,200,300, 30);
    memberidtext.setBounds(210,250,300,30);
    librarianidtext.setBounds(210,300,300,30);

    button.setBounds(310,600,250,30);
    button1.setBounds(100,600,250,30);
    button2.setBounds(530,200,30,30);

    f.add(bookcode);
    f.add(bookcodetext);
    f.add(issuedate);
    f.add(issuedatelbl);
    f.add(returndate);
    f.add(returndatetext);
    f.add(memberid);
    f.add(memberidtext);
    f.add(librarianid);
    f.add(librarianidtext);
    f.add(button);
    f.add(button1);
    f.add(button2);
    DateListener datepic = new DateListener();
    button2.addActionListener(datepic);
    button2.setToolTipText("Please click to get date");
    ResetListener reset = new ResetListener();
    button1.addActionListener(reset);
    ButtonListener Listener = new ButtonListener();
    button.addActionListener(Listener);
    //add(f);
    f.setSize(1024,768);
    f.setVisible(true);
    }

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

    }
    }

    private class ResetListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
    bookcodetext.setText("");
    memberidtext.setText("");
    librarianidtext.setText("");

    }
    }

    private class ButtonListener implements ActionListener{

    public void actionPerformed(ActionEvent ae){
    String value1=bookcodetext.getText();
    String value2=memberidtext.getText();
    String value3=librarianidtext.getText();
    String value4=issuedatelbl.getText();
    String value5=returndatetext.getText();


    try{
    String host = "jdbc:mysql://localhost:3306/library";
    String uname = "root";
    String pass = "";
    Connection con = DriverManager.getConnection(host, uname, pass);
    Statement st = con.createStatement();
    String sql ="insert into book(lib_id,member_ID,issue_date,return_date,book_ code)"
    + " values('"+value3+"','"+value2+"','"+value4+"','"+v alue5+"','"+value1+"')";

    st.execute(sql);
    JOptionPane.showMessageDialog(null, "Data is successfully inserted");
    }
    catch(SQLException e){
    System.out.println(e.getMessage());
    }
    }
    }

    public static void main (String[]args){
    Issuance issue = new Issuance();
    }
    }







    [[code]]

  9. #9
    Junior Member
    Join Date
    Mar 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Disable past dates in datepicker

    Nh�̣n Lắp mạng FPT tại nhà mi�̃n phí, 0985088983,Tại Ph�́ H�ng Giầy
    Nh�̣n Lắp mạng FPT tại nhà mi�̃n phí, 0985088983,Tại Ph�́ H�ng Gi�**
    PH?N M?M K� KHAI THU? QUA M?NG, CH?NG TH? S?, CH? K� S?, CA, FPT-CA, VNPT-CA, VIETTEL-CA, NACENCOMM-CA, BKAV-CA, CK, H? TH?NG KHAI THU? QUA M?NG, CH?NG TH?C CH? K� S?, N?P THU? QUA M?NG, H??NG D?N K�

Similar Threads

  1. Button disable
    By deependeroracle in forum AWT / Java Swing
    Replies: 6
    Last Post: April 22nd, 2012, 10:06 AM
  2. disable a button ?
    By dime111 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 28th, 2011, 03: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, 05:10 PM