Deepeshkumar
Deepeshkumar

Reputation: 443

How to change height of a jpopupmenu in a jcombobox?

enter image description here

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. 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? So far i have tried this, but it doesn not work.

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

Upvotes: 1

Views: 1475

Answers (2)

Marko Krajnc
Marko Krajnc

Reputation: 326

A much cleaner approach with using only public API (and without hacks) is to:

  • set the width of the JComboBox with its layout (method setLayout on it's parent) - this will also set the width of the JPopupMenu
  • set the height of the JPopupMenu with JComboBox.setMaximumRowCount

Upvotes: 4

Sergiy Medvynskyy
Sergiy Medvynskyy

Reputation: 11327

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.

Upvotes: 5

Related Questions