Java Code Examples for javax.swing.plaf.basic.BasicComboPopup

The following examples show how to use javax.swing.plaf.basic.BasicComboPopup. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: openAGV   Source File: BoundsPopupMenuListener.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Adjusts the width of the scrollpane used by the popup.
 */
protected void popupWider(BasicComboPopup popup) {
  JList<?> list = popup.getList();

  //  Determine the maximimum width to use:
  //  a) determine the popup preferred width
  //  b) ensure width is not less than the scroll pane width
  int popupWidth = list.getPreferredSize().width
      + 5 // make sure horizontal scrollbar doesn't appear
      + getScrollBarWidth(popup, scrollPane);

  Dimension scrollPaneSize = scrollPane.getPreferredSize();
  popupWidth = Math.max(popupWidth, scrollPaneSize.width);

  //  Adjust the width
  scrollPaneSize.width = popupWidth;
  scrollPane.setPreferredSize(scrollPaneSize);
  scrollPane.setMaximumSize(scrollPaneSize);
}
 
Example 2
private static Point getItemPointToClick(final int item) throws Exception {
    final Point[] result = new Point[1];

    SwingUtilities.invokeAndWait(new Runnable() {
        @Override
        public void run() {
            BasicComboPopup popup = (BasicComboPopup)comboBoxUI.getComboPopup();
            Point point = popup.getLocationOnScreen();
            Dimension size = popup.getSize();

            int step = size.height / items.length;
            point.x += size.width / 2;
            point.y += step / 2 + step * item;
            result[0] = point;
        }
    });
    return result[0];
}
 
Example 3
private static Point getItemPointToClick(final int item) throws Exception {
    final Point[] result = new Point[1];

    SwingUtilities.invokeAndWait(new Runnable() {
        @Override
        public void run() {
            BasicComboPopup popup = (BasicComboPopup)comboBoxUI.getComboPopup();
            Point point = popup.getLocationOnScreen();
            Dimension size = popup.getSize();

            int step = size.height / items.length;
            point.x += size.width / 2;
            point.y += step / 2 + step * item;
            result[0] = point;
        }
    });
    return result[0];
}
 
