Example usage for javax.swing JList addKeyListener

List of usage examples for javax.swing JList addKeyListener

Introduction

In this page you can find the example usage for javax.swing JList 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:org.executequery.gui.browser.FindAction.java

private JList initSearchResultsList() {

    final JList list = new JList();
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    ListCellRenderer listCellRenderer = getListCellRenderer();
    if (listCellRenderer != null) {

        list.setCellRenderer(listCellRenderer);
    }//from  w  ww . j av a  2 s  . c o  m

    list.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() >= 2) {
                listValueSelected((T) list.getSelectedValue());
            }
        }
    });

    list.addKeyListener(new KeyAdapter() {
        public void keyReleased(KeyEvent e) {
            int keyCode = e.getKeyCode();
            if (keyCode == KeyEvent.VK_ENTER) {
                listValueSelected((T) list.getSelectedValue());
            } else if (keyCode == KeyEvent.VK_BACK_SPACE) {
                searchField.requestFocus();
            }
        }
    });

    return list;
}

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

/**
 * Initializes the components composing the display.
 * //from w  w  w. ja v a2 s .com
 * @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);
                }
            });
        }
    }
}

From source file:pl.otros.vfs.browser.VfsBrowser.java

License:asdf

private JPopupMenu addPopupMenu(JList list, String... actions) {
    JPopupMenu favoritesPopupMenu = new JPopupMenu();
    for (String action : actions) {
        favoritesPopupMenu.add(list.getActionMap().get(action));
    }/*from   ww  w  .  j  ava  2  s .  c  o m*/
    list.addKeyListener(new PopupListener(favoritesPopupMenu));
    list.addMouseListener(new PopupListener(favoritesPopupMenu));
    return favoritesPopupMenu;
}