Example usage for javax.swing JComboBox addKeyListener

List of usage examples for javax.swing JComboBox addKeyListener

Introduction

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

Prototype

public synchronized void addKeyListener(KeyListener l) 

Source Link

Document

Adds the specified key listener to receive key events from this component.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String[] items = { "A", "A", "B", "B", "C" };
    JComboBox cb = new JComboBox(items);

    cb.addKeyListener(new MyKeyListener());
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String[] items = { "A", "A", "B", "B", "C" };
    JComboBox cb = new JComboBox(items);

    // Create and register the key listener
    cb.addKeyListener(new MyKeyListener());

}

From source file:imageuploader.ImgWindow.java

private void setExplicitSelectionManager(JComboBox comboBox) {

    class ExplicitSelectionManager implements KeyListener, FocusListener {

        private JComboBox src;
        private KeyListener superKeyListener;

        ExplicitSelectionManager(JComboBox src) {
            this.src = src;

            //   we like what the default key listener does, but not the action command
            // it uses for ActionEvents it fires for plain text type-ahead characters
            this.superKeyListener = src.getKeyListeners()[0]; // we only have one
            src.removeKeyListener(superKeyListener); // will be replace right away, below
        }/*  w w w  . jav a  2  s  .c om*/

        @Override
        public void keyTyped(KeyEvent e) {
            // basic combo box has no code in keyTyped
        }

        @Override
        public void keyPressed(KeyEvent e) {

            //   in the default JComboBox implementation, the KeySelectionManager is
            // called from keyPressed. I'm fine with the implementation of
            // the default, but I don't want it firing ActionEvents that will cause
            // model updates
            src.setActionCommand("comboBoxMovement");
            this.superKeyListener.keyPressed(e);
            src.setActionCommand("comboBoxChanged");

            if (e.getKeyCode() == 10) {
                src.setSelectedIndex(src.getSelectedIndex());
            }
        }

        @Override
        public void keyReleased(KeyEvent e) {
            // basic combo box has no code in keyReleased
        }

        @Override
        public void focusGained(FocusEvent e) {
        }

        @Override
        //  this will also give us the event we want, if the user decides to Tab out of
        // the combo box, instead of hitting Enter
        public void focusLost(FocusEvent e) {
            src.setSelectedIndex(src.getSelectedIndex());
        }

    }

    ExplicitSelectionManager newSelectionManager = new ExplicitSelectionManager(comboBox);

    comboBox.addKeyListener(newSelectionManager);
    comboBox.addFocusListener(newSelectionManager);

}

From source file:org.openmicroscopy.shoola.agents.fsimporter.chooser.ImportDialog.java

/**
 * Initializes the components composing the display.
 * /*from  ww  w  .ja va 2  s  .c  o  m*/
 * @param filters The filters to handle.
 * @param importerAction The cancel-all-imports action.
 */