Example 4
Source Project: xdm   Source File: XDMComboBoxUI.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
protected ComboPopup createPopup() {
	return new BasicComboPopup(comboBox) {
		/**
		 * 
		 */
		private static final long serialVersionUID = -4232501153552563408L;

		@Override
		protected JScrollPane createScroller() {
			JScrollPane scroller = new JScrollPane(list, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
					JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
			scroller.setVerticalScrollBar(new DarkScrollBar(JScrollBar.VERTICAL));
			return scroller;
		}
	};
}
 
Example 5
Source Project: jdk8u60   Source File: MouseComboBoxTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static Point getItemPointToClick(final int item) throws Exception {
    final Point[] result = new Point[1];

    SwingUtilities.invokeAndWait(new Runnable() {
        @Override
        public void run() {
            BasicComboPopup popup = (BasicComboPopup)comboBoxUI.getComboPopup();
            Point point = popup.getLocationOnScreen();
            Dimension size = popup.getSize();

            int step = size.height / items.length;
            point.x += size.width / 2;
            point.y += step / 2 + step * item;
            result[0] = point;
        }
    });
    return result[0];
}
 
Example 6
private static Point getItemPointToClick(final int item) throws Exception {
    final Point[] result = new Point[1];

    SwingUtilities.invokeAndWait(new Runnable() {
        @Override
        public void run() {
            BasicComboPopup popup = (BasicComboPopup)comboBoxUI.getComboPopup();
            Point point = popup.getLocationOnScreen();
            Dimension size = popup.getSize();

            int step = size.height / items.length;
            point.x += size.width / 2;
            point.y += step / 2 + step * item;
            result[0] = point;
        }
    });
    return result[0];
}
 
Example 7
private static Point getItemPointToClick(final int item) throws Exception {
    final Point[] result = new Point[1];

    SwingUtilities.invokeAndWait(new Runnable() {
        @Override
        public void run() {
            BasicComboPopup popup = (BasicComboPopup)comboBoxUI.getComboPopup();
            Point point = popup.getLocationOnScreen();
            Dimension size = popup.getSize();

            int step = size.height / items.length;
            point.x += size.width / 2;
            point.y += step / 2 + step * item;
            result[0] = point;
        }
    });
    return result[0];
}
 
Example 8
private static Point getItemPointToClick(final int item) throws Exception {
    final Point[] result = new Point[1];

    SwingUtilities.invokeAndWait(new Runnable() {
        @Override
        public void run() {
            BasicComboPopup popup = (BasicComboPopup)comboBoxUI.getComboPopup();
            Point point = popup.getLocationOnScreen();
            Dimension size = popup.getSize();

            int step = size.height / items.length;
            point.x += size.width / 2;
            point.y += step / 2 + step * item;
            result[0] = point;
        }
    });
    return result[0];
}
 
Example 9
Source Project: jdk8u-jdk   Source File: MouseComboBoxTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static Point getItemPointToClick(final int item) throws Exception {
    final Point[] result = new Point[1];

    SwingUtilities.invokeAndWait(new Runnable() {
        @Override
        public void run() {
            BasicComboPopup popup = (BasicComboPopup)comboBoxUI.getComboPopup();
            Point point = popup.getLocationOnScreen();
            Dimension size = popup.getSize();

            int step = size.height / items.length;
            point.x += size.width / 2;
            point.y += step / 2 + step * item;
            result[0] = point;
        }
    });
    return result[0];
}
 
Example 10
Source Project: hottub   Source File: MouseComboBoxTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static Point getItemPointToClick(final int item) throws Exception {
    final Point[] result = new Point[1];

    SwingUtilities.invokeAndWait(new Runnable() {
        @Override
        public void run() {
            BasicComboPopup popup = (BasicComboPopup)comboBoxUI.getComboPopup();
            Point point = popup.getLocationOnScreen();
            Dimension size = popup.getSize();

            int step = size.height / items.length;
            point.x += size.width / 2;
            point.y += step / 2 + step * item;
            result[0] = point;
        }
    });
    return result[0];
}
 
Example 11
Source Project: jdk8u_jdk   Source File: MouseComboBoxTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static Point getItemPointToClick(final int item) throws Exception {
    final Point[] result = new Point[1];

    SwingUtilities.invokeAndWait(new Runnable() {
        @Override
        public void run() {
            BasicComboPopup popup = (BasicComboPopup)comboBoxUI.getComboPopup();
            Point point = popup.getLocationOnScreen();
            Dimension size = popup.getSize();

            int step = size.height / items.length;
            point.x += size.width / 2;
            point.y += step / 2 + step * item;
            result[0] = point;
        }
    });
    return result[0];
}
 
Example 12
Source Project: swing-material   Source File: MaterialComboBox.java    License: MIT License 6 votes vote down vote up
public MaterialComboBox() {
    setModel(new DefaultComboBoxModel<T>());
    setRenderer(new FieldRenderer<T>(this));
    setUI(new BasicComboBoxUI() {
        @Override
        protected ComboPopup createPopup() {
            BasicComboPopup popup = new Popup(comboBox);
            popup.getAccessibleContext().setAccessibleParent(comboBox);
            return popup;
        }

        @Override
        protected JButton createArrowButton() {
            JButton button = new javax.swing.plaf.basic.BasicArrowButton(
                    javax.swing.plaf.basic.BasicArrowButton.SOUTH,
                    MaterialColor.TRANSPARENT,
                    MaterialColor.TRANSPARENT,
                    MaterialColor.TRANSPARENT,
                    MaterialColor.TRANSPARENT);
            button.setName("ComboBox.arrowButton");
            return button;
        }
    });
    setOpaque(false);
    setBackground(MaterialColor.TRANSPARENT);
}
 
Example 13
/**
 * Creates a grayed out version of the loading gif that can be used in a combobox through an image observer
 *
 * @since 9.2.0
 */
private static ImageIcon makeLoadingIcon(JComboBox comboBox) {
	ImageIcon icon = new ImageIcon(LOADING_ICON);
	icon.setImageObserver((img, infoflags, x, y, w, h) -> {
		boolean isFrameOrAll = (infoflags & (ImageObserver.FRAMEBITS | ImageObserver.ALLBITS)) != 0;
		if (!comboBox.isShowing() || !isFrameOrAll) {
			return !isFrameOrAll;
		}
		if (comboBox.getSelectedIndex() == 0) {
			comboBox.repaint();
		}
		BasicComboPopup p = (BasicComboPopup) comboBox.getAccessibleContext().getAccessibleChild(0);
		JList list = p.getList();
		if (list.isShowing()) {
			list.repaint();
		}
		return true;
	});
	return icon;
}
 
Example 14
Source Project: jdk8u-jdk   Source File: MouseComboBoxTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static Point getItemPointToClick(final int item) throws Exception {
    final Point[] result = new Point[1];

    SwingUtilities.invokeAndWait(new Runnable() {
        @Override
        public void run() {
            BasicComboPopup popup = (BasicComboPopup)comboBoxUI.getComboPopup();
            Point point = popup.getLocationOnScreen();
            Dimension size = popup.getSize();

            int step = size.height / items.length;
            point.x += size.width / 2;
            point.y += step / 2 + step * item;
            result[0] = point;
        }
    });
    return result[0];
}
 
Example 15
private static Point getItemPointToClick(final int item) throws Exception {
    final Point[] result = new Point[1];

    SwingUtilities.invokeAndWait(new Runnable() {
        @Override
        public void run() {
            BasicComboPopup popup = (BasicComboPopup)comboBoxUI.getComboPopup();
            Point point = popup.getLocationOnScreen();
            Dimension size = popup.getSize();

            int step = size.height / items.length;
            point.x += size.width / 2;
            point.y += step / 2 + step * item;
            result[0] = point;
        }
    });
    return result[0];
}
 
Example 16
Source Project: Juicebox   Source File: BoundsPopupMenuListener.java    License: MIT License 6 votes vote down vote up
/**
 * Alter the bounds of the popup just before it is made visible.
 */
@Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
    @SuppressWarnings("unchecked")
    JComboBox<E> comboBox = (JComboBox<E>) e.getSource();

    if (comboBox.getItemCount() == 0) return;

    final Object child = comboBox.getAccessibleContext().getAccessibleChild(0);

    if (child instanceof BasicComboPopup) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                customizePopup((BasicComboPopup) child);
            }
        });
    }
}
 
