setPreferredSize()

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:

sliderV = new JSlider( JSlider.VERTICAL,  
    0, 1000, 400);
sliderV.setMajorTickSpacing( 100 );
sliderV.setMinorTickSpacing(  50 );
sliderV.setPaintTicks ( true );
sliderV.setPaintLabels( true ); 

setPreferredSize()

Vertical Slider

The layout managers have control over the size of components, but often do their job better if you indicate a preferred size. This is done with

setPreferredSize( new Dimension( int width, int height ) )

If this is not used, the graphical representation of a slider often has its labels and tick marks crowded together.

QUESTION 7:

Suggest that this slider have a width of 50 and a height of 300:

sliderV.setPreferredSize( new Dimension(  , ) );