0

I've made my own ScrollbarUI and it works perfectly with JScrollbar. The problem is that I've got to connect this scrollbar with a JPanel to scroll it. I've already tried to use the JScrollPane but when I try to approach the vertical Scrollbar it just won'T respond. I've tried to change some easy things like the cursor, width, height, background to test if the UI is the problem but that didn't work either.

    JPanel panel_1 = new JPanel();
    JScrollPane scrollpane = new JScrollPane(panel_1);
    scrollpane.setBorder(null);
    scrollpane.getVerticalScrollBar().setUI(new MyScrollbarUI());

    JTextArea txtrLorem = new JTextArea();
    txtrLorem.setPreferredSize(new Dimension(400, 1500));
    txtrLorem.setMaximumSize(new Dimension(400, 2147483647));
    txtrLorem.setText("Lorem ipsum...");
    panel_1.add(txtrLorem);

    scrollpane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    scrollpane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    GridBagConstraints gbc_panel_1 = new GridBagConstraints();
    gbc_panel_1.gridwidth = 5;
    gbc_panel_1.insets = new Insets(0, 0, 0, 5);
    gbc_panel_1.fill = GridBagConstraints.BOTH;
    gbc_panel_1.gridx = 1;
    gbc_panel_1.gridy = 4;
    add(scrollpane, gbc_panel_1);

    JScrollBar scrollBar = new JScrollBar();
    scrollBar.setPreferredSize(new Dimension(20, 96));
    scrollBar.setMinimumSize(new Dimension(20, 5));
    scrollBar.setMaximumSize(new Dimension(20, 32767));
    scrollBar.setUI(new MyScrollbarUI());
    GridBagConstraints gbc_scrollBar = new GridBagConstraints();
    gbc_scrollBar.fill = GridBagConstraints.VERTICAL;
    gbc_scrollBar.insets = new Insets(0, 0, 0, 5);
    gbc_scrollBar.gridx = 6;
    gbc_scrollBar.gridy = 4;
    add(scrollBar, gbc_scrollBar);

the JScrollBar has the right design, but the Scrollbar of the JScrollPane hasn't. Here is the UI:

public class MyScrollbarUI extends BasicScrollBarUI {

    @Override
    protected JButton createDecreaseButton(int orientation) {
        JButton btnL = new JButton("");
        btnL.setPressedIcon(new ImageIcon(KFO_baender_bracketsetz_einzel.class.getResource("/gui/icons/top_active.png")));
        btnL.setBorderPainted(false);
        btnL.setIcon(new ImageIcon(KFO_baender_bracketsetz_einzel.class.getResource("/gui/icons/top_on.png")));
        btnL.setRolloverIcon(new ImageIcon(KFO_baender_bracketsetz_einzel.class.getResource("/gui/icons/top.png")));
        btnL.setRolloverEnabled(true);
        return btnL;
    }

    @Override
    protected JButton createIncreaseButton(int orientation) {
        JButton btnL = new JButton("");
        btnL.setPressedIcon(new ImageIcon(KFO_baender_bracketsetz_einzel.class.getResource("/gui/icons/down_active.png")));
        btnL.setBorderPainted(false);
        btnL.setIcon(new ImageIcon(KFO_baender_bracketsetz_einzel.class.getResource("/gui/icons/down_on.png")));
        btnL.setRolloverIcon(new ImageIcon(KFO_baender_bracketsetz_einzel.class.getResource("/gui/icons/down.png")));
        btnL.setRolloverEnabled(true);
        return btnL;
    }

    @Override
    protected void paintDecreaseHighlight(Graphics g)
    {
    Insets insets = scrollbar.getInsets();
    Rectangle thumbR = getThumbBounds();
    g.setColor(new Color(137,144,144));

    if (scrollbar.getOrientation() == JScrollBar.VERTICAL) {
        int x = insets.left+decrButton.getWidth()/2-2;
        int y = decrButton.getY() + decrButton.getHeight();
        int w = 4;
        int h = thumbR.y - y;
        g.fillRect(x, y, w, h);
    } 
    else    {
        int x, w;
        if (scrollbar.getComponentOrientation().isLeftToRight()) {
        x = decrButton.getX() + decrButton.getWidth();
        w = thumbR.x - x;
        } else {
        x = thumbR.x + thumbR.width;
        w = decrButton.getX() - x;
        }
        int y = insets.top;
        int h = scrollbar.getHeight() - (insets.top + insets.bottom);
        g.fillRect(x, y, w, h);
    }
    }

    @Override
    protected void paintIncreaseHighlight(Graphics g) {
            Insets insets = scrollbar.getInsets();
            Rectangle thumbR = getThumbBounds();
            g.setColor(new Color(202,207,203));

            if (scrollbar.getOrientation() == JScrollBar.VERTICAL) {
                int x = insets.left+decrButton.getWidth()/2-2;
                int y = thumbR.y;
                int w = 4;
                int h = incrButton.getY() - y;
                g.fillRect(x, y, w, h);
            }
        }


    @Override
    protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds)  
    {
        g.setColor(Color.WHITE);
        g.fillRect(trackBounds.x, trackBounds.y, trackBounds.width, trackBounds.height);
        paintDecreaseHighlight(g);
        paintIncreaseHighlight(g);

    }

  @Override
  protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds)
  {
      if(thumbBounds.isEmpty() || !scrollbar.isEnabled())     {
          return;
      }

      int w = 16;
      int h = 16;

      Graphics2D g2 = (Graphics2D) g;
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
          RenderingHints.VALUE_ANTIALIAS_ON);

      g.translate(thumbBounds.x, thumbBounds.y);

      GradientPaint gr = new GradientPaint(2, 0, new Color(158,161,162), 18, 16, new Color(96,99,98));
      g2.setPaint(gr);
      g2.fill(new Ellipse2D.Double(2, 0, w-1, h-1));

      g2.setPaint(new Color(203,207,203));
      g2.fill(new Ellipse2D.Double(6, 4, 7, 7));

      g.translate(-thumbBounds.x, -thumbBounds.y);
  }





} 
user3172673
  • 1
  • 1
  • 2
  • You should post an [SSCCE](http://sscce.org) for faster help – Paul Samsotha Jan 08 '14 at 10:37
  • I tried what you described and it worked without problems so your problem lies somewhere in the code you didn’t post. By the way I don’t understand why you need to implement a UI. You can alter the properties and behavior of a `JScrollBar` just like with any other component without implementing a UI by just invoking the very same methods your custom UI would invoke. – Holger Jan 08 '14 at 11:19
  • I added the code of the UI since it could be possible that the problem could be find there. – user3172673 Jan 13 '14 at 09:50
  • _I've made my own ScrollbarUI_ why? – kleopatra Jan 13 '14 at 10:40
  • beware: [don't use setXXSize, ever](http://stackoverflow.com/a/7229519/203657) Also, setting a ui per-instance is highly brittle (won't survive any updateUI) – kleopatra Jan 13 '14 at 10:42

1 Answers1

1

Try this:

JScrollPane table = new JScrollPane(content){ @SuppressWarnings("static-access") @Override public JScrollBar createVerticalScrollBar() { return (new Scroler()).makeUI(); } };

Worked for me

Dogiuks
  • 23
  • 4