How to prevent JComboBox drop-down list from exceeding vertical screen size

老子叫甜甜 提交于 2019-12-06 11:08:08

I find this description hard to believe. Can you back it up with an SSCCE?

Your skepticism is well founded; my description was based on a distant memory of a remote implementation. With the sscce below, I see a scroll bar and the truncation reported by @uhm; I can only select from among the last, half-dozen, hidden entries by using the keyboard. I get similar results on these platforms:

Mac OS X: [Aqua Look and Feel for Mac OS X - com.apple.laf.AquaLookAndFeel]
Ubuntu:   [The Java(tm) Look and Feel - javax.swing.plaf.metal.MetalLookAndFeel]
Windows:  [The Java(tm) Look and Feel - javax.swing.plaf.metal.MetalLookAndFeel]
import java.awt.EventQueue;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;

/** @see http://stackoverflow.com/questions/8270256 */
public class TallCombo extends JPanel {

    private static final int N = 128;

    public TallCombo() {
        final JComboBox combo = new JComboBox();
        for (int i = 0; i < N; i++) {
            combo.addItem("Item " + Integer.valueOf(i));
        }
        combo.setMaximumRowCount(N / 2);
        this.add(combo);
    }

    private void display() {
        JFrame f = new JFrame("TallCombo");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
        System.out.println(UIManager.getLookAndFeel());
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TallCombo().display();
            }
        });
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!