Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Resize combobox dropdown

f9141710-e037-4870-a0f5-22cb0e4b0d9dNov 4 2014 — edited Nov 4 2014

I try to resize the dropdown of combobox and the code works since the third clicks :(. The first click as the normal combobox, the dropdown has the same width. Then the second time, the dropdown is larger but the scrollpane does not change.

Capture.PNG

I implemented the popupmenulistener, before the dropdown becomes visible,

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

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 2 2014
Added on Nov 4 2014
0 comments
683 views