event

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.

Ilias Tsagklis

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

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

0 Comments
Inline Feedbacks
View all comments
Back to top button