How to hide the arrow buttons in a JScrollBar

前端 未结 2 1770
夕颜
夕颜 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:00

    Try this.. it replaces the regular buttons on the Vertical ScrollBar with buttons that are 0x0 in size.

    It does limit your look and feel though :(

    JScrollPane scroller = new JScrollPane(mainPane);
    scroller.setPreferredSize(new Dimension(200,200));
    // ... etc
    scroller.getVerticalScrollBar().setUI(new BasicScrollBarUI()
        {   
            @Override
            protected JButton createDecreaseButton(int orientation) {
                return createZeroButton();
            }
    
            @Override    
            protected JButton createIncreaseButton(int orientation) {
                return createZeroButton();
            }
    
            private JButton createZeroButton() {
                JButton jbutton = new JButton();
                jbutton.setPreferredSize(new Dimension(0, 0));
                jbutton.setMinimumSize(new Dimension(0, 0));
                jbutton.setMaximumSize(new Dimension(0, 0));
                return jbutton;
            }
        });
    

    Update: sorry, this is a swing solution

    0 讨论(0)
  • 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<JComponent> {
    
        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);
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题