Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7079759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:42:41+00:00 2026-05-28T06:42:41+00:00

I am trying to add horizontal scroll bar in a JComboBox using reference with

  • 0

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.

How to add a horizontal scroll bar to JComboBox correctly?

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 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:42:42+00:00Added an answer on May 28, 2026 at 6:42 am

    if you meaning code posted by @Kleopatra then void adjustScrollBar() never will be called from 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) {
            }
        };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to add a scroll bar to a JTextArea. Would someone please
I am trying to add vertical and horizontal scrollbars to my UserControl with the
I'm trying add a tab to my web page that looks like this: Using
I'm trying to add a horizontal rule as the last item in a div
I'm trying to add a scrollbar to a JList (which uses a custom data
I'm trying to add a horizontal line on top to divide the header text
I'm trying to add a horizontal line to my axes to mark an important
I'm using GWT 2.4. I'm trying to create a tabs panel that has multiple
Trying to add an onclick handler to my tabs, and can't seem to get
trying to add an ExpiresDefault ExpiresByType to content on my website so that way

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.