• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using Dates in JCombo's  RSS feed

 
Melanie Walsh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am passing an array of 24 hours of the day into a JCombo box. I have formated my Date usinging the SimpleDateFormat class, in order to produce a list consisting of 24 hours. Looks like 1 M, 2 M etc, the trouble is when i pass this into a JCombo it looses the formatting and becomes a date object againg with full Date, Time etc in long format.
I am sure that it is beacuse to cast an array i have used the following Code
startTimeCombo = new JComboBox((Date[]) availTimes);

The 24 elements appear in the combo but in an unuasble format, can anyone help

Thanks
 
Melanie Walsh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry ur system seems to have changed some text into smilies ,
the results i have from using the SimpleDateFormat are in the form h:am/pm
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you required to fill in the Combo with Date objects instead of simple Strings?

--
:alex |.::the_mindstorm::.
 
Melanie Walsh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, im using two combo boxes which need to b populated with the 24 hours of a day. This is for my 3rd year degree project, i have all the back end functionalty, but part of my requirements state that a user may set a time Start and Finish. I need the time to do the rest of my program which will check for data then perform a filter between the hours set in start and finish.
As i said i have not trouble with the format of the hour until its passed into the combo.
 
Sisir Chanda
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
I am not sure why you are passing array of date object to JComboBox.If I am not wrong you are using SimpleDateFormat class to have a format of date..Correct? Then you can use SimpleDateFomate.fomat(Date date) which returns String.You accumulate those String in array and then pass the array of string to JComboBox.
 
Melanie Walsh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have tried every perutation and cannot get it working. I realise SimpleDateFormate returns a string but it i still cant seem to add to my combo.
I have tried the following code without success
<Code>
Date[] availTimes = new Date[24];
String[] comboString = new String[24];

public void setComboTimes(Date[] availTimes)
{
availTimes = fc.getViewingTimes();
for(int index =0; index < availTimes.length; index++)
{
SimpleDateFormat df = new SimpleDateFormat("h:a");
df.format(availTimes[index]);
comboString = ((Date)availTimes[index]);
startTimeCombo.add(comboString);
}
}
</Code>

I am at a total loss with this, i cannot understand why after formatting the combo changes back, this code is an addition to exsisting code, have tried paasing both a sting and date to the comboString - all i get is incompatible types

Any help gratefully recieved
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maybe it needs to be this way

SimpleDateFormat df = new SimpleDateFormat("h:a");
String comboString = df.format(availTimes[index]);
startTimeCombo.addItem(comboString);
 
Melanie Walsh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
UR a star, thank you VERY much
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!