0

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.

public void itemStateChanged(ItemEvent choice)
	{
		if (choice.getStateChange() == ItemEvent.SELECTED)
		{
				if (optBox.isSelected()==true)
				{
					txtLength.setEnabled(true);
					txtWidth.setEnabled(true);
					txtHeight.setEnabled(true);
					txtRadius.setEnabled(false);
					lblLength.setEnabled(true);
					lblWidth.setEnabled(true);
					lblHeight.setEnabled(true);
					lblRadius.setEnabled(false);
				}
				else if (optCylinder.isSelected()==true)
				{
					txtLength.setEnabled(false);
					txtWidth.setEnabled(false);
					txtHeight.setEnabled(true);
					txtRadius.setEnabled(true);
					lblLength.setEnabled(false);
					lblWidth.setEnabled(false);
					lblHeight.setEnabled(true);
					lblRadius.setEnabled(true);
				}
				else if (optCone.isSelected()==true)
				{
					txtLength.setEnabled(false);
					txtWidth.setEnabled(false);
					txtHeight.setEnabled(true);
					txtRadius.setEnabled(true);
					lblLength.setEnabled(false);
					lblWidth.setEnabled(false);
					lblHeight.setEnabled(true);
					lblRadius.setEnabled(true);
				}
				else if (optSphere.isSelected()==true)
				{
					txtLength.setEnabled(false);
					txtWidth.setEnabled(false);
					txtHeight.setEnabled(false);
					txtRadius.setEnabled(true);
					lblLength.setEnabled(false);
					lblWidth.setEnabled(false);
					lblHeight.setEnabled(false);
					lblRadius.setEnabled(true);
				}
		}

	}//End ItemStateChanged
3
Contributors
3
Replies
5
Views
7 Years
Discussion Span
Last Post by jko2326
0

You could define a method like

private void enableLength(boolean enable){
   txtLength.setEnabled( enable );
   lblLength.setEnabled( enable );
}

so your if() code becomes

if (optBox.isSelected() ){
  enableLength(true);
}

That would be a little clearer to follow and a bit less repetition. You would have one method for each of your dimensions.

0

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.

0

You could define a method like

private void enableLength(boolean enable){
   txtLength.setEnabled( enable );
   lblLength.setEnabled( enable );
}

so your if() code becomes

if (optBox.isSelected() ){
  enableLength(true);
}

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.