Home » Desktop Java » awt » event » AdjustmentListener example

About Ilias Tsagklis

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

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() {

@Override

public void adjustmentValueChanged(AdjustmentEvent event) {

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

    dumpInfo(event);

}

  };

  JScrollBar hscrollBar = scrollPane.getHorizontalScrollBar();

  hscrollBar.addAdjustmentListener(adjListener);

  AdjustmentListener vListener = new AdjustmentListener() {

@Override

public void adjustmentValueChanged(AdjustmentEvent event) {

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

    dumpInfo(event);

}

  };

  JScrollBar vscrollBar = scrollPane.getVerticalScrollBar();

  vscrollBar.addAdjustmentListener(vListener);

  cPane.add(scrollPane, BorderLayout.CENTER);

  jFrame.setSize(500, 600);

  jFrame.setVisible(true);
    }

    private static void dumpInfo(AdjustmentEvent e) {

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

  String type = null;

  switch (e.getAdjustmentType()) {

case AdjustmentEvent.TRACK:

    type = "Track";

    break;

case AdjustmentEvent.BLOCK_DECREMENT:

    type = "Block Decrement";

    break;

case AdjustmentEvent.BLOCK_INCREMENT:

    type = "Block Increment";

    break;

case AdjustmentEvent.UNIT_DECREMENT:

    type = "Unit Decrement";

    break;

case AdjustmentEvent.UNIT_INCREMENT:

    type = "Unit Increment";

    break;

  }

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

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

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of