JComboBox help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • javatech007
    New Member
    • Nov 2007
    • 51

    JComboBox help

    I am trying to use comboboxes in a ticket payment screen for train tickets. I have two combo boxes. One for picking a route and the other for picking a time. What i want is when you pick a route in the first combo box, different departure times are availble in the time combobox depending on your route choice. I am trying to use if statements but its not working.

    [JAVA = CODE]

    routesCmb = new JComboBox();
    routesCmb.addIt em("Please pick a route");
    routesCmb.addIt em(route);
    routesCmb.addIt em(route2);
    timesCmb = new JComboBox();
    timesCmb.addIte m("Please pick a time");
    if routesCmb.Selec tedItem(route);
    {
    timesCmb.addIte m("3.00")
    times.addITem(" 5.00")
    }

    [/CODE]
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #2
    Originally posted by javatech007
    I am trying to use comboboxes in a ticket payment screen for train tickets. I have two combo boxes. One for picking a route and the other for picking a time. What i want is when you pick a route in the first combo box, different departure times are availble in the time combobox depending on your route choice. I am trying to use if statements but its not working.

    [JAVA = CODE]

    routesCmb = new JComboBox();
    routesCmb.addIt em("Please pick a route");
    routesCmb.addIt em(route);
    routesCmb.addIt em(route2);
    timesCmb = new JComboBox();
    timesCmb.addIte m("Please pick a time");
    if routesCmb.Selec tedItem(route);
    {
    timesCmb.addIte m("3.00")
    times.addITem(" 5.00")
    }

    [/CODE]
    In the if statement, no selectedItem method exists, maybe

    getSelectedItem () - return's an object... Could be compared to String. Useful
    getSelectedInde x() - return's an element, depends on current
    selected item... nth...


    Update us,
    Sukatoa

    Comment

    • BigDaddyLH
      Recognized Expert Top Contributor
      • Dec 2007
      • 1216

      #3
      While there are multiple things wrong with the code you posted, the biggest mistake, I believe, is a failure to appreciate the event-driven paradigm.

      You need to write a listener that is notified when the user makes a selection from the first combo box. Your code seems to believe it can pause execute until the user does something, which is not how GUI code works.

      Why not read Sun's tutorial on Swing, especially on event handling?

      This Swing Java Tutorial describes developing graphical user interfaces (GUIs) for applications and applets using Swing components

      Comment

      Working...