Listing 12.1  JScrollBar (TJScrollBar.java)
/*
 * <Applet code = TJScrollBar.class width = 400 height = 200>
 * </Applet>
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TJScrollBar extends JApplet {
    // 1. Create references to two scrollbars.
    JScrollBar horizSBar = null;
    JScrollBar vertiSBar = null;
    // 2. Create the other necessary text fields and a panel.
    JPanel panel1 = null; JPanel panel2 = null;
    JTextField tf = null;
    JTextField tf1 = null; JTextField tf2 = null;
    JTextField tf3 = null; JTextField tf4 = null;
    JTextField tf5 = null; JTextField tf6 = null;

    public void init() {
        // 3. Get a handle to the applet's container.
        Container c = this.getContentPane();
        c.setBackground(Color.lightGray);

        // 4. Create horizontal and vertical scrollbar objects.
        horizSBar = new JScrollBar(JScrollBar.HORIZONTAL,
                                   20, 60, 0, 100);
        horizSBar.setBlockIncrement(50);
        ScrollBarListener hsbListener = new ScrollBarListener();
        horizSBar.addAdjustmentListener(hsbListener);

        vertiSBar = new JScrollBar();
        vertiSBar.setOrientation(JScrollBar.VERTICAL);
        vertiSBar.setValue(10);
        vertiSBar.setVisibleAmount(30);
        vertiSBar.setMinimum(0);
        vertiSBar.setMaximum(50);
        ScrollBarListener vsbListener = new ScrollBarListener();
        vertiSBar.addAdjustmentListener(vsbListener);


        // 5. Create a panel object panel1.
        JPanel panel1 = new JPanel();
        panel1.setLayout(new GridLayout(1, 2));
        // 6. Create a label and text field objects.
        // and fix them to panel1
        JLabel label = new JLabel("ScrollBar selected:",
                                   JLabel.CENTER);
        tf =  new JTextField();
        panel1.add(label); panel1.add(tf);

        // 7. Create a panel object panel2.
        JPanel panel2 = new JPanel();
        GridLayout gridLayout = new GridLayout(6, 2);
        gridLayout.setHgap(20);
        panel2.setLayout(gridLayout);

        // 8. Create the following labels and text fields, and
        // fix them to the panel2.
        JLabel label1 = new JLabel("Current Value:", JLabel.RIGHT);
        JLabel label2 = new JLabel("Visible Extent:", JLabel.RIGHT);
        JLabel label3 = new JLabel("Minimum Value:", JLabel.RIGHT);
        JLabel label4 = new JLabel("Maximum Value:", JLabel.RIGHT);
        JLabel label5 = new JLabel("Unit Increment:", JLabel.RIGHT);
        JLabel label6 = new JLabel("Block Increment:", JLabel.RIGHT);
        tf1 = new JTextField();
        tf2 = new JTextField();
        tf3 = new JTextField();
        tf4 = new JTextField();
        tf5 = new JTextField();
        tf6 = new JTextField();
        panel2.add(label1); panel2.add(tf1);
        panel2.add(label2); panel2.add(tf2);
        panel2.add(label3); panel2.add(tf3);
        panel2.add(label4); panel2.add(tf4);
        panel2.add(label5); panel2.add(tf5);
        panel2.add(label6); panel2.add(tf6);

        // 9. Set the border layout for the applet's content pane
        // and add the panels and scrollbars as give next.
        BorderLayout borderLayout = new BorderLayout();
        borderLayout.setHgap(10);
        borderLayout.setVgap(20);
        c.setLayout(borderLayout);
        c.add("North", panel1);
        c.add("Center", panel2);
        c.add("South", horizSBar);
        c.add("East", vertiSBar);
}

// 10. A listener class that handle the scrollbar adjustment events
class ScrollBarListener implements AdjustmentListener {
        public void adjustmentValueChanged(AdjustmentEvent evt) {
            JScrollBar sBar = (JScrollBar) evt.getSource();

            if (sBar.getOrientation() == 0) {
                tf.setText("HORIZONTAL");
              tf1.setText(Integer.toString(sBar.getValue()));
                tf2.setText(Integer.toString(sBar.getVisibleAmount()));
                tf3.setText(Integer.toString(sBar.getMinimum()));
                tf4.setText(Integer.toString(sBar.getMaximum()));
                tf5.setText(Integer.toString(sBar.getUnitIncrement()));
                tf6.setText(Integer.toString(sBar.getBlockIncrement()));
            }
            else if (sBar.getOrientation() == 1) {
                tf.setText("VERTICAL");
                tf1.setText(Integer.toString(sBar.getValue()));
                tf2.setText(Integer.toString(sBar.getVisibleAmount()));
                tf3.setText(Integer.toString(sBar.getMinimum()));
                tf4.setText(Integer.toString(sBar.getMaximum()));
                tf5.setText(Integer.toString(sBar.getUnitIncrement()));
                tf6.setText(Integer.toString(sBar.getBlockIncrement()));
            }
        }
    }
}

Listing 12.2  JScrollPane (TJScrollPane.java)
/*
 * <Applet code = TJScrollPane.class width = 300 height = 200>
 * </Applet>
 */
