How to create a JComboBox with Dates?

Hi... I need to create a JComboBox with Date elements...For example: from "Jan-01-2007" to "Mar-02-2007"Jan-01-2007Jan-02-2007Jan-03-2007............Mar-01-2007Mar-02-2007Anyone know how to do it?Regards...
[290 byte] By [MetalTuxa] at [2007-11-26 20:18:27]
# 1
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?
camickra at 2007-7-10 0:42:06 > top of Java-index,Desktop,Core GUI APIs...
# 2
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...
MetalTuxa at 2007-7-10 0:42:06 > top of Java-index,Desktop,Core GUI APIs...
# 3

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];

}

Michael_Dunna at 2007-7-10 0:42:06 > top of Java-index,Desktop,Core GUI APIs...
# 4
Thanks for your mini example...Anything, I post it...Regards... and Thanks again...
MetalTuxa at 2007-7-10 0:42:06 > top of Java-index,Desktop,Core GUI APIs...
# 5

//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));

}

}

henyoa at 2007-7-10 0:42:06 > top of Java-index,Desktop,Core GUI APIs...
# 6
Thanks to you too...I really appreciate your time...
MetalTuxa at 2007-7-10 0:42:07 > top of Java-index,Desktop,Core GUI APIs...
# 7

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);

}

}

hiwaa at 2007-7-10 0:42:07 > top of Java-index,Desktop,Core GUI APIs...
# 8
Thanks all of you...I do it... Regards...
MetalTuxa at 2007-7-10 0:42:07 > top of Java-index,Desktop,Core GUI APIs...