我尝试调整combobox
的下拉列表,并且代码在第三次点击后起作用:(。
第一次点击为正常combobox
,dropdown
具有相同的宽度。
然后第二次,下拉列表更大但滚动窗格不会改变。
我在popupmenulistener
变得可见之前实施了dropdown
,
public void popupMenuWillBecomeVisible(PopupMenuEvent pme)
{
JComboBox comboBox = (JComboBox) pme.getSource();
if (comboBox.getItemCount() == 0)
{
return;
}
final Object child = comboBox.getAccessibleContext().getAccessibleChild(0);
if (child instanceof BasicComboPopup)
{
JList list = ((BasicComboPopup) child).getList();
list.ensureIndexIsVisible(3);
JScrollPane scrollPane = (JScrollPane) SwingUtilities.getAncestorOfClass(JScrollPane.class, list);
// int scrollBarWidth = 0;
// if (comboBox.getItemCount() > comboBox.getMaximumRowCount()) {
// JScrollBar vertical = scrollPane.getVerticalScrollBar();
// scrollBarWidth = vertical.getPreferredSize().width;
// }
int popupWidth = 500;
Dimension scrollPaneSize = scrollPane.getPreferredSize();
scrollPane.getViewport().setSize(scrollPaneSize);
// popupWidth = Math.max(popupWidth, scrollPaneSize.width);
scrollPaneSize.width = popupWidth;
scrollPane.setMinimumSize(scrollPaneSize);
scrollPane.setPreferredSize(scrollPaneSize);
scrollPane.setMaximumSize(scrollPaneSize);
// scrollPane.repaint();
// ((BasicComboPopup) child).setVisible(false);
((BasicComboPopup) child).setSize(scrollPaneSize);
// ((BasicComboPopup) child).repaint();
// comboBox.setPrototypeDisplayValue("");
// comboBox.setPrototypeDisplayValue(comboBox.getSelectedItem());
// ((BasicComboPopup) child).revalidate();
((BasicComboPopup) child).setVisible(true);
}
}