Java AWT AdjustmentListener handle adjustment event

Description

Java AWT AdjustmentListener handle adjustment event

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

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.SwingConstants;

public class Main extends JFrame implements AdjustmentListener {
   private JScrollBar bar = new JScrollBar(SwingConstants.HORIZONTAL, 50, 10, 0, 100);

   public Main() {
      setSize(350, 100);/*from  w  w w .j av  a2s  .  c  om*/
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      bar.addAdjustmentListener(this);
      JPanel pane = new JPanel();
      pane.setLayout(new BorderLayout());
      pane.add(bar, "South");
      setContentPane(pane);
   }

   public void adjustmentValueChanged(AdjustmentEvent evt) {
      Object source = evt.getSource();
      int newValue = bar.getValue();
      System.out.println(newValue);
      repaint();
   }

   public static void main(String[] arguments) {
      JFrame frame = new Main();
      frame.setVisible(true);
   }

}



PreviousNext

Related