Scroll Bars
Home ] Up ] [ Scroll Bars ] Scroll Panes ]

 

 

You can use a scroll bar as a slider control, specifying the range it slides between. You can orient the slide horizontally or vertically.

You use adjustment events to detect changes to the position of the scroll bar. 

For example:

package swingExamples;

import java.awt.Adjustable;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;

import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.SwingConstants;

class ColorSelectPanel extends JPanel
{
  public ColorSelectPanel()
  {
    setLayout(new BorderLayout());
    m_colorPanel = new ColorPanel();
    add(m_colorPanel, BorderLayout.CENTER);
    add(new ScrollPanel(), BorderLayout.SOUTH);
  }
  
  ////// Private data //////
  private ColorPanel m_colorPanel;
  
  ////// Inner classes /////
  class ColorPanel extends JPanel
  {
    ColorPanel()
    {
      setBorder(
          BorderFactory.createLineBorder(Color.BLACK));
    }
  }
  
  class ScrollPanel extends JPanel 
      implements AdjustmentListener
  {
    public ScrollPanel()
    {
      setLayout(new GridLayout(3, 2));
      
      add(m_redLabel);
      m_redScroll.setBackground(Color.RED);
      m_redScroll.setBlockIncrement(BLOCK_INCREMENT);
      m_redScroll.addAdjustmentListener(this);
      add(m_redScroll);
      
      add(m_greenLabel);
      m_greenScroll.setBackground(Color.GREEN);
      m_greenScroll.setBlockIncrement(BLOCK_INCREMENT);
      m_greenScroll.addAdjustmentListener(this);
      add(m_greenScroll);
      
      add(m_blueLabel);
      m_blueScroll.setBackground(Color.BLUE);
      m_blueScroll.setBlockIncrement(BLOCK_INCREMENT);
      m_blueScroll.addAdjustmentListener(this);
      add(m_blueScroll);
      
      changeColor();
    }
    
    // AdjustmentListener required methods
    public void adjustmentValueChanged(AdjustmentEvent evt)
    {
      changeColor();
    }
    
    private void changeColor()
    {
      int red = m_redScroll.getValue();
      int green = m_greenScroll.getValue();
      int blue = m_blueScroll.getValue();
      m_redLabel.setText("" + red);
      m_greenLabel.setText("" + green);
      m_blueLabel.setText("" + blue);
      m_colorPanel.setBackground(
          new Color(red, green, blue));
    }
    
    ////////////// Private Data //////////////////
    private static final int BLOCK_INCREMENT = 16;
    
    private JLabel m_redLabel = 
        new JLabel("0", SwingConstants.CENTER);
    private JLabel m_greenLabel = 
        new JLabel("0", SwingConstants.CENTER);
    private JLabel m_blueLabel = 
        new JLabel("0", SwingConstants.CENTER);
    
    private JScrollBar m_redScroll =
        new JScrollBar(Adjustable.HORIZONTAL, 0, 0, 0, 255);
    private JScrollBar m_greenScroll =
        new JScrollBar(Adjustable.HORIZONTAL, 0, 0, 0, 255);
    private JScrollBar m_blueScroll =
        new JScrollBar(Adjustable.HORIZONTAL, 0, 0, 0, 255);
  }
}

class ColorSelectFrame extends JFrame
{
  public ColorSelectFrame()
  {
    setTitle("Scrollbars");
    setSize(300, 200);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    Container contentPane = getContentPane();
    contentPane.add( new ColorSelectPanel() );
  }
}

public class Scrollbars
{
  public static void main(String[] args)
  {
    ColorSelectFrame frame = new ColorSelectFrame();
    frame.setVisible(true);
  }
}

which produces:

As each scrollbar is dragged to some new value, the background color of the top panel changes to correspond, and the labels to the left show the component value for each color.

 

This page was last modified on 02 October, 2007