简体   繁体   中英

Custom Scrollbar in JScrollpane//Java Swing

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);
  }





} 

Try this:

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

Worked for me

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM