JScrollbar user problem (handle / thumb change)

I want to change the appearance of the JScrollBar.
I do this with a rewrite / extension of ScrollBarUI.
It is not a problem to change the appearance of the arrows by overwriting createIncreaseButton and createDecreaseButton . I change the width of the track by rewriting the paintThumb and paintTrack Methods.

Now it looks like <----o----> (very thin track and oval finger / pen).
Problem:
The handle cannot move to the very end:
What it looks like: <---o------>
What it should look like: <---------o>

I know this because I made the oval non-stretching (the original rectangle is stretched with a width).
I totally don’t understand how to change the thumb motion calculations so that it can move to the end.

I would be very grateful for the help.

Here is the code:

 public class TestScrollBarMain extends JFrame { public TestScrollBarMain() { setDefaultCloseOperation(DISPOSE_ON_CLOSE); JPanel p = new JPanel(); p.setPreferredSize(new Dimension(500, 500)); JScrollPane s = new JScrollPane(p); MyScrollBar b = new MyScrollBar(); s.setVerticalScrollBar(b); getContentPane().add(s); setSize(100, 100); setVisible(true); } public static void main(String[] args) { new TestScrollBarMain(); } public class MyScrollBarUI extends BasicScrollBarUI { @Override protected void paintThumb(final Graphics g, final JComponent c, final Rectangle thumbBounds) { if (thumbBounds.isEmpty() || !this.scrollbar.isEnabled()) { return; } g.translate(thumbBounds.x, thumbBounds.y); g.setColor(this.thumbDarkShadowColor); g.drawOval(2, 0, 14, 14); g.setColor(this.thumbColor); g.fillOval(2, 0, 14, 14); g.setColor(this.thumbHighlightColor); g.setColor(this.thumbLightShadowColor); g.translate(-thumbBounds.x, -thumbBounds.y); } @Override protected void paintTrack(final Graphics g, final JComponent c, final Rectangle trackBounds) { g.setColor(Color.black); g.fillRect(trackBounds.width / 2, trackBounds.y, 3, trackBounds.height); if (this.trackHighlight == BasicScrollBarUI.DECREASE_HIGHLIGHT) { this.paintDecreaseHighlight(g); } else if (this.trackHighlight == BasicScrollBarUI.INCREASE_HIGHLIGHT) { this.paintIncreaseHighlight(g); } } } public class MyScrollBar extends JScrollBar { MyScrollBar() { super(); setUI(new MyScrollBarUI()); } } } 
+1
source share
1 answer

Include this in your MyScrollBarUI code:

  protected void setThumbBounds(int x, int y,int width,int height) { super.setThumbBounds(x, y, 14, 14); } protected Rectangle getThumbBounds() { return new Rectangle(super.getThumbBounds().x,super.getThumbBounds().y,14,14); } protected Dimension getMinimumThumbSize() { return new Dimension(14,14); } protected Dimension getMaximumThumbSize() { return new Dimension(14,14); } 
+1
source

Source: https://habr.com/ru/post/1432300/


All Articles