How to hide the arrow buttons in a JScrollBar

前端 未结 2 1771
夕颜
夕颜 2021-01-02 15:56

I need to hide the arrow buttons of java.awt.Scrollbar(VERTICAL) in an AWT application. Does anyone know how this can be achieved?

I saw an example here, but the cod

2条回答
  •  迷失自我
    2021-01-02 16:11

    Using Nimbus Look and Feel you can use this to remove the arrow buttons:

        UIManager.getLookAndFeelDefaults().put(
            "ScrollBar:\"ScrollBar.button\".size", 0);
        UIManager.getLookAndFeelDefaults().put(
            "ScrollBar.decrementButtonGap", 0);
        UIManager.getLookAndFeelDefaults().put(
            "ScrollBar.incrementButtonGap", 0);
    

    Here is a full example:

    enter image description here

    public class ScrollDemo extends JFrame {
    
        public ScrollDemo() {
    
            String[] columnNames = {"Column"};
            Object[][] data = {
                    {"A"},{"B"},{"C"},{"D"},{"E"},{"F"},
                    {"A"},{"B"},{"C"},{"D"},{"E"},{"F"},
                    {"A"},{"B"},{"C"},{"D"},{"E"},{"F"},
                    {"A"},{"B"},{"C"},{"D"},{"E"},{"F"},
                    {"A"},{"B"},{"C"},{"D"},{"E"},{"F"},
            };
    
            add(new JScrollPane(new JTable(data, columnNames)));
            pack();
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
    
                    try {
                        UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
                    } catch (Exception e) {
                        // No Nimbus
                    }
                    UIManager.getLookAndFeelDefaults().put(
                            "ScrollBar:ScrollBarThumb[Enabled].backgroundPainter",
                            new FillPainter(new Color(127, 169, 191)));
                    UIManager.getLookAndFeelDefaults().put(
                            "ScrollBar:ScrollBarThumb[MouseOver].backgroundPainter",
                            new FillPainter(new Color(127, 169, 191)));
                    UIManager.getLookAndFeelDefaults().put(
                            "ScrollBar:ScrollBarTrack[Enabled].backgroundPainter",
                            new FillPainter(new Color(190, 212, 223)));
    
                    UIManager.getLookAndFeelDefaults().put(
                            "ScrollBar:\"ScrollBar.button\".size", 0);
                    UIManager.getLookAndFeelDefaults().put(
                            "ScrollBar.decrementButtonGap", 0);
                    UIManager.getLookAndFeelDefaults().put(
                            "ScrollBar.incrementButtonGap", 0);
    
                    new ScrollDemo();
                }
            });
        }
    
    }
    

    Code for the Painter used:

    public class FillPainter implements Painter {
    
        private final Color color;
    
        public FillPainter(Color c) { color = c; }
    
        @Override
        public void paint(Graphics2D g, JComponent object, int width, int height) {
            g.setColor(color);
            g.fillRect(0, 0, width-1, height-1);
        }
    
    }
    

提交回复
热议问题