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

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.

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.