Example 17
Source Project: Juicebox   Source File: BoundsPopupMenuListener.java    License: MIT License 6 votes vote down vote up
private void customizePopup(BasicComboPopup popup) {
    scrollPane = getScrollPane(popup);

    if (popupWider)
        popupWider(popup);

    checkHorizontalScrollBar(popup);

    //  For some reason in JDK7 the popup will not display at its preferred
    //  width unless its location has been changed from its default
    //  (ie. for normal "pop down" shift the popup and reset)

    Component comboBox = popup.getInvoker();
    Point location = comboBox.getLocationOnScreen();

    if (popupAbove) {
        int height = popup.getPreferredSize().height;
        popup.setLocation(location.x, location.y - height);
    } else {
        //int height = comboBox.getPreferredSize().height;
        //popup.setLocation(location.x, location.y + height - 1);
        //popup.setLocation(location.x, location.y + height);
        //TODO should not be hardcoded
        popup.setLocation(location.x + 5, location.y + 35);
    }
}
 
Example 18
Source Project: Juicebox   Source File: BoundsPopupMenuListener.java    License: MIT License 6 votes vote down vote up
private void popupWider(BasicComboPopup popup) {
    @SuppressWarnings("unchecked")
    JList<E> list = (JList<E>) popup.getList();

    //  Determine the maximimum width to use:
    //  a) determine the popup preferred width
    //  b) limit width to the maximum if specified
    //  c) ensure width is not less than the scroll pane width

    int popupWidth = list.getPreferredSize().width
            + 5  // make sure horizontal scrollbar doesn't appear
            + getScrollBarWidth(popup, scrollPane);

    if (maximumWidth != -1) {
        popupWidth = Math.min(popupWidth, maximumWidth);
    }

    Dimension scrollPaneSize = scrollPane.getPreferredSize();
    popupWidth = Math.max(popupWidth, scrollPaneSize.width);

    //  Adjust the width

    scrollPaneSize.width = popupWidth;
    scrollPane.setPreferredSize(scrollPaneSize);
    scrollPane.setMaximumSize(scrollPaneSize);
}
 
Example 19
Source Project: openAGV   Source File: BoundsPopupMenuListener.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Alter the bounds of the popup just before it is made visible.
 *
 * @param e The event.
 */
@Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
  JComboBox<?> comboBox = (JComboBox) e.getSource();
  if (comboBox.getItemCount() == 0) {
    return;
  }
  final Object child = comboBox.getAccessibleContext().getAccessibleChild(0);

  if (child instanceof BasicComboPopup) {
    SwingUtilities.invokeLater(() -> customizePopup((BasicComboPopup) child));
  }
}
 
