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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

newbie to Java; How Do I branch to different code based on drop down

Status
Not open for further replies.

DougP

MIS
Dec 13, 1999
5,985
US
I have a drop down box and want to do differnt things based on what is choosen

being a VB guy I would use select case
Select case form.combo.value or text
Case 1
print on screen "you choose" & form.combo.value
Case 2
print on screen "you choose" & form.combo.value
Case 3
print on screen "you choose" & form.combo.value
end select

TIA

DougP, MCP, A+
 
Are you asking how to write a switch statement ?
By "drop down box", which class do you mean ?

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Doug, let me expand :

switch in Java :

Code:
int  value = 1;  // hardcode for example
switch (value) {
  case 0 :
    doSomething();
    break;
  case 1 :
    doAnotherThing();
    break;
  default :
    nothingWorked();
    break;
}

I imagine that the value from the combo box (not that good on Swing, but am thinking that that the value from a combo box is an int, ie index of the choice), so I guess you would extract the value from the Swing component, and switch on that.

If its not an int, and its an object of some type (String or something) then switch won't work, as you cannot switch on non-primitive data types, so you would have to use an if/else if/else block for that.

HTH

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
YES that’s it
Now how can I have textboxes only show when a particular option is chosen

Like
If you choose option 1 then
Show Last name, first name
If option 2 then show city and state

These are just samples mind you
I don’t have the particulars for each option just yet


DougP, MCP, A+
 
It depends on what you want the panel to look like. You could use the setVisible method to hide the fields you don't want. Or use the setEnabled method to disable them, but still leave the fields visible.

Or if the different cases have vastly different associated entry fields, use a JPanel for each group of entry fields and swap these panels in and out depending ... or better still, use the CardLayout to stack these panels and show the relevent panel at any time.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top