Java/Swing  Step-by-Step Guide

Tutorial 3

Slider Control Variables


GOAL: To understand that control variables can be a pre-defined class and demonstrate working with control variables
PREREQUISITES: Tutorial 2

Add Text Fields For Vertical Slider Bar
  1. Open the Palette
  2. Drag a JLabel to the JFrame window
    1. and change the text property to "Vertical Bar"
  3. Create a JLabel with
    1. Change Variable Name... to  "lbl_VSlider_Echo"
    2. text to "0"
    3. border to LineBorder
Add Vertical Slider Bar
  1. Drag a JSlider to the JFrame window, and Change Variable Name... to  "vSliderBar".  Next, inside the Properties window, change
    1. orientation to "VERTICAL"
    2. minimum to 0
    3. maximum to 100
    4. value to 0
Create Event Handler For Vertical Slider Bar
  1. Select the main application window and Left-Mouse-Button single click to select the vertical slider bar, then Right-Mouse-Button single-click on it, and then select
    Events >> Change >> stateChanged
  2. Now inside the function code block for vSliderBarStateChanged(...), add the following code:
     lbl_VSlider_Echo.setText( Integer.toString( vSliderBar.getValue() ) );
Add Text Fields For Horizontal Slider Bar
  1. Drag a JLabel to the JFrame window
    1. and change the text property to "Horizontal Bar"
  2. Create a JLabel with
    1. Change Variable Name... to  "lbl_HSlider_Echo"
    2. text to "0"
    3. border to LineBorder
Add Horizontal Slider Bar, and Event Handler
  1. Repeat the steps for Adding Vertical Slider Bar with the following changes
  2. Inside the Slider Control Properties window, change
    1. Change Variable Name... to  "hSliderBar"
    2. Keep the the orientation as "HORIZONTAL"
  3. Create an Event Handler, and inside the function code block for it, add the following code:
    lbl_HSlider_Echo.setText( Integer.toString( hSliderBar.getValue() ) );


» Written by William Frankhouser (wjf2@washington.edu)
» Adapted by Michael Panitz.
» Advised by Kelvin Sung (ksung@washington.edu) as part of the project sponsored by the National Science Foundation under Grant No. 0442420. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation.
» Produced in the "Essential Concepts for Building Interactive Computer Graphics Applications", A.K. Peters, Ltd.