AdjustmentListener example

In this tutorial we are going to see how AdjustmentListener works in Java. It is quite useful when you want to monitor a very wide variety of changes in a program with rich GUI components.

For example if you bundle an AdjustmentListener with a scroll pane every time a value or a property of that component changes, the corresponding event will be handled.

In order to work with an AdjustmentListener one should follow these steps:

  • Create a new AdjustmentListener instance.
  • Override the methods that correspond to the events that you want to monitor about the components e.g, adjustmentValueChanged and customize as you wish the handling of the respective events. Now every time one of these events occurs, the corresponding method will be executed.
  • Use addAdjustmentListener to add the AdjustmentListener to a specific component.

Let’s take a look at this code snippets:

package com.javacodegeeks.snippets.desktop;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;

public class Adjustment {

    public static void main(String args[]) {

  JFrame jFrame = new JFrame();

  Container cPane = jFrame.getContentPane();

  JButton jButton = new JButton();

  JScrollPane scrollPane = new JScrollPane(jButton);

  AdjustmentListener adjListener = new AdjustmentListener() {


public void adjustmentValueChanged(AdjustmentEvent event) {

    System.out.println("Horizontal: ");




  JScrollBar hscrollBar = scrollPane.getHorizontalScrollBar();


  AdjustmentListener vListener = new AdjustmentListener() {


public void adjustmentValueChanged(AdjustmentEvent event) {

    System.out.println("Vertical: ");




  JScrollBar vscrollBar = scrollPane.getVerticalScrollBar();


  cPane.add(scrollPane, BorderLayout.CENTER);

  jFrame.setSize(500, 600);


    private static void dumpInfo(AdjustmentEvent e) {

  System.out.println("     Value: " + e.getValue());

  String type = null;

  switch (e.getAdjustmentType()) {

case AdjustmentEvent.TRACK:

    type = "Track";


case AdjustmentEvent.BLOCK_DECREMENT:

    type = "Block Decrement";


case AdjustmentEvent.BLOCK_INCREMENT:

    type = "Block Increment";


case AdjustmentEvent.UNIT_DECREMENT:

    type = "Unit Decrement";


case AdjustmentEvent.UNIT_INCREMENT:

    type = "Unit Increment";



  System.out.println("    Type: " + type);

This was an example on how to work with AdjustmentListener in Java.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button