JTextField: getScrollOffset()
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 MainClass {
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);
}
}
Related examples in the same category