- Another option is to use
scroller.setModel(textField.getHorizontalVisibility());
- The advantage of this method is that the
JScrollBar
thumb scroll while selecting text(or the right arrow key).
- the disadvantages of this method is that the
JScrollBar
thumb is always show if all text removed.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
public class HorizontalVisibilityTest {
private static final boolean DEBUG = false;
private static final String TEXT = "javascript:(function(){var l=location,"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa=0;}());";
public JComponent makeUI() {
Box box = Box.createVerticalBox();
JScrollPane scroll = new JScrollPane(
ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.setViewportView(new JTextField(TEXT));
box.add(new JLabel("JScrollPane + VERTICAL_SCROLLBAR_NEVER"));
box.add(scroll);
box.add(Box.createVerticalStrut(15));
final JTextField textField = new JTextField(TEXT);
JScrollBar scroller = new JScrollBar(JScrollBar.HORIZONTAL);
scroller.setModel(textField.getHorizontalVisibility());
if (DEBUG) { //I'm not sure if this is a bug...
EmptyThumbHandler handler = new EmptyThumbHandler(textField, scroller);
textField.addComponentListener(handler);
textField.getDocument().addDocumentListener(handler);
}
box.add(new JLabel("BoundedRangeModel: textField.getHorizontalVisibility()"));
box.add(textField, BorderLayout.SOUTH);
box.add(Box.createVerticalStrut(5));
box.add(scroller);
JPanel p = new JPanel(new BorderLayout());
p.add(box, BorderLayout.NORTH);
p.setBorder(BorderFactory.createEmptyBorder(20, 5, 5, 5));
return p;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new HorizontalVisibilityTest().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
// 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 i = m.getMaximum() - m.getMinimum() - m.getExtent() - 1; // -1: ?????
// System.out.println(i);
// if (i <= 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();
// }
// }