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 4015416
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:37:00+00:00 2026-05-20T09:37:00+00:00

I have a problem with a JComboBox popup. My JComboBox has an autocompletition implementation

  • 0

I have a problem with a JComboBox popup. My JComboBox has an autocompletition implementation like google search box.

So, the problem is if I add or remove items when popup is visible it is not resized, I need to close and reopen it. But this fire popupBecomeInvisible and popupBecomeVisible and so i can’t use this events for my real porpouse.

There is a way to “refresh” popup size in according to count of items that it contains, without close and reopen it?

Thanks.

  • 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-20T09:37:00+00:00Added an answer on May 20, 2026 at 9:37 am

    Invoke revalidate() on the panel containing the combobox. This will cause the components to be layed out again based on their preferred sizes.

    This is the same concept as adding/removing a component on a visible GUI.

    Edit:

    Just reread your question. I’m not sure if you can dynamically resize the popup when it is open but you can check out Combo Box Popup. It shows you how to override the preferred width of the popup. This code is executed when the popup menu is about to be shown. But you may be able to use the concepts to access the popup and change the width dynamically.

    Edit 2:

    Here is an example that shows the basic concept. The popup will adjust its width every 2 seconds. However, I don’t know if this will help with your problem because if you are dynamicallyl adding/removing items from the popup, then you will need to recreate the popup every time the popup is changed. This will probably result in the popup hiding/showing which means you will need to live with a little flicker anyway.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.plaf.basic.*;
    
    public class ComboBoxExample extends JPanel implements ActionListener
    {
        private JComboBox comboBox;
    
        public ComboBoxExample()
        {
            String[] petStrings = { "Select Pet", "Bird", "Cat", "Dog", "Rabbit", "Pig", "Other" };
            comboBox = new JComboBox( petStrings );
            add( comboBox, BorderLayout.PAGE_START );
    
            Timer timer = new javax.swing.Timer(2000, this);
            timer.start();
        }
    
        public void actionPerformed(ActionEvent e)
        {
            comboBox.showPopup();
            Object child = comboBox.getAccessibleContext().getAccessibleChild(0);
            BasicComboPopup popup = (BasicComboPopup)child;
            JList list = popup.getList();
            Container c = SwingUtilities.getAncestorOfClass(JScrollPane.class, list);
            JScrollPane scrollPane = (JScrollPane)c;
    
            Dimension size = scrollPane.getSize();
    
            if (size.width > 20)
                size.width -= 5;
    
            scrollPane.setPreferredSize(size);
            scrollPane.setMaximumSize(size);
    
            Dimension popupSize = popup.getSize();
            popupSize.width = size.width;
            Component parent = popup.getParent();
            parent.setSize(popupSize);
    
            parent.validate();
            parent.repaint();
    
            Window mainFrame = SwingUtilities.windowForComponent(comboBox);
            Window popupWindow = SwingUtilities.windowForComponent(popup);
    
            //  For heavy weight popups you need to pack the window
    
            if (popupWindow != mainFrame)
                popupWindow.pack();
        }
    
        private static void createAndShowGUI()
        {
            JFrame frame = new JFrame( "ComboBoxExample" );
            frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            JComponent newContentPane = new ComboBoxExample();
            newContentPane.setOpaque( true );
            frame.setContentPane( newContentPane );
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a little problem. I had some JComboBox to a JDialog but they
I have problem with cakephp's Session->write method. If I set a value like $_SESSION['..']
I have problem with displaying adsense ads in ie7 google adsense adds an iframe
I have a JComboBox which contains an MRU list combo-box and a for a
one problem I'm having is i have 2 JTextAreas and i need to add
I am writing a search form for desktop application and I have a problem
We have a Java Swing application which contains components like JTable, JCombobox, JTextArea and
I have a problem regarding TReeview and combo box. Problem: I have a Treeview
I have list of objects(Artikel.java) and i add them to JComboBox. I want a
I have a WPF app that has a combobox with lookup items. The combobox

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.