JComboBox自定义垂直滚动条

时间:2016-05-23 17:19:24

标签: java swing jcombobox custom-component vertical-scrolling

这个目的主要是审美,我已经在JTextArea上做了类似的事情,但是我无法弄明白或者可以从JComboBox上获取弹出滚动条。我通过将其设置为0来移除显示在顶部的箭头。

    setUI(new BasicComboBoxUI(){
        protected JButton createArrowButton(){
            return new JButton(){
                @Override public int getWidth() {
                    return 0;
                }
                @Override
                public void setFocusable(boolean focusable) {
                    super.setFocusable(false);
                }
            };
        }
    });

jcombobox image

这是JScrollPane内部的JTextArea,没有箭头和更细的条

    getVerticalScrollBar().setPreferredSize(new Dimension(10, 0));
    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;
        }
    });

jtextarea example

2 个答案:

答案 0 :(得分:3)

您可以覆盖BasicComboBoxUI的{​​{3}}方法,以便创建新的getPopup()。然后,您可以访问BasicComboPopup方法,通过覆盖它,您可以返回自己的自定义JScrollPane

createScroller()

import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.plaf.basic.BasicComboBoxUI;
import javax.swing.plaf.basic.BasicComboPopup;
import javax.swing.plaf.basic.BasicScrollBarUI;
import javax.swing.plaf.basic.ComboPopup;

public class Example {

    public Example() {
        JComboBox<Integer> comboBox = new JComboBox<Integer>();
        for (int i = 1; i <= 10; i++) {
            comboBox.addItem(i);
        }

        comboBox.setUI(new BasicComboBoxUI() {
            @Override
            protected ComboPopup createPopup() {
                return new BasicComboPopup(comboBox) {
                    @Override
                    protected JScrollPane createScroller() {
                        JScrollPane scroller = new JScrollPane(list, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
                        scroller.getVerticalScrollBar().setUI(new BasicScrollBarUI() {
                            @Override
                            protected JButton createDecreaseButton(int orientation) {
                                return createZeroButton();
                            }

                            @Override
                            protected JButton createIncreaseButton(int orientation) {
                                return createZeroButton();
                            }

                            @Override
                            public Dimension getPreferredSize(JComponent c) {
                                return new Dimension(10, super.getPreferredSize(c).height);
                            }

                            private JButton createZeroButton() {
                                return new JButton() {
                                    @Override
                                    public Dimension getMinimumSize() {
                                        return new Dimension(new Dimension(0, 0));
                                    }

                                    @Override
                                    public Dimension getPreferredSize() {
                                        return new Dimension(new Dimension(0, 0));
                                    }

                                    @Override
                                    public Dimension getMaximumSize() {
                                        return new Dimension(new Dimension(0, 0));
                                    }
                                };
                            }
                        });
                        return scroller;
                    }
                };
            }
        });

        JPanel mainPanel = new JPanel();
        mainPanel.add(comboBox);

        JFrame frame = new JFrame("Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Example();
            }
        });
    }

}

答案 1 :(得分:0)

您可以尝试以下reference

中的代码
private void updateScrollBar(JComboBox<E> box) {
    Object comp = box.getUI().getAccessibleChild(box, 0);

    if (comp instanceof JPopupMenu) {
        JPopupMenu popup = (JPopupMenu) comp;
        JScrollPane scrollPane = (JScrollPane) popup.getComponent(0);
        jScrollPane.getVerticalScrollBar().setPreferredSize(new Dimension(35, 35));
    }
}