/* * This method is called every time: * - to make sure the viewport is returned to its default position * - to remove the horizontal scrollbar when it is not wanted */ private void checkHorizontalScrollBar(BasicComboPopup popup) { // Reset the viewport to the left JViewport viewport = scrollPane.getViewport(); Point p = viewport.getViewPosition(); p.x = 0; viewport.setViewPosition(p); // Remove the scrollbar so it is never painted if (!scrollBarRequired) { scrollPane.setHorizontalScrollBar(null); return; } // Make sure a horizontal scrollbar exists in the scrollpane JScrollBar horizontal = scrollPane.getHorizontalScrollBar(); if (horizontal == null) { horizontal = new JScrollBar(JScrollBar.HORIZONTAL); scrollPane.setHorizontalScrollBar(horizontal); scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); } // Potentially increase height of scroll pane to display the scrollbar if (horizontalScrollBarWillBeVisible(popup, scrollPane)) { Dimension scrollPaneSize = scrollPane.getPreferredSize(); scrollPaneSize.height += horizontal.getPreferredSize().height; scrollPane.setPreferredSize(scrollPaneSize); scrollPane.setMaximumSize(scrollPaneSize); scrollPane.revalidate(); } }
/* * I can't find any property on the scrollBar to determine if it will be * displayed or not so use brute force to determine this. */ protected int getScrollBarWidth(BasicComboPopup popup, JScrollPane scrollPane) { int scrollBarWidth = 0; JComboBox comboBox = (JComboBox) popup.getInvoker(); if (comboBox.getItemCount() > comboBox.getMaximumRowCount()) { JScrollBar vertical = scrollPane.getVerticalScrollBar(); scrollBarWidth = vertical.getPreferredSize().width; } return scrollBarWidth; }