Copyright Derek O'Reilly, Dundalk Institute of Technology (DkIT), Dundalk, Co. Louth, Ireland.
A scroll bar provides a convenient means of allowing a user to select from an range of values. A scrollbar can have either a horizontal or a vertical orientation.
The Scrollbar constructor is of the form:
Scrollbar(int orientation, int initialValue, int width, int minimum, int maximum)
For example:
Scrollbar slider = new Scrollbar(Scrollbar. VERTICAL, 0, 1, 9, 255); add(slider);
Scrollbar Example: (Run Applet)
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ScrollbarDemo extends JApplet { @Override public void init() { this.setContentPane(new View()); } public class View extends JPanel implements AdjustmentListener { private final JScrollBar horizontalSlider = new JScrollBar(JScrollBar.HORIZONTAL, 150, // initial position 200, // scrollbar width 100, // range from 500); // range to private final JScrollBar verticalSlider = new JScrollBar(JScrollBar.VERTICAL, 0, 5, 0, 100); private final JLabel message = new JLabel(); public View() { super(); this.setLayout(new BorderLayout()); this.add("Center", this.message); this.add("North", this.horizontalSlider); this.add("East", this.verticalSlider); this.horizontalSlider.addAdjustmentListener(this); this.verticalSlider.addAdjustmentListener(this); } @Override public void adjustmentValueChanged(AdjustmentEvent e) { final Object source = e.getSource(); if (source == this.horizontalSlider) { this.message.setText("horizontal moved to " + this.horizontalSlider.getValue()); } else if (source == this.verticalSlider) { this.message.setText("vertical moved to " + this.verticalSlider.getValue()); } } } }
A ScrollBar's maximum value is equal to its maximum value, minus the cursor width. Use the code below to normalise a scrollBar, so that it produces values in the range between the minimum and maximum.
Scrollbar_Normalised_Demo Example: (Run Applet)
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Scrollbar_Normalised_Demo extends JApplet { @Override public void init() { this.setContentPane(new View()); } public class View extends JPanel implements AdjustmentListener { private final int start = 0; private final int scrollBarWidth = 10; private final int rangeStart = 0; private final int rangeEnd = 100; private final JScrollBar scrollBar = new JScrollBar(JScrollBar.HORIZONTAL, start, scrollBarWidth, rangeStart, rangeEnd); private final NormalisedScrollBar normalisedScrollBar = new NormalisedScrollBar(JScrollBar.HORIZONTAL, start, scrollBarWidth, rangeStart, rangeEnd); private final JLabel message = new JLabel(); public View() { super(); this.setLayout(new BorderLayout()); this.add("Center", this.message); this.add("North", this.scrollBar); this.add("South", this.normalisedScrollBar); this.scrollBar.addAdjustmentListener(this); this.normalisedScrollBar.addAdjustmentListener(this); } @Override public void adjustmentValueChanged(AdjustmentEvent e) { final Object source = e.getSource(); if (source == this.scrollBar) { this.message.setText("horizontal moved to " + this.scrollBar.getValue()); } else { this.message.setText("horizontal moved to " + this.normalisedScrollBar.getValue()); } } } public class NormalisedScrollBar extends JScrollBar { public NormalisedScrollBar(int orientation, int value, int visible, int minimum, int maximum) { super(orientation, value, visible, minimum, maximum); } @Override public int getValue() { // Normalise the scrollBar range to account for the width of the scrollBar // Get the width of the scrollbar cursor final int ScrollBarCursorWidth = getVisibleAmount(); final int rangeStart = getMinimum(); final int rangeEnd = getMaximum(); int normalisedValue; // shift the start point to 0 normalisedValue = super.getValue() - rangeStart; // normalise within the range of rangeStart to rangeEnd normalisedValue = normalisedValue * (rangeEnd - rangeStart) / (rangeEnd - (rangeStart + ScrollBarCursorWidth)); // move the start point back to rangeStart normalisedValue = normalisedValue + rangeStart; return normalisedValue; } } }
Copyright Derek O' Reilly, Dundalk Institute of Technology (DkIT), Dundalk, Co. Louth, Ireland.