1

I am having a problem about JScrollBar.

In my application I have 2 panels (one fixed, and one that changes depending on what the user chooses in the menu)

In a given panel I have a JScrollPane with a table. Since the standard java scrollbar was outside the theme of the application, I decided to try to create another one.

I basically create a JScrollPane and change its scrollBar to a custom one I created (scrollPane.setVerticalScrollBar (new CustomScrollBar ()) ;.

The first time I present the panel, the scrollbar is perfect. However, when I change to another panel and go back to it, the scrollbar model has reset.

What could this be/how do I solve it?

Creation JScrollPane code:

JScrollPane tablePanel = new JScrollPane(table);
tablePanel.setBounds(10, 148, 495, 200);
tablePanel.setBorder(new EmptyBorder(0, 0, 0, 0));
tablePanel.setBackground(Color.WHITE);
tablePanel.setVerticalScrollBar(new CustomScrollBar());
add(tablePanel);

CustomScrollBar Code:

public class CustomScrollBar extends JScrollBar {

    public CustomScrollBar() {
        setOpaque(false);
        
        setUI(new BasicScrollBarUI() {
            private final Dimension d = new Dimension();

            @Override
            protected JButton createDecreaseButton(int orientation) {
                return new JButton() {
                    @Override
                    public Dimension getPreferredSize() {
                        return d;
                    }
                };
            }

            @Override
            protected JButton createIncreaseButton(int orientation) {
                return new JButton() {
                    @Override
                    public Dimension getPreferredSize() {
                        return d;
                    }
                };
            }

            @Override
            protected void paintTrack(Graphics g, JComponent c, Rectangle r) {
            }

            @Override
            protected void paintThumb(Graphics g, JComponent c, Rectangle r) {
                Graphics2D g2 = (Graphics2D) g.create();
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                Color color = null;
                JScrollBar sb = (JScrollBar) c;
                if (!sb.isEnabled() || r.width > r.height) {
                    return;
                } else if (isDragging) {
                    color = Colors.superdarkPurple;
                } else if (isThumbRollover()) {
                    color = Colors.lightPurple;
                } else {
                    color = Colors.darkPurple;
                }
                g2.setPaint(color);
                g2.fillRoundRect(r.x, r.y, r.width, r.height, 10, 10);
                g2.setPaint(Color.WHITE);
                g2.drawRoundRect(r.x, r.y, r.width, r.height, 10, 10);
                g2.dispose();
            }

            @Override
            protected void setThumbBounds(int x, int y, int width, int height) {
                super.setThumbBounds(x, y, width, height);
                scrollbar.repaint();
            }
        });
    }
    
    public JScrollBar geCustomScrollBar() {
        return this;
    }

EDIT 1

This goes to a panel to add questions

public void gotoAddQuestionsPanel(Question q) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                currentQuestionsPanel = "add";
                updateFrameComponentTreeUI();
                remove(subPanel);
                if (q != null) {
                    addQuestionsPanel.setQuestion(q);
                }
                subPanel = addQuestionsPanel.getPanel();
                subPanel.setBackground(Color.WHITE);
                subPanel.setBounds(304, 0, 515, 415);
                getContentPane().add(subPanel);
                subPanel.setLayout(null);
            }
        });
    }

And this goes back to what has the customized ScrollBar

public void gotoQueryQuestionsPanel() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                currentQuestionsPanel = "query";
                updateFrameComponentTreeUI();
                remove(subPanel);
                subPanel = queryQuestionsPanel.getPanel();
                subPanel.setBackground(Color.WHITE);
                subPanel.setBounds(304, 0, 515, 415);
                getContentPane().add(subPanel);
                subPanel.setLayout(null);
            }
        });
    }

public void updateFrameComponentTreeUI() {
    SwingUtilities.updateComponentTreeUI(this);
}
Kuro
  • 19
  • 2
  • "However, when I change to another panel and go back to it, the scrollbar model has reset." What Java code changes to another panel and goes back to the table panel? – Gilbert Le Blanc Jul 17 '20 at 14:05
  • @GilbertLeBlanc I updated my question with the information you asked for. I'm sorry I didn't put it on at first – Kuro Jul 18 '20 at 21:36
  • This interests me because I intend to customize a ScrollBar for a widget in my project but haven't done so yet. I'm wondering, from the code you have shown, what exactly is the connection between JScrollPane tablePanel and the rest of the code which doesn't have any mentions of either JScrollPane or tablePanel that I can see. Can you make this more explicit? Is "subPanel" the JScrollPane tablePanel? Can you show the method queryQuestionsPanel.getPanel()? That's code you wrote, correct? – Phil Freihofner Jul 18 '20 at 22:03
  • You could use a CardLayout or a JTabbedPane to hold the JPanels. https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html https://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html – Gilbert Le Blanc Jul 19 '20 at 03:37
  • 1
    @PhilFreihofner The bar code I found online and adapted it for me. Here is the link: https://stackoverflow.com/questions/16373459/java-jscrollbar-design/16375805 – Kuro Jul 20 '20 at 00:02
  • @GilbertLeBlanc Okay, I'm going to test and see if it solves my problem. Thank you :) – Kuro Jul 20 '20 at 00:03
  • Looks interesting, thanks @Kurottebayo. – Phil Freihofner Jul 20 '20 at 04:57

0 Answers0