Google Tag Manager

2022/04/30

Disable the arrow button when the knob position of the JScrollBar is on the boundary

Code

// UIManager.put("ScrollBar.alwaysShowThumb", Boolean.TRUE);
JScrollPane scroll = new JScrollPane(new JTable(24, 3));
scroll.getVerticalScrollBar().addAdjustmentListener(e -> {
  JScrollBar scrollBar = (JScrollBar) e.getAdjustable();
  BoundedRangeModel m = scrollBar.getModel();
  int value = m.getValue();
  boolean max = value == m.getMaximum() - m.getExtent();
  Optional.ofNullable(scrollBar.getComponent(0))
      .ifPresent(b -> b.setEnabled(!max));
  boolean min = value == m.getMinimum();
  Optional.ofNullable(scrollBar.getComponent(1))
      .ifPresent(b -> b.setEnabled(!min));
});

References

No comments:

Post a Comment