How do I change my repeated code to pass arguments?

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jul 2009
Posts: 16
Reputation: jko2326 is an unknown quantity at this point 
Solved Threads: 0
jko2326 jko2326 is offline Offline
Newbie Poster

How do I change my repeated code to pass arguments?

 
0
  #1
31 Days Ago
I have my program working but, I need to cut out the repeated code. How do I change this part of my code to pass arguments to a user defined method? I'm lost. I really have no clue exactly where to start on this. Basically when a user clicks a certain radio button it enables and disables the jlabels and textfield associated with them. Please help.
  1. public void itemStateChanged(ItemEvent choice)
  2. {
  3. if (choice.getStateChange() == ItemEvent.SELECTED)
  4. {
  5. if (optBox.isSelected()==true)
  6. {
  7. txtLength.setEnabled(true);
  8. txtWidth.setEnabled(true);
  9. txtHeight.setEnabled(true);
  10. txtRadius.setEnabled(false);
  11. lblLength.setEnabled(true);
  12. lblWidth.setEnabled(true);
  13. lblHeight.setEnabled(true);
  14. lblRadius.setEnabled(false);
  15. }
  16. else if (optCylinder.isSelected()==true)
  17. {
  18. txtLength.setEnabled(false);
  19. txtWidth.setEnabled(false);
  20. txtHeight.setEnabled(true);
  21. txtRadius.setEnabled(true);
  22. lblLength.setEnabled(false);
  23. lblWidth.setEnabled(false);
  24. lblHeight.setEnabled(true);
  25. lblRadius.setEnabled(true);
  26. }
  27. else if (optCone.isSelected()==true)
  28. {
  29. txtLength.setEnabled(false);
  30. txtWidth.setEnabled(false);
  31. txtHeight.setEnabled(true);
  32. txtRadius.setEnabled(true);
  33. lblLength.setEnabled(false);
  34. lblWidth.setEnabled(false);
  35. lblHeight.setEnabled(true);
  36. lblRadius.setEnabled(true);
  37. }
  38. else if (optSphere.isSelected()==true)
  39. {
  40. txtLength.setEnabled(false);
  41. txtWidth.setEnabled(false);
  42. txtHeight.setEnabled(false);
  43. txtRadius.setEnabled(true);
  44. lblLength.setEnabled(false);
  45. lblWidth.setEnabled(false);
  46. lblHeight.setEnabled(false);
  47. lblRadius.setEnabled(true);
  48. }
  49. }
  50.  
  51. }//End ItemStateChanged
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,483
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 515
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster
 
0
  #2
31 Days Ago
You could define a method like
  1. private void enableLength(boolean enable){
  2. txtLength.setEnabled( enable );
  3. lblLength.setEnabled( enable );
  4. }
so your if() code becomes
  1. if (optBox.isSelected() ){
  2. enableLength(true);
  3. }
That would be a little clearer to follow and a bit less repetition. You would have one method for each of your dimensions.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz
 
0
  #3
31 Days Ago
Nothing to change. This form is fully acceptable.
--------
You can try CardLayout; on each tab, you can put only the necessary components labels and txtfields, and switch between tabs in accordance with the choice, but it still requires additional changes in code.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 16
Reputation: jko2326 is an unknown quantity at this point 
Solved Threads: 0
jko2326 jko2326 is offline Offline
Newbie Poster
 
0
  #4
31 Days Ago
Originally Posted by Ezzaral View Post
You could define a method like
  1. private void enableLength(boolean enable){
  2. txtLength.setEnabled( enable );
  3. lblLength.setEnabled( enable );
  4. }
so your if() code becomes
  1. if (optBox.isSelected() ){
  2. enableLength(true);
  3. }
That would be a little clearer to follow and a bit less repetition. You would have one method for each of your dimensions.
Thank you so much It helped a lot. Now I just have to apply it to all the dimensions.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC