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

£可爱£侵袭症+ 提交于 2019-12-22 14:59:36

问题


I use a JComboBox with many entries (hundreds). I want to limit the size of its drop-down list to the vertical size of the screen. Using a fixed size does not work out properly for different look&feels and screen resolutions.

I am using Java 6u25 on Windows 7.

If I set the maximum row count to a value (e.g. 100) that exceeds the number of list items (=rows) that fit on the screen (75), the drop-down list seems to be drawn in full size but the lowest entries are never visible.

Here is a screenshot for illustation (thanks for the SSCCE by @trashgod). The sceenshot was taken in a virtual machine on XP.

I also tested the code on another PC, so I think I can rule out some driver issues.

What I like to have is a drop-down list that fits on screen where I can scroll down completely to the very last value (and see that value). The other way round, I would like to see the scroll down button of the scrollbar.

Is the only possibility to render a cell of the list and use this in my calculations? Manipulation of height parameters of the combobox did not work.

Any ideas how to solve this?

What puzzles me is that I did not find any reference to that problem whatsoever. I assume that I am either missing something obvious here or that I am using the wrong keywords for my search. If any of the latter two, my apologies please give me a hint.

Thanks.


回答1:


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();
            }
        });
    }
}



回答2:


I found this example: http://www.javaprogrammingforums.com/awt-java-swing/11457-jcombobox-scroll-bars.html (at the bottom)



来源:https://stackoverflow.com/questions/8270256/how-to-prevent-jcombobox-drop-down-list-from-exceeding-vertical-screen-size

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