Alter JComboBox popup size without disturbing Look and Feel?

后端 未结 2 1608
旧时难觅i
旧时难觅i 2021-01-26 19:23

I\'m looking for a way to alter the width of a JComboBox\'s popup. Basically, the popup should be as wide as the widest combobox entry requires, not as wide as the combobox curr

2条回答
  •  盖世英雄少女心
    2021-01-26 19:51

    1. by using GBC you are be able to set proper height and weight in the container

    2. as @trashgod mentioned next way is to set for PreferredSize by using JComboBox.setPrototypeDisplayValue()

    3. use Combo Box Popup by @camickr

    4. for derived JPopup must be used pack() otherwise too hard to change its Dimension easilly

    .

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.plaf.basic.*;
    
    public class ComboBoxExample extends JPanel implements ActionListener {
    //http://stackoverflow.com/a/5058210/714968
    
        private static final long serialVersionUID = 1L;
        private JComboBox comboBox;
    
        public ComboBoxExample() {
            String[] petStrings = {"Select Pet", "Bird", "Cat", "Dog", "Rabbit", "Pig", "Other"};
            comboBox = new JComboBox(petStrings);
            comboBox.setPrototypeDisplayValue("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
            add(comboBox, BorderLayout.PAGE_START);
            JFrame frame = new JFrame("ComboBoxExample");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(comboBox);
            frame.setName("ComboBoxExample");
            frame.setLocation(150, 150);
            frame.pack();
            frame.setVisible(true);
            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;
            popup.setName("BasicComboPopup");
            JList list = popup.getList();
            Container c = SwingUtilities.getAncestorOfClass(JScrollPane.class, list);
            JScrollPane scrollPane = (JScrollPane) c;
            Dimension size = scrollPane.getSize();
            if (size.width > 30) {
                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);
            //Returns the first Window ancestor of c, or null if c is not contained inside a Window.
            System.out.println(mainFrame.getName());
            Window popupWindow = SwingUtilities.windowForComponent(popup);
            System.out.println(popupWindow.getName());
            Window popupWindowa = SwingUtilities.windowForComponent(c);
            System.out.println(popupWindowa.getName());
    
            Window mainFrame1 = SwingUtilities.getWindowAncestor(comboBox);
            //Returns the first Window ancestor of c, or null if c is not contained inside a Window.
            System.out.println(mainFrame1.getName());
            Window popupWindow1 = SwingUtilities.getWindowAncestor(popup);
            System.out.println(popupWindow1.getName());
    
            Component mainFrame2 = SwingUtilities.getRoot(comboBox);
            //Returns the root component for the current component tree.
            System.out.println(mainFrame2.getName());
            Component popupWindow2 = SwingUtilities.getRoot(popup);
            System.out.println(popupWindow2.getName());
            //  For heavy weight popups you need always to pack() for the window
            if (popupWindow != mainFrame) {
                popupWindow.pack();
            }
        }
    
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
    
                public void run() {
                    ComboBoxExample comboBoxExample = new ComboBoxExample();
                }
            });
        }
    }
    

提交回复
热议问题