Introduction to ScrollBar in Java
The ScrollBar is a java class. It is used to create a horizontal & vertical scrollbar in the window frame. By default created scrollbar remains vertical. Scrollbar added to the top-level container. It is the class like a component in the AWT package.
The scrollbar is a component of GUI. It is used to display countless numbers of hidden rows & columns in the window frame. Scrolling of the Scrollbar takes place in a range of integer values. Users can move the scroll box to show hidden rows & columns on the window frame. Users can also click on either side of the scrollbar corners to move to the corresponding direction content.
ScrollBar Class Declaration
The ScrollBar is a class of java awt library which extends components class of awt.
Syntax:
public class Scrollbar extends Component implements Adjustable, Accessible
Scrollbar class provides a certain constructor. These constructors define the initial state of the generated Scrollbar. The following given details describe the Scrollbar constructors.
- Scrollbar(): This will generate a vertical Scrollbar.
- Scrollbar(int orientation): This will generate Scrollbar with the specified value of orientation.
- Scrollbar(int orientation, int value, int visible, int min, int max): Constructor Scrollbar will accept the following parameters & generates the scrollbar with the specified orientation, value, visibility & specified min, max values.
Types of ScrollBar in Java
The scrollbar in the window frame can be created by using different classes as per the need. Scrolling in the window frame can be achieved through the following ways.
- Scrollbar
- ScrollPane
- Scrolling an Image
1. Scrollbar
The scrollbar can be created in the window frame horizontally & vertically both. Scrollbar provides multiple methods for setting up the page size, range of scrollbar in min & max integer value. Scrollbar doesn’t have any area to display its value.
Scrollbar constructor creates a scrollbar with the direction of orientation & initially value will be the same as passed in the parameter. The visible (integer) parameter in the constructor will set the size of the slider. Scrollbar value ranges in between the min & max value. By default value for the scrolling is 1 while by default value for paging amount is 10.
2. ScrollPane
ScrollPane is also a java class for generating Scroll in window frames. ScrollPane is also known as the container which has a built-in Scrollbar. Scrolling is handled by ScrollPane peers therefore scrolling is very fast. In the case of ScrollPane, once a user performs a scrolling event, the peer sends the events to the java program for further execution related to the scrolling change.
Syntax:
public ScrollPane (int scrollbarDisplayPolicy)
For controlling the Scrollbar display, ScrollPane also contains the three constant display policy. Constant is also self-explanatory means it explains itself.
1. The following given constant is the default one which will generate the scrollbar when it is needed & component is too large.
public static final int SCROLLBARS_AS_NEEDED
2. The below given constant will generate both the Scrollbar either it is required or not.
public static final int SCROLLBARS_ ALWAYS
3. Scrollpane will not generate the Scrollbar even if the component is too large.
public static final int SCROLLBARS_ NEVER
In Example #2, we can see how scrolling is generated using the ScrollPane class.
3. Scrolling an Image
Scroll of Image in a window frame can also be achieved in multiple ways. If the image size is too large for the viewport then scrolling will be required to see the complete image. To handle this type of use case some of the java libraries provide certain classes. Here in the example, the JScrollpane class is used to generate the scrolling for the image which is larger than the viewport size.
In the given example #3, the JScrollPane class is used to achieve the scrolling if the size of the image is larger than the viewport.
Examples of ScrollBar in Java
Following are the different examples of ScrollBar in Java.
Example #1
The below-given program will describe how to create a horizontal & vertical scrollbar.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ScrollBarDemo extends JPanel {
JLabel jLabelH;
JLabel jLabelV;
public ScrollBarDemo() {
super(true);
jLabelH = new JLabel();
jLabelV = new JLabel();
setLayout(new BorderLayout());
JScrollBar horizonBar = new JScrollBar( JScrollBar.HORIZONTAL, 60, 40, 0, 400);
JScrollBar verticalBar = new JScrollBar( JScrollBar.VERTICAL, 60, 60, 0, 150);
horizonBar.setUnitIncrement(2);
horizonBar.setBlockIncrement(1);
horizonBar.addAdjustmentListener(new HorizontalAdjustmentListener());
verticalBar.addAdjustmentListener(new VerticalAdjustmentListener());
add(horizonBar, BorderLayout.SOUTH);
add(verticalBar, BorderLayout.EAST);
add(jLabelH, BorderLayout.WEST);
add(jLabelV, BorderLayout.CENTER);
}
class HorizontalAdjustmentListener implements AdjustmentListener {
public void adjustmentValueChanged(AdjustmentEvent e) {
jLabelH.setText(" Horizontal scrolled value " + e.getValue());
repaint();
}
}
class VerticalAdjustmentListener implements AdjustmentListener {
public void adjustmentValueChanged(AdjustmentEvent e) {
jLabelV.setText(" | Vertical scrolled value " + e.getValue());
repaint();
}
}
public static void main(String s[]) {
JFrame jFrame = new JFrame("Horizontal & Vertical Scroll Bar Example");
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setContentPane(new ScrollBarDemo());
jFrame.setSize(500, 150);
jFrame.setVisible(true);
}
}
Output:
The above-given program can be run through the following way as given in the below screenshot.
The above screenshot shows how to program is first compiled & then executed to see the desired result as given in the following screenshot.
In the above-given screenshot, horizontal & vertical scrollbar are generated under the specified window frame. Also, values shown in the scrollbar show how much area of the screen has been scrolled horizontally & vertically. Given arrow in the horizontal scrollbar used to move the page scroll left & right while arrow given in the vertical scrollbar used to move the page scroll up & down. scrolling can also be done by selecting the square highlighted area in the horizontal & vertical scrollbar.
Example #2
In this Example, ScrollPane is used to generate Scrollbar.
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class ScrollPaneExample {
private static void displayScollingGUI() {
final JFrame jFrame = new JFrame("Scroll Pane Example");
jFrame.getContentPane().setLayout(new FlowLayout());
JTextArea textArea = new JTextArea(10, 10);
JScrollPane scrollingArea = new JScrollPane(textArea);
scrollingArea.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollingArea.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jFrame.getContentPane().add(scrollingArea);
jFrame.setSize(250, 250);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
displayScollingGUI();
}
});
}
}
Output:
The above screenshot shows how the program is first compiled & then executed to see the desired result as given in the following screenshot.
In the below-given screenshot, we can see the output of the above-given program.
Example #3
In this Example, the JScrollPane swing class is used to generate Scrolling in the image container.
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
public class ScrollImageExample extends JFrame{
public ScrollImageExample() {
super("JScrollPane Example");
ImageIcon imgIcon = new ImageIcon("electronics-1.jpg");
JScrollPane jScrollPane = new JScrollPane(new JLabel(imgIcon));
getContentPane().add(jScrollPane);
setSize(500, 450);
setVisible(true);
}
//Main Method to start the execution
public static void main(String[] args) {
new ScrollImageExample();
}
}
Output:
In the below-given example, we can see the compilation & execution of the above-given program.
In the below-given screenshot, we can see the output of the above-given program
Conclusion
The scrollbar is a component to work with GUI. It enables users to load a lot of the content on the window frame in the hidden rows & columns which can be visible inside the frame by scrolling the scrollbar up & down. The scrollbar can be added to the window frame in certain ways as given in the type of Scrollbar.
Recommended Articles
This is a guide to ScrollBar in Java. Here we discuss the scrollbar class declaration in java along with the top 3 types and examples. You may also look at the following articles to learn more –