在JComboBox中添加水平滚动条不起作用

时间:2022-12-05 21:21:44

I am trying to add horizontal scroll bar in a JComboBox using reference with thread - Horizontal scrollbar for JComboBox across multiple look and feel at the OTN, but it's not working in my case.

我正在尝试使用带有线程的引用在JComboBox中添加水平滚动条 - 用于JComboBox的水平滚动条跨越OTN的多个外观,但它在我的情况下不起作用。

How to add a horizontal scroll bar to JComboBox correctly?

如何正确添加水平滚动条到JComboBox?

Code -

代码 -

public class TestJComboBoxWithScrollBar {

    TestJComboBoxWithScrollBar() {
        JDialog jDialog = new JDialog();
        jDialog.setTitle("Test JComboBox With ScrollBar");
        JPanel jPanel_Sort = new JPanel();
        GridBagLayout gbl = new GridBagLayout();
        GridBagConstraints bagConstraints = new GridBagConstraints();
        jPanel_Sort.setLayout(gbl);
        bagConstraints.gridwidth = 1;
        bagConstraints.gridheight = 1;
        bagConstraints.fill = GridBagConstraints.NONE;
        bagConstraints.anchor = GridBagConstraints.WEST;
        bagConstraints.weightx = 0;
        bagConstraints.weighty = 0;
        bagConstraints.insets = new Insets(5, 5, 5, 5);
        bagConstraints.gridx = 0;
        bagConstraints.gridy = 0;
        SampleJComboBoxWithScrollBar cmbHeaders = new SampleJComboBoxWithScrollBar();
        cmbHeaders.addItem("aaaaaaaaaaaaa");
        cmbHeaders.addItem("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
        cmbHeaders.setPreferredSize(new Dimension(190, 50));
        cmbHeaders.setMinimumSize(new Dimension(190, 50));
        cmbHeaders.setMaximumSize(new Dimension(190, 50));
        jPanel_Sort.add(cmbHeaders, bagConstraints);
        JCheckBox chkOrderBy = new JCheckBox("Asc");
        bagConstraints.gridx = 1;
        bagConstraints.gridy = 0;
        jPanel_Sort.add(chkOrderBy, bagConstraints);
        jPanel_Sort.setPreferredSize(new Dimension(220, 70));
        jPanel_Sort.setMinimumSize(new Dimension(220, 70));
        jPanel_Sort.setMaximumSize(new Dimension(220, 70));
        jDialog.add(jPanel_Sort, BorderLayout.CENTER);
        jDialog.setPreferredSize(new Dimension(300, 100));
        jDialog.pack();
        jDialog.setResizable(false);
        jDialog.setModal(true);
        jDialog.setVisible(true);
    }

    public static void main(String[] argu) {
        new TestJComboBoxWithScrollBar();
    }

    class SampleJComboBoxWithScrollBar extends JComboBox {

        SampleJComboBoxWithScrollBar() {
            super();
            this.addPopupMenuListener(this.getPopupMenuListener());
            this.adjustScrollBar();
        }

        private void adjustPopupWidth() {
            if (getItemCount() == 0) {
                return;
            }
            Object comp = getUI().getAccessibleChild(this, 0);
            if (!(comp instanceof JPopupMenu)) {
                return;
            }
            JPopupMenu popup = (JPopupMenu) comp;
            JScrollPane scrollPane = (JScrollPane) popup.getComponent(0);
            Object value = getItemAt(0);
            Component rendererComp = getRenderer().getListCellRendererComponent(new JList(), value, 0, false, false);
            if (rendererComp instanceof JXTable) {
                scrollPane.setColumnHeaderView(((JTable) rendererComp).getTableHeader());
            }
            Dimension prefSize = rendererComp.getPreferredSize();
            Dimension size = scrollPane.getPreferredSize();
            size.width = Math.max(size.width, prefSize.width);
            scrollPane.setPreferredSize(size);
            scrollPane.setMaximumSize(size);
            scrollPane.revalidate();
        }

        private void adjustScrollBar() {
            if (getItemCount() == 0) {
                return;
            }
            Object comp = getUI().getAccessibleChild(this, 0);
            if (!(comp instanceof JPopupMenu)) {
                return;
            }
            JPopupMenu popup = (JPopupMenu) comp;
            JScrollPane scrollPane = (JScrollPane) popup.getComponent(0);
            scrollPane.setHorizontalScrollBar(new JScrollBar(JScrollBar.HORIZONTAL));
            scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        }

        private PopupMenuListener getPopupMenuListener() {

            return new PopupMenuListener() {

                @Override
                public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
                    adjustPopupWidth();
                }

                @Override
                public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
                }

                @Override
                public void popupMenuCanceled(PopupMenuEvent e) {
                }
            };

        }
    }
 }

1 个解决方案

#1


3  

if you meaning code posted by @Kleopatra then void adjustScrollBar() never will be called from PopupMenuListener

如果你的意思是@Kleopatra发布的代码那么void adjustScrollBar()将永远不会从PopupMenuListener调用

private PopupMenuListener getPopupMenuListener() {

    return new PopupMenuListener() {

        @Override
        public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
            //adjustPopupWidth();
            adjustScrollBar();
        }

        @Override
        public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
        }

        @Override
        public void popupMenuCanceled(PopupMenuEvent e) {
        }
    };
}

#1


3  

if you meaning code posted by @Kleopatra then void adjustScrollBar() never will be called from PopupMenuListener

如果你的意思是@Kleopatra发布的代码那么void adjustScrollBar()将永远不会从PopupMenuListener调用

private PopupMenuListener getPopupMenuListener() {

    return new PopupMenuListener() {

        @Override
        public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
            //adjustPopupWidth();
            adjustScrollBar();
        }

        @Override
        public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
        }

        @Override
        public void popupMenuCanceled(PopupMenuEvent e) {
        }
    };
}