Java’s Swing library provides various GUI components for creating graphical user interfaces. Among these components, is the JScrollBar class, which allows users to scroll through the content of a component, such as a JScrollPane. In Java JScrollBar is a part of javax.swing package.
Constructor of JScrollBar
1. This is the default constructor that creates a horizontal scrollbar with the default values. Its default orientation is JScrollBar (HORIZONTAL).
JScrollBar()
2. This constructor creates a scrollbar with the specified orientation. The orientation parameter can be one of the following constants: JScrollBar.HORIZONTAL, JScrollBar.VERTICAL
JScrollBar(int orientation)
3. This constructor creates a scrollbar with specified orientation, value, extent, minimum and maximum.
JScrollBar(int orientation, int value, int extent, int min, int max)
Some Methods of the JScrollBar
Methods of the JScrollBar are mentioned below:
- setValue(int value) : Sets the current value of the scrollbar to a specified value.
- getValue() : Retrieves the current value of the scrollbar, which indicates the current position of the thumb.
- setMinimum(int minimum) : Sets the minimum value of the scrollbar, defining the lower boundary of the range.
- getMinimum() : Returns the minimum value, allowing you to retrieve the lower boundary of the range.
- setMaximum(int maximum) : Sets the maximum value of the scrollbar, defining the upper boundary of the range.
- getMaximum() : Retrieves the maximum value, indicating the upper boundary of the range.
Following are the programs to implement JScrollBar
1. Java Program to Implement a Vertical ScrollBar
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class VerticalScrollBarDemo {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame( "Vertical ScrollBar Demo" );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize( 400 , 300 );
// Create a panel to hold the content
JPanel contentPanel = new JPanel();
contentPanel.setLayout( new BorderLayout());
// Create a JTextArea for displaying text
JTextArea textArea = new JTextArea( 10 , 40 );
textArea.setWrapStyleWord( true );
textArea.setLineWrap( true );
// Add the text area to a JScrollPane with a vertical scrollbar
JScrollPane scrollPane = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
// Create a vertical scrollbar
JScrollBar verticalScrollBar = scrollPane.getVerticalScrollBar();
// Add a button to simulate content updates
JButton addButton = new JButton( "Add Text" );
addButton.addActionListener( new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Append text to the JTextArea
textArea.append( "JScrollBar Example.\n" );
}
});
// Add components to the content panel
contentPanel.add(scrollPane, BorderLayout.CENTER);
contentPanel.add(addButton, BorderLayout.SOUTH);
// Add the content panel to the frame
frame.add(contentPanel);
frame.setVisible( true );
});
}
} |
Output :
Another Screenshot with Printed Data
2. Java Program to Implement a Horizontal ScrollBar
Below is the implementation of the above mentioned:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class HorizontalScrollBarDemo {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame( "Horizontal ScrollBar Demo" );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize( 400 , 300 );
// Create a panel to hold the content
JPanel contentPanel = new JPanel();
contentPanel.setLayout( new BorderLayout());
// Create a JTextArea for displaying text with horizontal scrolling
JTextArea textArea = new JTextArea( 10 , 40 );
textArea.setWrapStyleWord( true );
textArea.setLineWrap( false ); // Disable line wrapping for horizontal scrolling
// Create a JScrollPane with a horizontal scrollbar
JScrollPane scrollPane = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
// Create a horizontal scrollbar
JScrollBar horizontalScrollBar = scrollPane.getHorizontalScrollBar();
// Add a button to simulate content updates
JButton addButton = new JButton( "Add Text" );
addButton.addActionListener( new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Append text horizontally to the JTextArea
textArea.append( "JScrollBar Example. " );
}
});
// Add components to the content panel
contentPanel.add(scrollPane, BorderLayout.CENTER);
contentPanel.add(addButton, BorderLayout.SOUTH);
// Add the content panel to the frame
frame.add(contentPanel);
frame.setVisible( true );
});
}
} |
Output : (Horizontal ScollBar)
Extended ScrollBar:
3. Java Program to Implement setMinimum() ,setMaximum() ,setValue() ,getValue() in JScrollBar
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JScrollBarExample {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
// Create a JFrame
JFrame frame = new JFrame( "JScrollBar Example" );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize( 400 , 300 );
// Create a JPanel to hold the content
JPanel contentPanel = new JPanel();
contentPanel.setLayout( new BorderLayout());
// Create a horizontal JScrollBar
JScrollBar horizontalScrollBar = new JScrollBar(JScrollBar.HORIZONTAL);
// Set the minimum value for the horizontal scrollbar
horizontalScrollBar.setMinimum( 10 );
// Set the maximum value for the horizontal scrollbar
horizontalScrollBar.setMaximum( 100 );
// Set the initial value for the horizontal scrollbar
horizontalScrollBar.setValue( 50 );
// Create a label to display the scrollbar value
JLabel valueLabel = new JLabel( "Value: " + horizontalScrollBar.getValue());
// Add a change listener to the horizontal scrollbar
horizontalScrollBar.addAdjustmentListener( new AdjustmentListener() {
@Override
public void adjustmentValueChanged(AdjustmentEvent e) {
int value = horizontalScrollBar.getValue();
valueLabel.setText( "Value: " + value);
}
});
// Add components to the content panel
contentPanel.add(horizontalScrollBar, BorderLayout.NORTH);
contentPanel.add(valueLabel, BorderLayout.CENTER);
// Add the content panel to the frame
frame.add(contentPanel);
// Make the frame visible
frame.setVisible( true );
});
}
} |