Example 20
Source Project: openAGV   Source File: BoundsPopupMenuListener.java    License: Apache License 2.0 5 votes vote down vote up
protected void customizePopup(BasicComboPopup popup) {
  scrollPane = getScrollPane(popup);
  popupWider(popup);

  //  For some reason in JDK7 the popup will not display at its preferred
  //  width unless its location has been changed from its default
  //  (ie. for normal "pop down" shift the popup and reset)
  Component comboBox = popup.getInvoker();
  Point location = comboBox.getLocationOnScreen();

  int height = comboBox.getSize().height;
  popup.setLocation(location.x, location.y + height - 1);
  popup.setLocation(location.x, location.y + height);
}
 
Example 21
Source Project: openAGV   Source File: BoundsPopupMenuListener.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns the scroll pane used by the popup so its bounds can be adjusted.
 */
protected JScrollPane getScrollPane(BasicComboPopup popup) {
  JList<?> list = popup.getList();
  Container c = SwingUtilities.getAncestorOfClass(JScrollPane.class, list);

  return (JScrollPane) c;
}
 
Example 22
Source Project: openAGV   Source File: BoundsPopupMenuListener.java    License: Apache License 2.0 5 votes vote down vote up
protected int getScrollBarWidth(BasicComboPopup popup, JScrollPane scrollPane) {
  // 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.
  JComboBox<?> comboBox = (JComboBox) popup.getInvoker();

  if (comboBox.getItemCount() > comboBox.getMaximumRowCount()) {
    return scrollPane.getVerticalScrollBar().getPreferredSize().width;
  }
  else {
    return 0;
  }
}
 
Example 23
Source Project: jdk1.8-source-analysis   Source File: JPopupMenu.java    License: Apache License 2.0 5 votes vote down vote up
private void fireActiveDescendant() {
    if (JPopupMenu.this instanceof BasicComboPopup) {
        // get the popup list
        JList<?> popupList = ((BasicComboPopup)JPopupMenu.this).getList();
        if (popupList == null) {
            return;
        }

        // get the first selected item
        AccessibleContext ac = popupList.getAccessibleContext();
        AccessibleSelection selection = ac.getAccessibleSelection();
        if (selection == null) {
            return;
        }
        Accessible a = selection.getAccessibleSelection(0);
        if (a == null) {
            return;
        }
        AccessibleContext selectedItem = a.getAccessibleContext();

        // fire the event with the popup invoker as the source.
        if (selectedItem != null && invoker != null) {
            AccessibleContext invokerContext = invoker.getAccessibleContext();
            if (invokerContext != null) {
                // Check invokerContext because Component.getAccessibleContext
                // returns null. Classes that extend Component are responsible
                // for returning a non-null AccessibleContext.
                invokerContext.firePropertyChange(
                    ACCESSIBLE_ACTIVE_DESCENDANT_PROPERTY,
                    null, selectedItem);
            }
        }
    }
}
 
Example 24
Source Project: FlatLaf   Source File: FlatComboBoxUI.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected PropertyChangeListener createPropertyChangeListener() {
	return new BasicComboPopup.PropertyChangeHandler() {
		@Override
		public void propertyChange( PropertyChangeEvent e ) {
			super.propertyChange( e );

			if( e.getPropertyName() == "renderer" )
				list.setCellRenderer( new PopupListCellRenderer() );
		}
	};
}
 
Example 25
Source Project: dragonwell8_jdk   Source File: JPopupMenu.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void fireActiveDescendant() {
    if (JPopupMenu.this instanceof BasicComboPopup) {
        // get the popup list
        JList<?> popupList = ((BasicComboPopup)JPopupMenu.this).getList();
        if (popupList == null) {
            return;
        }

        // get the first selected item
        AccessibleContext ac = popupList.getAccessibleContext();
        AccessibleSelection selection = ac.getAccessibleSelection();
        if (selection == null) {
            return;
        }
        Accessible a = selection.getAccessibleSelection(0);
        if (a == null) {
            return;
        }
        AccessibleContext selectedItem = a.getAccessibleContext();

        // fire the event with the popup invoker as the source.
        if (selectedItem != null && invoker != null) {
            AccessibleContext invokerContext = invoker.getAccessibleContext();
            if (invokerContext != null) {
                // Check invokerContext because Component.getAccessibleContext
                // returns null. Classes that extend Component are responsible
                // for returning a non-null AccessibleContext.
                invokerContext.firePropertyChange(
                    ACCESSIBLE_ACTIVE_DESCENDANT_PROPERTY,
                    null, selectedItem);
            }
        }
    }
}
 
