Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

How to create a JComboBox with Dates???

843805Mar 2 2007 — edited Mar 3 2007
Hi... I need to create a JComboBox with Date elements...

For example: from "Jan-01-2007" to "Mar-02-2007"

Jan-01-2007
Jan-02-2007
Jan-03-2007
....
....
....
Mar-01-2007
Mar-02-2007

Anyone know how to do it???

Regards...

Comments

camickr
You create a combo box and use the addItem() method to add Date or Calendar objects to the combo box.

What exactly is your problem?
843805
My problem. is how I create the Dates... from Jan-01-2007 to Mar-03-2007...

Wath I have to use... java.util.Date, java.sql.Date, Calendar or GregorianCalendar???

Thats my real problem...

Sorry if I not explain so good before...

Thanks for your time... and Regards...
794342
use GregorianCalendar and set it to the first date you want
another gc set to the end date
you want to show the date in a particular format, so you'd use SimpleDateFormat()

then it's just
while(gcStart.after(gcEnd) == false)//dates inclusive
{
  combo.addItem(sdf.format(gcStart.getTime()));
  gcStart.[add a day];
}
843805
Thanks for your mini example...

Anything, I post it...

Regards... and Thanks again...
843805
//here is an example how to get the date
//I think this might help you!!!

import java.text.DateFormat;
import java.util.Date;

public class DateFormatExample1 {

    public static void main(String[] args) {
        // Make a new Date object. It will be initialized to the current time.
        Date now = new Date();

        // See what toString() returns
        System.out.println(" 1. " + now.toString());

        // Next, try the default DateFormat
        System.out.println(" 2. " + DateFormat.getInstance().format(now));

        // And the default time and date-time DateFormats
        System.out.println(" 3. " + DateFormat.getTimeInstance().format(now));
        System.out.println(" 4. " + 
            DateFormat.getDateTimeInstance().format(now));

        // Next, try the short, medium and long variants of the 
        // default time format 
        System.out.println(" 5. " + 
            DateFormat.getTimeInstance(DateFormat.SHORT).format(now));
        System.out.println(" 6. " + 
            DateFormat.getTimeInstance(DateFormat.MEDIUM).format(now));
        System.out.println(" 7. " + 
            DateFormat.getTimeInstance(DateFormat.LONG).format(now));

        // For the default date-time format, the length of both the
        // date and time elements can be specified. Here are some examples:
        System.out.println(" 8. " + DateFormat.getDateTimeInstance(
            DateFormat.SHORT, DateFormat.SHORT).format(now));
        System.out.println(" 9. " + DateFormat.getDateTimeInstance(
            DateFormat.MEDIUM, DateFormat.SHORT).format(now));
        System.out.println("10. " + DateFormat.getDateTimeInstance(
            DateFormat.LONG, DateFormat.LONG).format(now));
    }
}
843805
Thanks to you too...

I really appreciate your time...
800351
Here's a quick trial of a custom component. You can add a variety of constructors.
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.text.*;
import static java.util.Calendar.*;

public class DateComboBox extends JComboBox{
  static final Locale loc = Locale.US;

  SimpleDateFormat dateFmt;
  GregorianCalendar startDay;
  int daysSpan;
  Vector<GregorianCalendar> dvec;
  Vector<String> svec;

  public DateComboBox(GregorianCalendar start, int span, String fmtString){
    GregorianCalendar curDate;

    startDay = start;
    daysSpan = span; // days; not month, year, hour etc.
    dateFmt = new SimpleDateFormat(fmtString, loc);
    curDate = startDay;

    svec = new Vector<String>();
    dvec = new Vector<GregorianCalendar>();

    for (int i = 0; i < span; ++i){
      dvec.add(curDate);
      svec.add(dateFmt.format(curDate.getTime()));
      curDate.add(DAY_OF_MONTH, 1);
    }

    setModel(new DefaultComboBoxModel(svec));
  }

  public Vector<GregorianCalendar> getDateVector(){
    return dvec;
  }

  public GregorianCalendar[] getDateArray(){
    return (GregorianCalendar[])(dvec.toArray());
  }

  public static void main(String[] args){
    DateComboBox dcb = new DateComboBox(
        new GregorianCalendar(2007, 0, 1),
        61,
        "MMM-dd-yyyy");

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.getContentPane().add(dcb, BorderLayout.NORTH);
    frame.setSize(300, 300);
    frame.setVisible(true);
  }
}
843805
Thanks all of you...

I do it...

Regards...
1 - 8
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Mar 31 2007
Added on Mar 2 2007
8 comments
1,105 views