如何防止JComboBox下拉列表超出垂直屏幕大小 [英] How to prevent JComboBox drop-down list from exceeding vertical screen size

查看:160
本文介绍了如何防止JComboBox下拉列表超出垂直屏幕大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用带有许多条目的JComboBox(数百个)。我想将其下拉列表的大小限制为屏幕的垂直大小。对于不同的外观和感觉以及屏幕分辨率,使用固定大小无法正常工作。

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.

我在Windows 7上使用Java 6u25。

I am using Java 6u25 on Windows 7.

如果我将最大行数设置为超过适合屏幕(75)的列表项(=行)数的值(例如100),则下拉列表似乎被绘制全尺寸但最低的条目永远不可见。

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.

这是插图的截图(感谢@trashgod的SSCCE)。在XP上的虚拟机中拍摄了sceenshot。

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

我还测试了另一台PC上的代码,所以我想我可以排除一些驱动程序问题。

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.

谢谢。

推荐答案


我觉得这个描述很难相信。你能用SSCCE备份它吗?

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

你的怀疑是有根据的;我的描述是基于远程实现的远程记忆。通过下面的 sscce ,我看到了一个滚动条和@uhm报告的截断;我只能使用键盘从最后的六个隐藏条目中进行选择。我在这些平台上得到了类似的结果:

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

这篇关于如何防止JComboBox下拉列表超出垂直屏幕大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