Example 26
Source Project: dragonwell8_jdk   Source File: bug4743225.java    License: GNU General Public License v2.0 5 votes vote down vote up
public static BasicComboPopup getPopup() {
    AccessibleContext c = cb.getAccessibleContext();
    for(int i = 0; i < c.getAccessibleChildrenCount(); i ++) {
        if (c.getAccessibleChild(i) instanceof BasicComboPopup) {
            return (BasicComboPopup) c.getAccessibleChild(i);
        }
    }
    throw new AssertionError("No BasicComboPopup found");
}
 
Example 27
Source Project: TencentKona-8   Source File: JPopupMenu.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void fireActiveDescendant() {
    if (JPopupMenu.this instanceof BasicComboPopup) {
        // get the popup list
        JList<?> popupList = ((BasicComboPopup)JPopupMenu.this).getList();
        if (popupList == null) {
            return;
        }

        // get the first selected item
        AccessibleContext ac = popupList.getAccessibleContext();
        AccessibleSelection selection = ac.getAccessibleSelection();
        if (selection == null) {
            return;
        }
        Accessible a = selection.getAccessibleSelection(0);
        if (a == null) {
            return;
        }
        AccessibleContext selectedItem = a.getAccessibleContext();

        // fire the event with the popup invoker as the source.
        if (selectedItem != null && invoker != null) {
            AccessibleContext invokerContext = invoker.getAccessibleContext();
            if (invokerContext != null) {
                // Check invokerContext because Component.getAccessibleContext
                // returns null. Classes that extend Component are responsible
                // for returning a non-null AccessibleContext.
                invokerContext.firePropertyChange(
                    ACCESSIBLE_ACTIVE_DESCENDANT_PROPERTY,
                    null, selectedItem);
            }
        }
    }
}
 
Example 28
Source Project: TencentKona-8   Source File: bug4743225.java    License: GNU General Public License v2.0 5 votes vote down vote up
public static BasicComboPopup getPopup() {
    AccessibleContext c = cb.getAccessibleContext();
    for(int i = 0; i < c.getAccessibleChildrenCount(); i ++) {
        if (c.getAccessibleChild(i) instanceof BasicComboPopup) {
            return (BasicComboPopup) c.getAccessibleChild(i);
        }
    }
    throw new AssertionError("No BasicComboPopup found");
}
 
Example 29
Source Project: jdk8u60   Source File: JPopupMenu.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void fireActiveDescendant() {
    if (JPopupMenu.this instanceof BasicComboPopup) {
        // get the popup list
        JList<?> popupList = ((BasicComboPopup)JPopupMenu.this).getList();
        if (popupList == null) {
            return;
        }

        // get the first selected item
        AccessibleContext ac = popupList.getAccessibleContext();
        AccessibleSelection selection = ac.getAccessibleSelection();
        if (selection == null) {
            return;
        }
        Accessible a = selection.getAccessibleSelection(0);
        if (a == null) {
            return;
        }
        AccessibleContext selectedItem = a.getAccessibleContext();

        // fire the event with the popup invoker as the source.
        if (selectedItem != null && invoker != null) {
            AccessibleContext invokerContext = invoker.getAccessibleContext();
            if (invokerContext != null) {
                // Check invokerContext because Component.getAccessibleContext
                // returns null. Classes that extend Component are responsible
                // for returning a non-null AccessibleContext.
                invokerContext.firePropertyChange(
                    ACCESSIBLE_ACTIVE_DESCENDANT_PROPERTY,
                    null, selectedItem);
            }
        }
    }
}
 
Example 30
Source Project: jdk8u60   Source File: bug4743225.java    License: GNU General Public License v2.0 5 votes vote down vote up
public static BasicComboPopup getPopup() {
    AccessibleContext c = cb.getAccessibleContext();
    for(int i = 0; i < c.getAccessibleChildrenCount(); i ++) {
        if (c.getAccessibleChild(i) instanceof BasicComboPopup) {
            return (BasicComboPopup) c.getAccessibleChild(i);
        }
    }
    throw new AssertionError("No BasicComboPopup found");
}