Example usage for javax.swing JComboBox getAccessibleContext

List of usage examples for javax.swing JComboBox getAccessibleContext

Introduction

In this page you can find the example usage for javax.swing JComboBox getAccessibleContext.

Prototype

@BeanProperty(bound = false)
public AccessibleContext getAccessibleContext() 

Source Link

Document

Gets the AccessibleContext associated with this JComboBox.

Usage

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();/*  ww  w.  j a va2  s .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() {
    JComboBox jc = new JComboBox();
    jc.addItem("France");
    jc.addItem("Germany");
    jc.addItem("Italy");
    jc.addItem("Japan");
    jc.addItemListener(this);
    add(jc);/*ww  w .j  a  va  2s .  co m*/

    AccessibleContext ac = jc.getAccessibleContext();
}

From source file:Main.java

public Main() {
    String[] items = { "Item1", "Item2", "Item3", "Item4", "Item5" };
    JComboBox<String> comboBox = new JComboBox<>(items);
    add(comboBox);//w w  w .j  a  v a  2  s .c  om

    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:com.eviware.soapui.support.components.SimpleForm.java

public JComboBox appendComboBox(String label, ComboBoxModel model, String tooltip) {
    JComboBox comboBox = new JComboBox(model);
    comboBox.setToolTipText(StringUtils.defaultIfEmpty(tooltip, null));
    comboBox.getAccessibleContext().setAccessibleDescription(tooltip);
    append(label, comboBox);//ww  w . j  av  a2s. c  o m
    return comboBox;
}

From source file:com.eviware.soapui.support.components.SimpleForm.java

public JComboBox appendComboBox(String label, Object[] values, String tooltip) {
    JComboBox comboBox = new JComboBox(values);
    comboBox.setToolTipText(StringUtils.defaultIfEmpty(tooltip, null));
    comboBox.getAccessibleContext().setAccessibleDescription(tooltip);
    append(label, comboBox);//w  ww. ja v a  2 s  . c  o m
    return comboBox;
}

From source file:org.eclipse.jubula.rc.swing.tester.adapter.JComboBoxAdapter.java

/**
 * Tries to find the popup menu from the combobox
 * @param component the combobox/*from  ww  w .  j av  a  2 s . co m*/
 * @return the popup of the combobox
 * @throws StepExecutionException if the popup could not be found
 */
private JPopupMenu getPopupMenu(JComboBox component) throws StepExecutionException {

    AccessibleContext ac = component.getAccessibleContext();
    for (int i = 0; i < ac.getAccessibleChildrenCount(); i++) {
        Accessible a = ac.getAccessibleChild(i);
        if (a instanceof JPopupMenu) {
            return (JPopupMenu) a;
        }
    }
    throw new StepExecutionException("cannot find dropdown list", //$NON-NLS-1$
            EventFactory.createActionError(TestErrorEvent.DROPDOWN_LIST_NOT_FOUND));
}

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&#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.//from   w  w w  .j  a  va2  s  .c om
 * 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);
    }
}