public void mouseEntered(MouseEvent evt) { if (list != null) { TransferHandler th1 = getFileChooser().getTransferHandler(); TransferHandler th2 = list.getTransferHandler(); if (th1 != th2) { list.setTransferHandler(th1); } if (getFileChooser().getDragEnabled() != list.getDragEnabled()) { list.setDragEnabled(getFileChooser().getDragEnabled()); } } }
public ExampleField(ExampleOption option) { super(option); model = new DefaultListModel(); model.addListDataListener( new ListDataListener() { @Override public void intervalAdded(ListDataEvent e) { fireChangeEvent(); } @Override public void intervalRemoved(ListDataEvent e) { fireChangeEvent(); } @Override public void contentsChanged(ListDataEvent e) { fireChangeEvent(); } }); final JList list = new JList(model) { @Override public Dimension getPreferredScrollableViewportSize() { return new Dimension(3 * CELL_SIZE, 2 * CELL_SIZE); } }; list.setLayoutOrientation(JList.HORIZONTAL_WRAP); list.setVisibleRowCount(0); list.setFixedCellHeight(CELL_SIZE); list.setFixedCellWidth(CELL_SIZE); list.setTransferHandler( new URIImportTransferHandler() { @Override public boolean canImport(TransferSupport support) { support.setShowDropLocation(false); return super.canImport(support); } @Override public boolean importData(TransferSupport support) { try { List<BufferedImage> images = new ArrayList<BufferedImage>(); for (URI u : getURIs(support)) { try { images.add(ImageIO.read(u.toURL())); } catch (IOException e) { } } addExamples(images); return true; } catch (IOException e) { return false; } catch (UnsupportedFlavorException e) { return false; } catch (URISyntaxException e) { return false; } } }); list.setCellRenderer( new DefaultListCellRenderer() { @Override public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); Example example = (Example) value; setHorizontalAlignment(SwingConstants.CENTER); setText(null); setIcon(example.getIcon()); setBorder(BorderFactory.createEmptyBorder()); return this; } }); JScrollPane jsp = new JScrollPane(list); jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); final JButton remove = new JButton("Remove"); remove.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int[] indices = list.getSelectedIndices(); for (int i = indices.length - 1; i >= 0; i--) { model.remove(indices[i]); } } }); list.addListSelectionListener( new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent e) { remove.setEnabled(list.getSelectedIndex() != -1); } }); remove.setEnabled(false); panel = new JPanel(new GridBagLayout()); // Add list GridBagConstraints c = new GridBagConstraints(); c.gridx = 0; c.gridy = 0; c.weightx = 1; c.weighty = 1; c.fill = GridBagConstraints.BOTH; panel.add(jsp, c); // Add remove button c = new GridBagConstraints(); c.gridx = 0; c.anchor = GridBagConstraints.WEST; panel.add(remove, c); }