Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to select the Time slot...

Status
Not open for further replies.

rahulroy08

Programmer
Jul 3, 2007
58
0
0
US
Hi,

I'm working on a Java project, Which is to design an appointment scheduling software.

In my code,I have a TimeSlot class which gives the user the option to select the date and time.

But once the user selects a particular time slot for a particular day,then that must be removed from the list.

Can someone help me how to implement this,
Here is the code that creates a the frame for the TimeSlot,

<code>

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class TimeSlot1 extends Frame
{
Choice day,time;
Label l1,l2;
Button submit,cancel;

TimeSlot1()
{
setLayout(null);
setTitle("TimeSlot");

submit=new Button("Submit");
cancel=new Button("Cancel");
submit.setBounds(500,500,150,50);
cancel.setBounds(700,500,150,50);


l1=new Label("Day");
l2=new Label("Time");

l1.setBounds(150,200,50,25);
day=new Choice();
day.setBounds(200,200,100,25);

time=new Choice();
l2.setBounds(150,250,50,25);
time.setBounds(200,250,100,25);

add(day);
add(time);
add(submit);
add(cancel);

add(l1);
add(l2);

day.add(" ");
day.add("Monday");
day.add("Tuesday");
day.add("Wednesday");
day.add("Thursday");
day.add("Friday");

time.add(" ");
time.add("8.00 am");
time.add("8.15 am");
time.add("8.30 am");
time.add("8.45 am");
time.add("9.00 am");
time.add("9.15 am");
time.add("9.30 am");
time.add("9.45 am");
time.add("10.00 am");
time.add("10.15 am");
time.add("10.30 am");
time.add("10.45 am");
time.add("11.00 am");
time.add("11.15 am");
time.add("11.30 am");
time.add("11.45 am");
time.add("12.00 pm");
time.add("12.15 pm");
time.add("12.30 pm");
time.add("12.45 pm");
time.add("1.00 pm");
time.add("1.15 pm");
time.add("1.30 pm");
time.add("1.45 pm");
time.add("2.00 pm");
time.add("2.15 pm");
time.add("2.30 pm");
time.add("2.45 pm");
time.add("3.00 pm");
time.add("3.15 pm");
time.add("3.30 pm");
time.add("3.45 pm");
time.add("4.00 pm");
time.add("4.15 pm");

}

public static void main(String args[])
{
TimeSlot1 ts=new TimeSlot1();
Toolkit tk=Toolkit.getDefaultToolkit();
ts.setSize(tk.getScreenSize());
ts.setVisible(true);

}

}

</code>

Thanks,

Uday
 
The problem is that you have 2 choices: day and time. When the user selects a timeslot for a given day, you don't want to remove it from the other days. I think the best way to do this is to have a 2-D array so that the available time/day choices are listed in the array. Then, the choices can be built from the array. First the user chooses a day, then the time choices are built from the current state of the array. Then when the user chooses a time, that element is removed from the array.

_________________
Bob Rashkin
 
Hi Bob,

Thanks for your reply. I could understand what you said, but was unable to figure out how to implement it. Can you please send me a small piece of code how to implement it. It would be really great thanks.


Uday
 
Let's say you have an array:
static String tSlots[][];
You instantiate this array with all the time slots, where Monday - Friday correspond to first index 1-5, and second index 1-33 correspond to the time slots (8am - 4:15pm, every 15 minutes). So, for example, tSlots[4]= "8:45 am", for all i.
Now you can instantiate the "day" choice as before, but you want to add an ItemListener, whose action is to populate the "time" choice:
day.addItemListener(new ItemListener(){
int dIndex = day.getSelectedIndex();
for {int i=1; i<34; i++} {
if {tSlots[dIndex]!=null {
time.add(tSlots[dIndex];
}
}
}


You also put in an ItemListener on "time" so that when the user chooses a time slot, you capture the selected index (as above) and use it to make the corresponding entry in tSlots null.

_________________
Bob Rashkin
 
I left out the ItemStateChanged event:
day.addItemListener(new ItemListener(){
int dIndex = day.getSelectedIndex();
for {int i=1; i<34; i++} {
if {tSlots[dIndex]!=null {
time.add(tSlots[dIndex];
}
}
}
should be
Code:
day.addItemListener(new ItemListener(){
 [i]public void itemStateChanged(ItemEvent e) {[/i]
  int dIndex = day.getSelectedIndex();
  for {int i=1; i<34; i++} {
   if {tSlots[dIndex][i]!=null {
     time.add(tSlots[dIndex][i];
   }
  }
 [i]}[/i]
}

_________________
Bob Rashkin
 
Hi Bob,
Thanks for your code. I have tried it but it does not populate my time list.

Also I need some help closing the frame once I submit. My project navigates between 3 frames. But I want to close the desired frame.

Here is the code for the TimeSlot Class


<code>


class TimeSlot2 extends Frame implements ItemListener
{
Choice day,time;
Label l1,l2;

Button submit,cancel;
static String tSlots[][];

TimeSlot2()
{
setLayout(null);
setTitle("TimeSlot");

submit=new Button("Submit");
cancel=new Button("Cancel");
submit.setBounds(500,500,150,50);
cancel.setBounds(700,500,150,50);


l1=new Label("Day");
l2=new Label("Time");

l1.setBounds(150,200,50,25);
day=new Choice();
day.setBounds(200,200,100,25);



time=new Choice();
l2.setBounds(150,250,50,25);
time.setBounds(200,250,100,25);

add(day);
add(time);
add(submit);
add(cancel);

add(l1);
add(l2);

day.add(" ");
day.add("Monday");
day.add("Tuesday");
day.add("Wednesday");
day.add("Thursday");
day.add("Friday");

day.addItemListener(this);

time.add(" ");

}



public void itemStateChanged(ItemEvent e)
{
int dIndex = day.getSelectedIndex();

for(int i=1; i<34; i++)
{
if (tSlots[dIndex]!=null)
{
time.add(tSlots[dIndex]);
}
}
}






public static void main(String args[])
{
TimeSlot2 ts=new TimeSlot2();
Toolkit tk=Toolkit.getDefaultToolkit();
ts.setSize(tk.getScreenSize());
ts.setVisible(true);

}



}

</code>

When I execute it, it gives me a null pointer exception....

Please help me in this regards,

Thanks,

Uday
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top