简体   繁体   English

如何更改 jcombobox 中 jpopupmenu 的高度?

[英]How to change height of a jpopupmenu in a jcombobox?

在此处输入图片说明

I am working on an application in which I am handling key release event over a editable 'JComboBox', where on every key release a 'JPopupMenu' of the 'JComboBox' appears.我正在开发一个应用程序,在该应用程序中,我通过可编辑的“JComboBox”处理按键释放事件,在每次按键释放时,都会出现“JComboBox”的“JPopupMenu”。 I want to increase the height so that user will be able to see more items at a glance without scrolling.我想增加高度,以便用户无需滚动即可一目了然地看到更多项目。 Can anyone please demonstrate how to set the height of 'JPopupMenu' deliberately so that it will show considerable amount of items?任何人都可以演示如何故意设置'JPopupMenu'的高度以使其显示大量项目吗? So far i have tried this, but it doesn not work.到目前为止,我已经尝试过这个,但它不起作用。

combo.getComponentPopupMenu().setSize(10, 10);

Try following: 请尝试以下操作:

ComboPopup popup = (ComboPopup) combo.getUI().getAccessibleChild(combo, 0);
((JComponent) popup).setPreferredSize(size);
((JComponent) popup).setLayout(new GridLayout(1, 1));

It wold be nice to see your SSCCE , so I can test whether my proposal works. 很高兴看到您的SSCCE ,因此我可以测试我的建议是否有效。

A much cleaner approach with using only public API (and without hacks) is to:仅使用公共 API(并且没有黑客攻击)的更简洁的方法是:

  • set the width of the JComboBox with its layout (method setLayout on it's parent) - this will also set the width of the JPopupMenu设置 JComboBox 的宽度及其布局(方法 setLayout 在它的父项上) - 这也将设置 JPopupMenu 的宽度
  • set the height of the JPopupMenu with JComboBox.setMaximumRowCount使用 JComboBox.setMaximumRowCount 设置 JPopupMenu 的高度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM