By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,841 Members | 840 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,841 IT Pros & Developers. It's quick & easy.

JComboBox help

P: 51
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.addItem("Please pick a route");
routesCmb.addItem(route);
routesCmb.addItem(route2);
timesCmb = new JComboBox();
timesCmb.addItem("Please pick a time");
if routesCmb.SelectedItem(route);
{
timesCmb.addItem("3.00")
times.addITem("5.00")
}

[/code]
Mar 9 '08 #1
Share this Question
Share on Google+
2 Replies


100+
P: 539
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.addItem("Please pick a route");
routesCmb.addItem(route);
routesCmb.addItem(route2);
timesCmb = new JComboBox();
timesCmb.addItem("Please pick a time");
if routesCmb.SelectedItem(route);
{
timesCmb.addItem("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
getSelectedIndex() - return's an element, depends on current
selected item... nth...


Update us,
Sukatoa
Mar 9 '08 #2

BigDaddyLH
Expert 100+
P: 1,216
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?

http://java.sun.com/docs/books/tutor...nts/index.html
Mar 10 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.