import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

/**
    a GUI application demoing a JScrollBar

    @author Sharon Tuttle
    @version 2021-11-14 (corrected/improved comment describing the
                         arguments of the 5-argument JScrollBar constructor)
*/

public class JScrollBarDemo
{
    /**
        creates a simple frame to demo a JScrollBar
            
        @param args not used here
    */

    public static void main(String[] args)
    {
        EventQueue.invokeLater(
            () ->
               {
                   JScrollBarDemoFrame mainFrame = new JScrollBarDemoFrame();
                   mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                   mainFrame.setVisible(true);
               } );
    }
}

/**
    A frame that will demo more Java Swing GUI components
*/

class JScrollBarDemoFrame extends JFrame
{
    // data fields

    private static final int DEFAULT_WIDTH = 400;
    private static final int DEFAULT_HEIGHT = 200;
    
    /**
        construct a JScrollBarDemoFrame instance
    */

    public JScrollBarDemoFrame()
    {
        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
        setTitle("JScrollBar Demo");

        JScrollBarDemoPanel mainPanel = new JScrollBarDemoPanel();
        add(mainPanel);
    }
}

/**
    A panel including a JScrollBar
*/

class JScrollBarDemoPanel extends JPanel
{
    // data fields!

    private static final Font DISPLAY_FONT =
        new Font("SanSerif", Font.PLAIN, 30);

    private JScrollBar numChoiceBar;
    private static final int NUM_MAX = 100;
    private int latestChoice;

    private JTextField numChoiceDisplay;

    /**
        constructs a JScrollBarDemoPanel instance
    */

    public JScrollBarDemoPanel()
    {
        setLayout(new BorderLayout());

        // let's add a number-choice scrollbar-panel to the top of
        //     our panel
                
        // make it horizontal, initially with the bar to
        //    the far right with an initial value of NUM_MAX,
        //    with an extent -- how much it "jumps" when you click
        //        on either side of the scrollbar bar -- of 1,
        //    a minimum value of 0, and
        //    a maximum value of NUM_MAX+1 (so 1-unit-extent bar's
        //    "left" end can have the desired value NUM_MAX)
        
        numChoiceBar = new JScrollBar(JScrollBar.HORIZONTAL,
                                 NUM_MAX, 1, 0, (NUM_MAX+1));

        AdjustmentListener adjustNumber =
            event ->
            {
                latestChoice = numChoiceBar.getValue();

                numChoiceDisplay.setText("Number chosen: " +
                      latestChoice);
            };

        numChoiceBar.addAdjustmentListener(adjustNumber);
        
        add(numChoiceBar, BorderLayout.NORTH);
        
        // make a textfield to show the current number choice
        
        latestChoice = numChoiceBar.getValue();
        
        numChoiceDisplay = new JTextField(
            "Number chosen starts at: " + latestChoice);
        numChoiceDisplay.setEditable(false);
        add(numChoiceDisplay, BorderLayout.SOUTH);
    }
}