943,931 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 447
  • Java RSS
Nov 3rd, 2009
0

How do I change my repeated code to pass arguments?

Expand Post »
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.
Java Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jko2326 is offline Offline
19 posts
since Jul 2009
Nov 3rd, 2009
0
Re: How do I change my repeated code to pass arguments?
You could define a method like
Java Syntax (Toggle Plain Text)
  1. private void enableLength(boolean enable){
  2. txtLength.setEnabled( enable );
  3. lblLength.setEnabled( enable );
  4. }
so your if() code becomes
Java Syntax (Toggle Plain Text)
  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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Nov 3rd, 2009
0
Re: How do I change my repeated code to pass arguments?
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.
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Nov 3rd, 2009
0
Re: How do I change my repeated code to pass arguments?
Click to Expand / Collapse  Quote originally posted by Ezzaral ...
You could define a method like
Java Syntax (Toggle Plain Text)
  1. private void enableLength(boolean enable){
  2. txtLength.setEnabled( enable );
  3. lblLength.setEnabled( enable );
  4. }
so your if() code becomes
Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jko2326 is offline Offline
19 posts
since Jul 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Hibernate HBM with many <class> tags
Next Thread in Java Forum Timeline: HELP with code





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC