Code
scroller.setModel(textField.getHorizontalVisibility());
//...
class EmptyThumbHandler extends ComponentAdapter implements DocumentListener {
private final BoundedRangeModel emptyThumbModel
= new DefaultBoundedRangeModel(0, 1, 0, 1);
private final JTextField textField;
private final JScrollBar scroller;
public EmptyThumbHandler(JTextField textField, JScrollBar scroller) {
super();
this.textField = textField;
this.scroller = scroller;
}
private void changeThumbModel() {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
BoundedRangeModel m = textField.getHorizontalVisibility();
int iv = m.getMaximum() - m.getMinimum() - m.getExtent() - 1; //-1:bug?
if (iv <= 0) {
scroller.setModel(emptyThumbModel);
} else {
scroller.setModel(textField.getHorizontalVisibility());
}
}
});
}
@Override public void componentResized(ComponentEvent e) {
changeThumbModel();
}
@Override public void insertUpdate(DocumentEvent e) {
changeThumbModel();
}
@Override public void removeUpdate(DocumentEvent e) {
changeThumbModel();
}
@Override public void changedUpdate(DocumentEvent e) {
changeThumbModel();
}
}
References