private void initComponents(FileFilter[] filters, ImporterAction importerAction) {
    canvas = new QuotaCanvas();
    sizeImportLabel = new JLabel();
    diskSpacePane = new JPanel();
    diskSpacePane.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 0));
    diskSpacePane.add(UIUtilities.setTextFont(TEXT_FREE_SPACE));
    diskSpacePane.add(canvas);

    showThumbnails = new JCheckBox(TEXT_SHOW_THUMBNAILS);
    showThumbnails.setVisible(false);

    Registry registry = ImporterAgent.getRegistry();

    Boolean loadThumbnails = (Boolean) registry.lookup(LOAD_THUMBNAIL);

    if (loadThumbnails != null) {
        if (loadThumbnails.booleanValue()) {
            showThumbnails.setVisible(true);
            showThumbnails.setSelected(loadThumbnails.booleanValue());
        }
    }

    if (!isFastConnection()) // slow connection
        showThumbnails.setSelected(false);
    long groupId = -1;
    if (model.getSelectedGroup() != null)
        groupId = model.getSelectedGroup().getGroupId();
    if (groupId < 0)
        groupId = ImporterAgent.getUserDetails().getGroupId();

    locationDialog = new LocationDialog(owner, selectedContainer, type, objects, model, groupId, true);
    locationDialog.addPropertyChangeListener(this);

    int plugin = ImporterAgent.runAsPlugin();

    if (plugin == LookupNames.IMAGE_J_IMPORT || plugin == LookupNames.IMAGE_J) {
        detachedDialog = new LocationDialog(owner, selectedContainer, type, objects, model, groupId, false);
        detachedDialog.addPropertyChangeListener(this);
    }
    tagSelectionListener = new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            Object src = e.getSource();
            if (src instanceof JButton) {
                TagAnnotationData tag = tagsMap.get(src);
                if (tag != null) {
                    tagsMap.remove(src);
                    handleTagsSelection(tagsMap.values());
                }
            }
        }
    };

    tabbedPane = new JTabbedPane();
    numberOfFolders = new NumericalTextField();
    numberOfFolders.setMinimum(0);
    numberOfFolders.setText("0");
    numberOfFolders.setColumns(3);
    numberOfFolders.addPropertyChangeListener(this);
    tagsMap = new LinkedHashMap<JButton, TagAnnotationData>();

    IconManager icons = IconManager.getInstance();

    refreshFilesButton = new JButton(TEXT_REFRESH_FILES);
    refreshFilesButton.setBackground(UIUtilities.BACKGROUND);
    refreshFilesButton.setToolTipText(TOOLTIP_REFRESH_FILES);
    refreshFilesButton.setActionCommand("" + CMD_REFRESH_FILES);
    refreshFilesButton.addActionListener(this);

    tagButton = new JButton(icons.getIcon(IconManager.PLUS_12));
    UIUtilities.unifiedButtonLookAndFeel(tagButton);
    tagButton.addActionListener(this);
    tagButton.setActionCommand("" + CMD_TAG);
    tagButton.setToolTipText(TOOLTIP_ADD_TAGS);
    tagsPane = new JPanel();
    tagsPane.setLayout(new BoxLayout(tagsPane, BoxLayout.Y_AXIS));

    overrideName = new JCheckBox(TEXT_OVERRIDE_FILE_NAMING);
    overrideName.setToolTipText(UIUtilities.formatToolTipText(WARNING));
    overrideName.setSelected(true);
    ButtonGroup group = new ButtonGroup();
    fullName = new JRadioButton(TEXT_NAMING_FULL_PATH);
    group.add(fullName);
    partialName = new JRadioButton();
    partialName.setText(TEXT_NAMING_PARTIAL_PATH);
    partialName.setSelected(true);
    group.add(partialName);

    table = new FileSelectionTable(this);
    table.addPropertyChangeListener(this);
    chooser = new GenericFileChooser();
    JList list = (JList) UIUtilities.findComponent(chooser, JList.class);
    KeyAdapter ka = new KeyAdapter() {

        /**
         * Adds the files to the import queue.
         * 
         * @see KeyListener#keyPressed(KeyEvent)
         */
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                handleEnterKeyPressed(e.getSource());
            }
        }
    };
    if (list != null)
        list.addKeyListener(ka);
    if (list == null) {
        JTable t = (JTable) UIUtilities.findComponent(chooser, JTable.class);
        if (t != null)
            t.addKeyListener(ka);
    }

    try {
        File f = UIUtilities.getDefaultFolder();
        if (f != null)
            chooser.setCurrentDirectory(f);
    } catch (Exception e) {
        // Ignore: could not set the default container
    }

    chooser.addPropertyChangeListener(this);
    chooser.setMultiSelectionEnabled(true);
    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    chooser.setControlButtonsAreShown(false);
    chooser.setApproveButtonText(TEXT_IMPORT);
    chooser.setApproveButtonToolTipText(TOOLTIP_IMPORT);

    bioFormatsFileFilters = new ArrayList<FileFilter>();

    if (filters != null) {
        chooser.setAcceptAllFileFilterUsed(false);

        for (FileFilter fileFilter : filters) {
            if (fileFilter instanceof ComboFileFilter) {
                bioFormatsFileFiltersCombined = fileFilter;

                ComboFileFilter comboFilter = (ComboFileFilter) fileFilter;
                FileFilter[] extensionFilters = comboFilter.getFilters();

                for (FileFilter combinedFilter : extensionFilters) {
                    bioFormatsFileFilters.add(combinedFilter);
                }
                break;
            }
        }

        chooser.addChoosableFileFilter(bioFormatsFileFiltersCombined);

        for (FileFilter fileFilter : bioFormatsFileFilters) {
            chooser.addChoosableFileFilter(fileFilter);
        }

        chooser.setFileFilter(bioFormatsFileFiltersCombined);
    } else {
        chooser.setAcceptAllFileFilterUsed(true);
    }

    closeButton = new JButton(TEXT_CLOSE);
    closeButton.setToolTipText(TOOLTIP_CLOSE);
    closeButton.setActionCommand("" + CMD_CLOSE);
    closeButton.addActionListener(this);

    cancelImportButton = new JButton(importerAction);
    importerAction.setEnabled(false);

    importButton = new JButton(TEXT_IMPORT);
    importButton.setToolTipText(TOOLTIP_IMPORT);
    importButton.setActionCommand("" + CMD_IMPORT);
    importButton.addActionListener(this);
    importButton.setEnabled(false);

    pixelsSize = new ArrayList<NumericalTextField>();
    NumericalTextField field;
    for (int i = 0; i < 3; i++) {
        field = new NumericalTextField();
        field.setNumberType(Double.class);
        field.setColumns(2);
        pixelsSize.add(field);
    }

    List<Component> boxes = UIUtilities.findComponents(chooser, JComboBox.class);
    if (boxes != null) {
        JComboBox box;
        JComboBox filterBox = null;
        Iterator<Component> i = boxes.iterator();
        while (i.hasNext()) {
            box = (JComboBox) i.next();
            Object o = box.getItemAt(0);
            if (o instanceof FileFilter) {
                filterBox = box;
                break;
            }
        }
        if (filterBox != null) {
            filterBox.addKeyListener(new KeyAdapter() {

                public void keyPressed(KeyEvent e) {
                    String value = KeyEvent.getKeyText(e.getKeyCode());
                    JComboBox box = (JComboBox) e.getSource();
                    int n = box.getItemCount();
                    FileFilter filter;
                    FileFilter selectedFilter = null;
                    String d;
                    for (int j = 0; j < n; j++) {
                        filter = (FileFilter) box.getItemAt(j);
                        d = filter.getDescription();
                        if (d.startsWith(value)) {
                            selectedFilter = filter;
                            break;
                        }
                    }
                    if (selectedFilter != null)
                        box.setSelectedItem(selectedFilter);
                }
            });
        }
    }
}