Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JComboBox popup is not resized if i add item when it is visible

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.

like image 905
blow Avatar asked Dec 05 '25 23:12

blow


1 Answers

Here is another example that changes the width as items are added:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;

public class ComboBoxExample2 extends JPanel implements ActionListener
{
    private JComboBox comboBox;

    public ComboBoxExample2()
    {
        String[] petStrings = { "A" };
        comboBox = new JComboBox( petStrings );
        comboBox.setPrototypeDisplayValue("A1111111111");
        add( comboBox, BorderLayout.PAGE_START );

        Timer timer = new javax.swing.Timer(2000, this);
        timer.start();
    }

    public void actionPerformed(ActionEvent e)
    {
        String text = comboBox.getItemAt( comboBox.getItemCount() - 1 ).toString();
        comboBox.addItem( text + "1");
        comboBox.showPopup();

        Object child = comboBox.getAccessibleContext().getAccessibleChild(0);
        BasicComboPopup popup = (BasicComboPopup)child;
        JList list = popup.getList();
        Dimension preferred = list.getPreferredSize();
        preferred.width += 20; // allow for scrollbar
        int rowHeight = preferred.height / comboBox.getItemCount();
        int maxHeight = comboBox.getMaximumRowCount() * rowHeight;
        preferred.height = Math.min(preferred.height, maxHeight);

        Container c = SwingUtilities.getAncestorOfClass(JScrollPane.class, list);
        JScrollPane scrollPane = (JScrollPane)c;

        scrollPane.setPreferredSize(preferred);
        scrollPane.setMaximumSize(preferred);

        Dimension popupSize = popup.getSize();
        popupSize.width = preferred.width;
        popupSize.height = preferred.height + 2;
        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( "ComboBoxExample2" );
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        JComponent newContentPane = new ComboBoxExample2();
        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();
            }
        });
    }
}
like image 190
camickr Avatar answered Dec 08 '25 11:12

camickr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!