@Override public Dimension getPreferredSize() { Component[] comps = getAllComponents(); Insets insets = getInsets(); int width = null == insets ? 0 : insets.left + insets.right; int height = null == insets ? 0 : insets.top + insets.bottom; for (int i = 0; i < comps.length; i++) { Component comp = comps[i]; if (!comp.isVisible()) { continue; } width += getOrientation() == HORIZONTAL ? comp.getPreferredSize().width : comp.getPreferredSize().height; height = Math.max( height, getOrientation() == HORIZONTAL ? comp.getPreferredSize().height : comp.getPreferredSize().width ); } if(overflowToolbar.getComponentCount() > 0) { width += getOrientation() == HORIZONTAL ? overflowButton.getPreferredSize().width : overflowButton.getPreferredSize().height; } Dimension dim = getOrientation() == HORIZONTAL ? new Dimension(width, height) : new Dimension(height, width); return dim; }
private void setupOverflowButton() { overflowButton = new JButton(ImageUtilities.loadImageIcon(getOrientation() == HORIZONTAL ? toolbarArrowVertical : toolbarArrowHorizontal, false)); overflowButton.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { if(popup.isShowing()) { popup.setVisible(false); } else { displayOverflow(); } } @Override public void mouseEntered(MouseEvent e) { if(displayOverflowOnHover) { displayOverflow(); } } }); }
private void handleOverflowAddittion(int visibleButtons) { Component[] comps = getAllComponents(); removeAll(); overflowToolbar.setOrientation(getOrientation() == HORIZONTAL ? VERTICAL : HORIZONTAL); popup.removeAll(); for (Component comp : comps) { if (visibleButtons > 0) { add(comp); if (comp.isVisible()) { visibleButtons--; } } else { overflowToolbar.add(comp); } } popup.add(overflowToolbar); add(overflowButton); }
private int computeVisibleButtons() { if (isShowing()) { int w = getOrientation() == HORIZONTAL ? overflowButton.getIcon().getIconWidth() + 4 : getWidth() - getInsets().left - getInsets().right; int h = getOrientation() == HORIZONTAL ? getHeight() - getInsets().top - getInsets().bottom : overflowButton.getIcon().getIconHeight() + 4; overflowButton.setMaximumSize(new Dimension(w, h)); overflowButton.setMinimumSize(new Dimension(w, h)); Component[] comps = getAllComponents(); int sizeSoFar = 0; int maxSize = getOrientation() == HORIZONTAL ? getWidth() : getHeight(); int overflowButtonSize = getOrientation() == HORIZONTAL ? overflowButton.getPreferredSize().width : overflowButton.getPreferredSize().height; int showingButtons = 0; // all that return true from isVisible() int visibleButtons = 0; // all visible that fit into the given space (maxSize) Insets insets = getInsets(); if( null != insets ) { sizeSoFar = getOrientation() == HORIZONTAL ? insets.left+insets.right : insets.top+insets.bottom; int size = getOrientation() == HORIZONTAL ? comp.getPreferredSize().width : comp.getPreferredSize().height; if (sizeSoFar + size <= maxSize) { sizeSoFar += size;
private void displayOverflow() { int x = getOrientation() == HORIZONTAL ? overflowButton.getLocationOnScreen().x : overflowButton.getLocationOnScreen().x + overflowButton.getWidth(); int y = getOrientation() == HORIZONTAL ? overflowButton.getLocationOnScreen().y + overflowButton.getHeight() : overflowButton.getLocationOnScreen().y; popup.setLocation(x, y); popup.setVisible(true); }
int minX = overflowButton.getLocationOnScreen().x; int maxX_ob = minX + overflowButton.getWidth(); int maxX = getOrientation() == HORIZONTAL ? minX + popup.getWidth() : minX + overflowButton.getWidth() + popup.getWidth(); int minY = overflowButton.getLocationOnScreen().y; int maxY_ob = minY + overflowButton.getHeight(); int maxY = getOrientation() == HORIZONTAL ? minY + overflowButton.getHeight() + popup.getHeight() : minY + popup.getHeight(); if (e.getXOnScreen() < minX || e.getYOnScreen() < minY || e.getXOnScreen() > maxX || e.getYOnScreen() > maxY || (getOrientation() == HORIZONTAL && e.getXOnScreen() > maxX_ob && e.getYOnScreen() >= minY && e.getYOnScreen() < maxY_ob) || (getOrientation() == VERTICAL && e.getXOnScreen() >= minX && e.getXOnScreen() < maxX_ob && e.getYOnScreen() > maxY_ob)) { popup.setVisible(false);