Can someone help me in implementing a JDate picker without using packages, whereby will click the calender icon and the calender will appear.

and for which one

Any particular reason for doing this? - it's not easy, in fact I would rate it as a fairly advanced GUI exercise.

ps I did this for an app about 8 years ago, and just dug out the code. It's under 200 lines, but I seem to remember having a really knotty time getting the logic for populating the right days into the right cells of the month grid.

i want to biuld iot for a text field, bt canyt you helkp me with the logic in getting it done

Also guys cant jscipt be intergrated in java!!

hmmm,

better would be to check exist(s) DatePicker before, because if I stick there some excelent JavaCalendar, by 2nd. breath I respond, there are some bugs for me, many things that I have to rewrite before my 1st use

MDatePicker oldies but as I know one with implementations of Special days
SwingX oldies but with possiblilities for implementations of Special days, JCalendar by Toedter just excelent idea and workAround too, simpliest as you get

agreed with JamesCherrill, there is about usage of various Java Object and Methods altogether, but I'd never told that's complicated for any of Java Guru's as he's

but I'd never told that's complicated for any of Java Guru's

The Java isn't too difficult if you are happy with 2D arrays of JComponents, gridbag layouts, listener patterns etc - so it's OK for an experienced Java coder, but pretty hard for a newbie.

The challenge I had was getting the right days into the right rows/cols for any arbitrary month, and I wasted time on a couple of poor algorithms. As with so many problems like that, it because easy once I got a good statement of the requirement - ie that the date in the top left corner has to be the nearest Monday on or before the first day of the month.

If @cretaros can guarantee that this isn't for a marked assignment I can give you all a bit of a laugh and share my old code...

agreed

agreed :-) weekday can save me this *** nightMare:-)

agreed, but in other hand why, if I put JavaCalendar, JCalendar, Java Date Picer or Java Date Organizer (Sheduler)...., I think that it isn't humanly possible (one person) to write so much code (probably wrong, but is still around)

Edit: forgot touch about Locale and i18, but those are out of reality for this thread

hm! gud! java dosent someone whoose lazy, it want dedication and a heart willing to learn much more everyday.
bt guys why dont you share the litle u hav so we can learn from ur work so we can help others 2mmrr.

again
MDatePicker SwingX JCalendar by Toedter are in top 10 (free) for me

but you can search on google with JavaCalendar, JCalendar, Java Date Picer or Java Date Organizer (Sheduler)....


Edit

but let there be fun, big ..., endless fun JamesCherrill can you share you 8Y old JavaChild, I'll try that with all respect to your person :-) :-) :-)

@cretaros Is this for a course? Will it be marked? Or is it just something you are doing for your own interest/fun?

@JamesCherrill, its for my Major Project,(Bsc(Hons) in IT), My system is done and im Stuck there with the Date Picker only, its academically only not for industrial use.

The System im doing is the Prison Management System, so i need the Datepicker when reserving inmates for transfer dates.

OK. These are the relevant bits - you'll have to read and understand them to see how they work. Have fun:

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import static java.util.Calendar.*;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import static java.awt.GridBagConstraints.*;

public class CalendarPanel extends JPanel {

   // A simple calendar date selector for pop-up or embedded use.
   // It's a subclass of JPanel and can therefore be embedded and managed
   // in the usual ways in any Swing GUI context.
   // Date selection by the user is signalled by a callback to one or more
   // implementors of CalendarPanel.DateSelectionListener which must be
   // registered with the CalendarPanel instance via addDateSelectionListener
   //
   // Copyright 2002, 2005 (Minor updates for Java 1.5) James Cherrill. 

   public static void main(String[] args) {
      // A minimal test/demo harness.Just prints selected dates until closed.
      JFrame frame = new JFrame("Calendar demo");
      CalendarPanel calendar = new CalendarPanel();
      frame.add(calendar);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
      calendar.addDateSelectionListener(new DateSelectionListener() {

         @Override
         public void dateSelected(Date d) {
            System.out.println("Selected " + d);
         }
      });
   }

   // Public section /////////////////////////////////////////////////////////////

   public interface DateSelectionListener {

      // called when user clicks a day in the calendar...
      public void dateSelected(Date d);
   }

   public void addDateSelectionListener(DateSelectionListener listener) {
      // standard observer/listener pattern for date selection events
      listeners.add(listener);
   }

   public CalendarPanel() {
      initGraphics();
      setMonth(0);
   }

   // End of public section //////////////////////////////////////////////////////

   private static final long serialVersionUID = 1L;

   private static final String dayLetter[] = { "M", "T", "W", "T", "F", "S", "S" };

   private JLabel titleLabel = new JLabel();
   private JLabel back = new JLabel("<");
   private JLabel fwd = new JLabel(">");

   private JLabel[][] cell = new JLabel[7][7];

   private int offset = 0; // months before/after this month to display

