I have a class datePicker which works fine, but I want to disable the past date in it. Thanks
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.
I have a class datePicker which works fine, but I want to disable the past date in it. Thanks
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.
Here is my codeThankspackage 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()); } }
Last edited by Norm; March 25th, 2013 at 05:57 AM. Reason: Fixed code tags
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.
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
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.