Java Swing Tutorial - Java BoundedRangeModel.getExtent()








Syntax

BoundedRangeModel.getExtent() has the following syntax.

int getExtent()

Example

In the following code shows how to use BoundedRangeModel.getExtent() method.

/*w  ww  .  j  a v a 2  s.c  o m*/
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoundedRangeModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Main {

  public static void main(final String args[]) {
    JFrame frame = new JFrame("Offset Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel(new BorderLayout());
    final JTextField textField = new JTextField();
    panel.add(textField, BorderLayout.CENTER);
    frame.add(panel, BorderLayout.NORTH);
    JButton button = new JButton("Get Offset");
    ActionListener actionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        System.out.println("Offset: " + textField.getScrollOffset());
        System.out.println("Visibility: " + textField.getHorizontalVisibility());
        BoundedRangeModel model = textField.getHorizontalVisibility();
        int extent = model.getExtent();
        textField.setScrollOffset(extent);
      }
    };
    button.addActionListener(actionListener);
    frame.add(button, BorderLayout.SOUTH);
    frame.setSize(250, 150);
    frame.setVisible(true);

  }

}