   private final ArrayList<DateSelectionListener> listeners = new ArrayList<DateSelectionListener>();

   private void initGraphics() {

      setLayout(new GridBagLayout());
      setOpaque(false); // allow container to draw background (eg image)

      Font headingFont = new Font("Dialog", Font.BOLD, 14);
      Font dayLetterFont = new Font("Dialog", Font.BOLD, 11);
      Font cellFont = new Font("Verdana", Font.BOLD, 12);

      titleLabel.setFont(headingFont);
      titleLabel.setForeground(Color.darkGray);

      this.add(titleLabel, new GridBagConstraints(1, 0, 5, 1, 0.0, 0.0, CENTER, NONE,
            new Insets(1, 0, 1, 0), 0, 0));
      titleLabel.addMouseListener(new MouseAdapter() {

         @Override
         public void mouseClicked(MouseEvent arg0) {
            setMonth(0);
         }
      });

      this.add(back, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, CENTER, NONE,
            new Insets(1, 0, 1, 0), 0, 0));
      back.addMouseListener(new MouseAdapter() {

         @Override
         public void mouseClicked(MouseEvent arg0) {
            setMonth(-1);
         }
      });

      this.add(fwd, new GridBagConstraints(6, 0, 1, 1, 0.0, 0.0, CENTER, NONE,
            new Insets(1, 0, 1, 0), 0, 0));
      fwd.addMouseListener(new MouseAdapter() {

         @Override
         public void mouseClicked(MouseEvent arg0) {
            setMonth(+1);
         }
      });

      // M...S day letter headings
      for (int d = 0; d < 7; d++) {
         cell[d][0] = new JLabel();
         cell[d][0].setOpaque(false);
         cell[d][0].setFont(dayLetterFont);
         cell[d][0].setText(dayLetter[d]);
         cell[d][0].setForeground(Color.gray);
         this.add(cell[d][0], new GridBagConstraints(d, 1, 1, 1, 0.0, 0.0, EAST, NONE,
               new Insets(0, 11, 2, 2), 0, 0));
         // The left offset actually sets the min width of the calendar
         // so that November fits without re-sizing
      }

      // day number fields...
      DayClickListener dcl = new DayClickListener();
      for (int w = 1; w < 7; w++) {
         for (int d = 0; d < 7; d++) {
            cell[d][w] = new JLabel();
            cell[d][w].setOpaque(false);
            cell[d][w].setFont(cellFont);
            this.add(cell[d][w], new GridBagConstraints(d, w + 1, 1, 1, 0.0, 0.0, EAST,
                  NONE, new Insets(1, 2, 0, 2), 0, 0));
            cell[d][w].addMouseListener(dcl);
         }
      }

   }

   protected void setMonth(int increment) {
      // moves current month back / forward by "increment" months
      // if increment==0 sets month to today's date 
      if (increment == 0) {
         offset = 0;
      } else {
         offset += increment;
      }
      Calendar c = Calendar.getInstance(); // initialised to date/time now
      int dayNow = c.get(DAY_OF_MONTH); // 1-31
      int monthNow = c.get(MONTH); // 0-11
      int yearNow = c.get(YEAR);

      c.set(yearNow, monthNow, 1); // first day of this month
      c.add(MONTH, offset); // first day of month to display
      int monthToShow = c.get(MONTH);
      String titleText = new SimpleDateFormat("MMMM yyyy").format(c.getTime());
      titleLabel.setText(titleText);

      // display of days starts with a Monday...
      // 1st of month, if this happens to be a Monday, otherwize...
      // the Monday immediately preceeding the 1st of the month
      while (c.get(DAY_OF_WEEK) != 2)
         c.add(DATE, -1);

      // day numbers...
      for (int w = 1; w < 7; w++) {
         for (int d = 0; d < 7; d++) {

            int cellDay = c.get(DAY_OF_MONTH);
            int cellMonth = c.get(MONTH);
            int cellYear = c.get(YEAR);

            cell[d][w].setText("" + cellDay);
            cell[d][w].putClientProperty("Date", c.getTime());

            if (cellMonth != monthToShow) {
               cell[d][w].setForeground(Color.lightGray);
            } else {
               cell[d][w].setForeground(Color.black);
            }
            if (cellDay == dayNow && cellMonth == monthNow && cellYear == yearNow) {
               cell[d][w].setForeground(Color.red);
            }

            c.add(DATE, 1);
         }
      }
      invalidate();
   }

   class DayClickListener extends MouseAdapter {

      @Override
      public void mouseClicked(MouseEvent arg0) {
         JLabel source = (JLabel) arg0.getSource();
         Date clickedDate = (Date) source.getClientProperty("Date");
         for (DateSelectionListener listener : listeners) {
            listener.dateSelected(clickedDate);
         }
      }
   }

}

Thanx a lot master. im realy happy with ur work!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.