import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ScrollbarExample implements AdjustmentListener, ActionListener{ JScrollBar scrollBar; public ScrollbarExample(){ JFrame frame = new JFrame("Demo"); Container contentPane = frame.getContentPane(); contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); scrollBar = new JScrollBar(JScrollBar.HORIZONTAL, 2, 1, 0, 20); scrollBar.setPreferredSize(new Dimension(30, 25)); scrollBar.addAdjustmentListener(this); contentPane.add(scrollBar); JButton button = new JButton("move to right"); button.setAlignmentX(Component.CENTER_ALIGNMENT); button.setPreferredSize(new Dimension(30, 20)); button.addActionListener(this); contentPane.add(button); frame.setSize(300, 100); frame.setVisible(true); } public void adjustmentValueChanged(AdjustmentEvent e){ System.out.println("current value:" + e.getValue()); } public void actionPerformed(ActionEvent e){ int currentValue = scrollBar.getValue(); currentValue++; scrollBar.setValue(currentValue); } public static void main(String args[]) { ScrollbarExample scrollbarExample = new ScrollbarExample(); } }