Search code examples
javaswingjcombobox

height of dropdown in BasicComboPopup


As mentioned, I am now providing workable code for my problem.

**HTML to Run Applet**
<HTML>
<HEAD>
</HEAD>
<BODY>
<div >
<APPLET CODE="Main.class" WIDTH="800" HEIGHT="500">
</APPLET>
</div>
</BODY>
</HTML>

Applet Class

public class Main extends Applet{
   public void init(){
       JComboBoxUI c = new JComboBoxUI();
       c.addItem("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
       c.addItem("B");
c.addItem("C");
c.addItem("D");
c.addItem("E");
c.addItem("F");
c.addItem("G");
c.addItem("H");    
c.addItem("I");
c.addItem("J");
c.addItem("K");
c.addItem("L");
c.setName("Combo Box");
c.setSelectedItem("D");
       c.setMaximumRowCount(20);
       c.setPreferredSize(new Dimension(150,20));
     add(c);
   }
}

MetalComboBox - override BasicComboPopUp

public class JComboBoxUI extends JComboBox{

     public void updateUI() {
          setUI(new CustomComboBoxUI());
       }

       public void addPopupMenuListener(PopupMenuListener l) {
           CustomComboBoxUI ui = (CustomComboBoxUI)getUI();
          ui.addPopupMenuListener(l);
       }


    public class CustomComboBoxUI extends MetalComboBoxUI {
           protected JPopupMenu _popup;
            protected ComboPopup createPopup() {
                 BasicComboPopup popup = new BasicComboPopup(comboBox) {
                    public JScrollPane createScroller() {
                        JScrollPane j = new JScrollPane( list, ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
                              ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
                        return j;
                     }
                 };         
                 _popup = (JPopupMenu)popup;
                 return popup;
              }
           @Override 
           protected void installListeners() {
               super.installListeners();
           }

           public void addPopupMenuListener(PopupMenuListener l) {
              if (_popup != null)
                 _popup.addPopupMenuListener(l);
           }
        }
}

Problem

If you will run this, you will see that vertical scrollbar is not coming(as set in JScrollPane) and some elements are not showing up. I want to increase height of Popup so I can see all elements. Any Help


Solution

  • I have overridden BasicComboPopup class and overridden getPopupHeightForRowCount(int maxRowCount) method to include height of horizontal scroll bar.

     public class OverrideComboPopup extends BasicComboPopup {
    
        private JScrollPane sPane;
        public KronosComboPopup(KronosComboBox combo) {
            super(combo);
            setBackground(GUIResources.POPUP_MENU_BACKGROUND);
        }
    
     public JScrollPane createScroller() {
            sPane = new JScrollPane( list, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
                          ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            return sPane;
         }
    
         //Overrides the getPopupHeightForRowCount to include the height of horizontal bar.
         protected int getPopupHeightForRowCount(int maxRowCount) {
            int currentElementCount = comboBox.getModel().getSize();
            int rowCount = Math.min( maxRowCount, currentElementCount );
            if (rowCount==0) return rowCount;
            int ht = super.getPopupHeightForRowCount(maxRowCount);
            double cWidth = comboBox.getSize().getWidth();
            double vWidth = list.getPreferredScrollableViewportSize().getWidth();
            if (vWidth>cWidth) {
               int barHt = sPane.getHorizontalScrollBar().getHeight();
               //Sets the default height of scroll bar
               if (barHt==0) barHt = 16;
               ht = ht + barHt;
            }
            return ht;
         }
    
    
        }
    

    Let me know if you stuck anytime with this, contact me on [email protected]