如何将另一个LayoutManager应用于JComboBox? (多列JComboBox尝试) [英] How to apply another LayoutManager to a JComboBox? (multi-column JComboBox attempt)

查看:84
本文介绍了如何将另一个LayoutManager应用于JComboBox? (多列JComboBox尝试)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作多列JComboBox.我环顾了四周,这似乎是一件非常棘手的事情.除非有很多人,否则我对拥有表格(在其中选择行)不感兴趣:我需要消除JComboBox中的滚动条,为了实现这一点,我想将其项目放置在多列中而不是仅将它们放在一列中.

I'm trying to make a multi-column JComboBox. I've looked around quite a bit and it seems to be a very tricky thing to do. Unless many people, I'm not interested in having a table (where you select a row): I need to eliminate the scroll bar in the JComboBox and, in order to achieve this, I want to lay its items in a multi-column list instead of having them in only one column.

到目前为止,我最好的选择是这样做:

My best bet so far was to do this:

JComboBox dropdown = new JComboBox(validValues);
CellRendererPane crp = (CellRendererPane) dropdown.getComponent(1);
crp.setLayout(new GridLayout(4, 4)); // for 16 items...

但是它不起作用.它仍然将单元格放在单个列中.设置LayoutManager后,我尝试添加项目,但这不会影响结果.

But it doesn't work. It still lays cells in a single column. I tried adding items after setting the LayoutManager, but it doesn't affect the result.

有人知道如何实现这一目标吗?

Anyone has a clue about how to achieve this?

到目前为止,我已经看到ListCellRenderer没用.它仅指定如何绘制一个单元格(一次一个),而不指定如何放置所有单元格(它们彼此之间的相对位置).

So far, I've seen the ListCellRenderer as useless to play with. It only specifies how to draw a cell (one at a time), not how to lay all of them (what is their relative position to each other).

欢迎任何帮助!

谢谢!

MJ

推荐答案

组合框使用JList在弹出窗口中呈现项目.默认情况下,每个项目都显示在一行中.您可以使用以下方法直接访问此列表:

A combobox uses a JList to render the items in a popup. By default each item is displayed in a single row. You can access this list directly using:

Object child = comboBox.getAccessibleContext().getAccessibleChild(0);
BasicComboPopup popup  = (BasicComboPopup)child;
JList list = popup.getList();

现在您可以访问列表了,您应该可以使用以下方法更改默认显示:

Now that you have acess to the list you should be able to change the default display by using:

list.setLayoutOrientation(JList.HORIZONTAL_WRAP);

希望现在,当下拉列表的宽度达到时,这些项目将自动换行.下拉列表的宽度由组合框的宽度控制,因此您可能需要使用以下组合框来播放组合框的宽度:

Hopefully the items will now wrap when the width of the dropdown is reached. The width of the dropdown is controlled by the width of the combo box so you may need to play with the width of the combo box by using:

list.setPrototypeDisplayValue(....);

实际上,忘了使用setPrototypeDisplayValue(...),我认为您将需要手动设置弹出窗口的大小.

Actually, forget about using setPrototypeDisplayValue(...), I think you will need to manually set the size of the popup.

默认情况下,弹出窗口的宽度始终等于组合框的宽度.您可以通过使用PopupMenuListener覆盖弹出窗口的大小来修改此行为.要开始使用,可以查看组合框弹出窗口入口.您的代码将更加简单,因为您所需要做的就是对所需的弹出窗口宽度进行硬编码.

By default the width of the popup is always equal to the width of the combo box. You can modify this behaviour by using a PopupMenuListener to override the size of the popup. To get you started you can look at the Combo Box Popup entry. Your code will be much simpler since all you will need to do is hardcode the desired width of your popup.

这篇关于如何将另一个LayoutManager应用于JComboBox? (多列JComboBox尝试)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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