如何让JComboBox下拉列表比JComboBox本身更宽

通过参考Multi Columns Combo Box for Swing的答案,我设法实现了3个多列JComboBox如下。

替代文字

但是,这并不完美。 我的意图是没有水平滚动条,如下所示。 替代文字

我的问题是,我怎么能有一个JComboBox下拉列表,它比JComboBox本身更宽? 我只是想摆脱水平滚动条。 然而,能够将3列放入单个列表中。

源代码是ResultSetCellRenderer和AjaxAutoCompleteJComboBox

我通过以下论坛Oracle Java Swing论坛解决了我的问题

为了将来参考,我为所有感兴趣的人提供了完整可行的源代码。

AjaxAutoCompleteJComboBox.java

我有同样的问题,所以我创建了以下方法

/** * * @param box is the ComboBox that is about to show its own popup menu * @param metrics is used to calculate the width of your combo box's items */ public static void adjustPopupWidth(JComboBox box,FontMetrics metrics) { if (box.getItemCount() == 0) { return; } Object comp = box.getUI().getAccessibleChild(box, 0); if (!(comp instanceof JPopupMenu)) { return; } //Find which option is the most wide, to set this width as pop up menu's preferred! int maxWidth=0; for(int i=0;i