如何防止JComboBox下拉列表超出垂直屏幕尺寸

发布于 2024-12-18 04:38:02 字数 625 浏览 7 评论 0原文

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

我在 Windows 7 上使用 Java 6u25。

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

这是用于说明的屏幕截图(感谢 @trashgod 的 SSCCE)。截图是在 XP 虚拟机中拍摄的。

在此处输入图像描述

我还在另一台电脑上测试了代码,所以我认为我可以排除一些驱动程序问题。

我喜欢的是一个适合屏幕的下拉列表,我可以在其中完全向下滚动到最后一个值(并查看该值)。相反,我想看到滚动条的向下滚动按钮。

渲染列表单元格并在我的计算中使用它的唯一可能性是什么?组合框高度参数的操作不起作用。

有什么想法如何解决这个问题吗?

令我困惑的是,我没有找到任何与该问题相关的内容。我认为我要么在这里遗漏了一些明显的东西,要么我在搜索中使用了错误的关键字。如果是后两者中的任何一个,我很抱歉,请给我一个提示。

谢谢。

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.

enter image description here

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三生池水覆流年 2024-12-25 04:38:02

我觉得这个描述难以置信。您可以使用 SSCCE 对其进行备份吗?

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

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

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();
            }
        });
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文