import javax.swing.*;
import java.awt.*;

public class TJScrollPane extends JApplet {
    // 1. Create a scroll pane object and the other
    // necessary objects.
    JScrollPane scrollPane = null;
    JLabel label = null;  // Not a canvas for JScrollPane!
    JPanel panel = null;  // supports double buffering
    Icon icon = null;

    public void init() {
        // 2. Get a handle on the JApplet's container.
        Container container = getContentPane();
        container.setLayout(new GridLayout(1,1));

        // 3. Create a Swing label and a panel for double buffering.
        icon = new ImageIcon("saravan.gif");
        label = new JLabel(icon);
        panel = new JPanel();
        panel.add(label);

        // 4. Create a scroll pane and add the panel to it.
        scrollPane = new JScrollPane(panel,
                     JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                     JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        // 5. Add the scroll pane to the contentpane of JApplet.
        container.add(scrollPane);
    }
}

Listing 12.3  Jslider Audio Control Panel (TJSlider.java)
// Demonstrates the Swing slider widget...
/*
 * <Applet code = TJSlider.class width = 400 height = 200>
 * </Applet>
 */
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class TJSlider extends JApplet {
    // 1. Declare the required JSlider references.
    JSlider slider = null;
    JSlider slider1 = null;
    JSlider slider2 = null;
    JSlider slider3 = null;
    JSlider slider4 = null;
    JSlider slider5 = null;

    // 2. Other necessary fields
    JTextField textField = null;
    JTextField textField1 = null;
    JTextField textField2 = null;
    GridBagLayout gridbag = null;
    GridBagConstraints c = null;
    Container container = null;
    boolean buttonPressed = false;

    public void init() {
        // 3. Get a handle on the container of JApplet
        // and assign the color and layout model.
        container = this.getContentPane();
        container.setBackground(Color.lightGray);
        gridbag = new GridBagLayout();
        container.setLayout(gridbag);

        // 4. Constraints for the layout
        c = new GridBagConstraints();
        c.weightx = 1.0; c.weighty = 1.0;
        c.fill = GridBagConstraints.NONE;
        c.insets = new Insets(4, 4, 4, 4);

        // 5. Label showing �Equalizer (x 100Hz)
        c.gridx = 0; c.gridy = 0;
        c.gridwidth = 1; c.gridheight = 1;
        c.ipadx = 150;
        Font font = new Font("Helvetica", Font.BOLD, 14);
        JLabel label = new JLabel("Equalizer (X 100Hz)",
                                  JLabel.CENTER);
        label.setFont(font);
        gridbag.setConstraints(label, c);
        container.add(label);

        // 6. Create horizontal slider1.
        c.gridy = 1;
        setSlider(JSlider.HORIZONTAL, true,
                  0, 20, 5, 5, 1);
        slider3 = slider;
        slider3.setLabelTable(slider3.createStandardLabels(5));
        slider3.setPaintLabels(true);

        // 7. Create horizontal slider2.
        c.gridy = 2;
        setSlider(JSlider.HORIZONTAL, true,
                  20, 100, 60, 10, 5);
        slider4 = slider;
        slider4.setLabelTable(slider4.createStandardLabels(10));
        slider4.setPaintLabels(true);

        // 8. Create horizontal slider3.
        c.gridy = 3;
        setSlider(JSlider.HORIZONTAL, true,
                  100, 200, 150, 20, 10);
        slider5 = slider;
        slider5.setLabelTable(slider5.createStandardLabels(20));
        slider5.setPaintLabels(true);

        // 9. Create the toggle button for the volume balance.
        c.ipadx = 0;
        c.gridx = 1; c.gridy = 4;
        c.gridwidth = 2; c.gridheight = 1;
        setButton("L|R Balance");

        // 10. Create volume slider1.
        c.ipady = 75;
        c.gridy = 1;
        c.gridwidth = 1; c.gridheight = 3;
        setSlider(JSlider.VERTICAL, false, 0, 10, 8);
        slider1 = slider;

        // 11. Create volume slider2.
        c.gridx = 2; c.gridy = 1;
        setSlider(JSlider.VERTICAL, false, 0, 10, 8);
        slider2 = slider;

        // 12. Create textfield1 for the volume slider1.
        c.ipadx = 0; c.ipady = 0;
        c.gridx = 1; c.gridy = 0;
        c.gridwidth = 1; c.gridheight = 1;
        setTextField(slider1);
        textField1 = textField;

        // 13. Create textfield2 for the volume slider2.
        c.gridx = 2;
        setTextField(slider2);
        textField2 = textField;
}
    // 14. Creates a slider object.
    public void setSlider(int orientation,
                          boolean paintTicks,
                          int minimumValue, int maximumValue,
                          int initValue) {
        setSlider(orientation, paintTicks,
                  minimumValue, maximumValue, initValue, 0, 0);
    }

    // 15. Overload the previous above.
    public void setSlider(int orientation,
                          boolean paintTicks,
                          int minimumValue, int maximumValue,
                          int initValue,
                          int majorTickSpacing,
                          int minorTickSpacing) {
        slider = new JSlider(orientation,
                             minimumValue, maximumValue,
                             initValue);
        slider.addChangeListener(new SliderListener());
        slider.setPaintTicks(paintTicks);
        slider.setMajorTickSpacing(majorTickSpacing);
        slider.setMinorTickSpacing(minorTickSpacing);
        gridbag.setConstraints(slider, c);
        container.add(slider);
    }

    // 16. Create the toggle button for the balance of channels.
    public void setButton(String name) {
        JToggleButton button = new JToggleButton(name);
        button.setBackground(Color.lightGray);
        gridbag.setConstraints(button, c);
        button.addActionListener(new ButtonListener());
        container.add(button);
    }

    // 17. Create text field objects.
    public void setTextField(JSlider slider) {
        textField = new JTextField(2);
        textField.setText(Integer.toString(slider.getValue()));
        gridbag.setConstraints(textField, c);
        container.add(textField);
    }

    // 18. Button listener for the channel balance
    class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent actEvt) {
            JToggleButton buttonTemp = (JToggleButton)
                                        actEvt.getSource();
            buttonPressed = buttonTemp.isSelected();
            slider2.setValue(slider1.getValue());
        }
    }

    // 19. The slider knob position change listener
    class SliderListener implements ChangeListener {
        public void stateChanged(ChangeEvent chngEvt) {
            updateTextField(slider1.getValue(),
                            slider2.getValue());

            JSlider sliderTemp = (JSlider) chngEvt.getSource();
            if(buttonPressed) {
                if(sliderTemp == slider1) {
                    slider2.setValue(slider1.getValue());
                }
                else if(sliderTemp == slider2) {
                    slider1.setValue(slider2.getValue());
                }
            }
        }
    }

    public void updateTextField(int currValue1, int currValue2) {
        textField1.setText(Integer.toString(currValue1));
        textField2.setText(Integer.toString(currValue2));
    }
}