List of usage examples for javax.swing.plaf.basic BasicComboPopup getList
public JList<Object> getList()
From source file:Main.java
static ImageIcon makeImageIcon(URL url, JComboBox combo, int row) { ImageIcon icon = new ImageIcon(url); icon.setImageObserver(new ImageObserver() { public boolean imageUpdate(Image img, int infoflags, int x, int y, int w, int h) { if (combo.isShowing() && (infoflags & (FRAMEBITS | ALLBITS)) != 0) { if (combo.getSelectedIndex() == row) { combo.repaint();/* w w w. j a v a 2s . c o m*/ } BasicComboPopup p = (BasicComboPopup) combo.getAccessibleContext().getAccessibleChild(0); JList list = p.getList(); if (list.isShowing()) { list.repaint(list.getCellBounds(row, row)); } } return (infoflags & (ALLBITS | ABORT)) == 0; }; }); return icon; }
From source file:Main.java
public Main() { String[] items = { "Item1", "Item2", "Item3", "Item4", "Item5" }; JComboBox<String> comboBox = new JComboBox<>(items); add(comboBox);/* ww w.j av a 2 s . c o m*/ comboBox.addPopupMenuListener(new PopupMenuListener() { public void popupMenuWillBecomeVisible(PopupMenuEvent e) { JComboBox<String> comboBox = (JComboBox<String>) e.getSource(); BasicComboPopup popup = (BasicComboPopup) comboBox.getAccessibleContext().getAccessibleChild(0); JList list = popup.getList(); list.setSelectedIndex(2); } public void popupMenuCanceled(PopupMenuEvent e) { } public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { } }); }
From source file:Main.java
public Main() { comboBox = new JComboBox(new String[] { "Select Pet", "Bird", "Cat", "Dog", "Rabbit", "Pig", "Other" }); add(comboBox, BorderLayout.PAGE_START); JFrame frame = new JFrame("Main"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(comboBox);/* w w w . j a va2 s . c o m*/ frame.pack(); frame.setVisible(true); comboBox.showPopup(); Object child = comboBox.getAccessibleContext().getAccessibleChild(0); BasicComboPopup popup = (BasicComboPopup) child; popup.setName("BasicComboPopup"); JList list = popup.getList(); Container c = SwingUtilities.getAncestorOfClass(JScrollPane.class, list); JScrollPane scrollPane = (JScrollPane) c; Window mainFrame = SwingUtilities.windowForComponent(comboBox); System.out.println(mainFrame.getName()); Window popupWindow = SwingUtilities.windowForComponent(popup); System.out.println(popupWindow); Window popupWindowa = SwingUtilities.windowForComponent(c); System.out.println(popupWindowa); Window mainFrame1 = SwingUtilities.getWindowAncestor(comboBox); System.out.println(mainFrame1); Window popupWindow1 = SwingUtilities.getWindowAncestor(popup); System.out.println(popupWindow1); Component mainFrame2 = SwingUtilities.getRoot(comboBox); System.out.println(mainFrame2.getName()); Component popupWindow2 = SwingUtilities.getRoot(popup); System.out.println(popupWindow2); if (popupWindow != mainFrame) { popupWindow.pack(); } }
From source file:org.yccheok.jstock.gui.Utils.java
/** * Adjust popup for combo box, so that horizontal scrollbar will not display. * http://forums.oracle.com/forums/thread.jspa?messageID=8037483� * http://www.camick.com/java/source/BoundsPopupMenuListener.java * * Update : According to https://forums.oracle.com/forums/thread.jspa?messageID=9789603#9789603 * , the above techniques is longer workable. * ========================================================================= * 6u25 changed when popupMenuWillBecomeVisible is called: it is now called * before the list is created so you can add items in that method and still * have the list size correctly./*w w w.j a v a 2 s . co m*/ * See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4743225 * So for your workaround: either it isn't needed anymore or you need to add * an extra hierarchy listener to check when the list is actually added. * ========================================================================= * * I use a quick hack from * http://javabyexample.wisdomplug.com/java-concepts/34-core-java/59-tips-and-tricks-for-jtree-jlist-and-jcombobox-part-i.html * * @param comboBox The combo box */ public static void adjustPopupWidth(JComboBox comboBox) { if (comboBox.getItemCount() == 0) return; Object comp = comboBox.getAccessibleContext().getAccessibleChild(0); if (!(comp instanceof BasicComboPopup)) { return; } BasicComboPopup popup = (BasicComboPopup) comp; JList list = popup.getList(); JScrollPane scrollPane = getScrollPane(popup); // Just to be paranoid enough. if (list == null || scrollPane == null) { return; } // 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(comboBox, scrollPane); Dimension scrollPaneSize = scrollPane.getPreferredSize(); //popupWidth = Math.max(popupWidth, scrollPaneSize.width); // Use comboBox.getSize(), since we realize under Linux's Java 6u25, // After expanding, scrollPane.getPreferredSize() will return expanded // size in the 2nd round, although no expand is required. popupWidth = Math.max(popupWidth, comboBox.getSize().width); // Adjust the width scrollPaneSize.width = popupWidth; scrollPane.setPreferredSize(scrollPaneSize); scrollPane.setMaximumSize(scrollPaneSize); // The above workaround is no longer working. Use the below hack code! if (comboBox instanceof JComboBoxPopupAdjustable) { ((JComboBoxPopupAdjustable) comboBox).setPopupWidth(popupWidth); } }
From source file:org.yccheok.jstock.gui.Utils.java
private static JScrollPane getScrollPane(BasicComboPopup popup) { JList list = popup.getList(); Container c = SwingUtilities.getAncestorOfClass(JScrollPane.class, list); return (JScrollPane) c; }