getName() and getValue()

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

ChangeListener

getName() and getValue()

Assume that the slider has a name you have set using setName(). The name of the slider is a string that is stored as part of its data. To ask the slider for its name use:

String getName()

Then, to get the value of the slider use

int getValue()

The value is an int between the minimum and maximum value of the slider.

QUESTION 11:

Here is part of a program that has two sliders and a text field for each. Fill in the blanks so that when a slider changes its value the value is written it the correct text field.

JSlider    sliderA;
JTextField textA;
JSlider    sliderB;
JTextField textB;
 
public void stateChanged( ChangeEvent evt )
{
  JSlider source;
  source = (JSlider)evt.getSource();
  if ( source.().equals("sliderA") )
    textA.setText( source. + " " );
  if ( source.().equals("sliderB") )
    textB.setText( source. + " " );
}