Following are the programs to implement JScrollBar
1. Java Program to Implement a Vertical ScrollBar
Java
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:
Java
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
Java
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 ); }); } } |
Output: (Setting Value According to ScrollBar)
Output Showing the value 90:
Java JScrollBar
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.