List of usage examples for javax.swing JList setTransferHandler
@BeanProperty(hidden = true, description = "Mechanism for transfer of data to and from the component") public void setTransferHandler(TransferHandler newHandler)
From source file:DnDDemo3.java
public static void main(String[] args) { JPanel north = new JPanel(); north.add(new JLabel("Drag from here:")); JTextField field = new JTextField(10); field.setDragEnabled(true);//from w w w .ja v a 2 s. co m north.add(field); final DefaultListModel listModel = new DefaultListModel(); listModel.addElement("first"); listModel.addElement("second"); final JList list = new JList(listModel); list.setDragEnabled(true); list.setTransferHandler(new TransferHandler() { public boolean canImport(TransferHandler.TransferSupport support) { if (!support.isDataFlavorSupported(DataFlavor.stringFlavor)) { return false; } JList.DropLocation dl = (JList.DropLocation) support.getDropLocation(); if (dl.getIndex() == -1) { return false; } else { return true; } } public boolean importData(TransferHandler.TransferSupport support) { if (!canImport(support)) { return false; } Transferable transferable = support.getTransferable(); String data; try { data = (String) transferable.getTransferData(DataFlavor.stringFlavor); } catch (Exception e) { return false; } JList.DropLocation dl = (JList.DropLocation) support.getDropLocation(); int index = dl.getIndex(); if (dl.isInsert()) { listModel.add(index, data); } else { listModel.set(index, data); } Rectangle r = list.getCellBounds(index, index); list.scrollRectToVisible(r); return true; } }); JScrollPane center = new JScrollPane(); center.setViewportView(list); list.setDropMode(DropMode.USE_SELECTION); JPanel cp = new JPanel(); cp.setLayout(new BorderLayout()); cp.add(north, BorderLayout.NORTH); cp.add(center, BorderLayout.CENTER); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(cp); frame.pack(); frame.setVisible(true); }
From source file:DropModeON.java
public static void main(String[] args) { JPanel north = new JPanel(); north.add(new JLabel("Drag from here:")); JTextField field = new JTextField(10); field.setDragEnabled(true);// ww w .j a v a 2s .co m north.add(field); final DefaultListModel listModel = new DefaultListModel(); listModel.addElement("first"); listModel.addElement("second"); final JList list = new JList(listModel); list.setDragEnabled(true); list.setTransferHandler(new TransferHandler() { public boolean canImport(TransferHandler.TransferSupport support) { if (!support.isDataFlavorSupported(DataFlavor.stringFlavor)) { return false; } JList.DropLocation dl = (JList.DropLocation) support.getDropLocation(); if (dl.getIndex() == -1) { return false; } else { return true; } } public boolean importData(TransferHandler.TransferSupport support) { if (!canImport(support)) { return false; } Transferable transferable = support.getTransferable(); String data; try { data = (String) transferable.getTransferData(DataFlavor.stringFlavor); } catch (Exception e) { return false; } JList.DropLocation dl = (JList.DropLocation) support.getDropLocation(); int index = dl.getIndex(); if (dl.isInsert()) { listModel.add(index, data); } else { listModel.set(index, data); } // Scroll to display the element that was dropped Rectangle r = list.getCellBounds(index, index); list.scrollRectToVisible(r); return true; } }); JScrollPane center = new JScrollPane(); center.setViewportView(list); list.setDropMode(DropMode.ON); JPanel cp = new JPanel(); cp.setLayout(new BorderLayout()); cp.add(north, BorderLayout.NORTH); cp.add(center, BorderLayout.CENTER); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(cp); frame.pack(); frame.setVisible(true); }
From source file:DropModeON_OR_INSERT.java
public static void main(String[] args) { JPanel north = new JPanel(); north.add(new JLabel("Drag from here:")); JTextField field = new JTextField(10); field.setDragEnabled(true);/* w w w. j a v a 2s . co m*/ north.add(field); final DefaultListModel listModel = new DefaultListModel(); listModel.addElement("first"); listModel.addElement("second"); final JList list = new JList(listModel); list.setDragEnabled(true); list.setTransferHandler(new TransferHandler() { public boolean canImport(TransferHandler.TransferSupport support) { if (!support.isDataFlavorSupported(DataFlavor.stringFlavor)) { return false; } JList.DropLocation dl = (JList.DropLocation) support.getDropLocation(); if (dl.getIndex() == -1) { return false; } else { return true; } } public boolean importData(TransferHandler.TransferSupport support) { if (!canImport(support)) { return false; } Transferable transferable = support.getTransferable(); String data; try { data = (String) transferable.getTransferData(DataFlavor.stringFlavor); } catch (Exception e) { return false; } JList.DropLocation dl = (JList.DropLocation) support.getDropLocation(); int index = dl.getIndex(); if (dl.isInsert()) { listModel.add(index, data); } else { listModel.set(index, data); } // Scroll to display the element that was dropped Rectangle r = list.getCellBounds(index, index); list.scrollRectToVisible(r); return true; } }); JScrollPane center = new JScrollPane(); center.setViewportView(list); list.setDropMode(DropMode.ON_OR_INSERT); JPanel cp = new JPanel(); cp.setLayout(new BorderLayout()); cp.add(north, BorderLayout.NORTH); cp.add(center, BorderLayout.CENTER); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(cp); frame.pack(); frame.setVisible(true); }
From source file:DropModeINSERT.java
public static void main(String[] args) { JPanel north = new JPanel(); north.add(new JLabel("Drag from here:")); JTextField field = new JTextField(10); field.setDragEnabled(true);/*from w w w . j a v a 2 s . c om*/ north.add(field); final DefaultListModel listModel = new DefaultListModel(); listModel.addElement("first"); listModel.addElement("second"); final JList list = new JList(listModel); list.setDragEnabled(true); list.setTransferHandler(new TransferHandler() { public boolean canImport(TransferHandler.TransferSupport support) { if (!support.isDataFlavorSupported(DataFlavor.stringFlavor)) { return false; } JList.DropLocation dl = (JList.DropLocation) support.getDropLocation(); if (dl.getIndex() == -1) { return false; } else { return true; } } public boolean importData(TransferHandler.TransferSupport support) { if (!canImport(support)) { return false; } Transferable transferable = support.getTransferable(); String data; try { data = (String) transferable.getTransferData(DataFlavor.stringFlavor); } catch (Exception e) { return false; } JList.DropLocation dl = (JList.DropLocation) support.getDropLocation(); int index = dl.getIndex(); if (dl.isInsert()) { listModel.add(index, data); } else { listModel.set(index, data); } // Scroll to display the element that was dropped Rectangle r = list.getCellBounds(index, index); list.scrollRectToVisible(r); return true; } }); JScrollPane center = new JScrollPane(); center.setViewportView(list); list.setDropMode(DropMode.INSERT); JPanel cp = new JPanel(); cp.setLayout(new BorderLayout()); cp.add(north, BorderLayout.NORTH); cp.add(center, BorderLayout.CENTER); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(cp); frame.pack(); frame.setVisible(true); }
From source file:ListDrop.java
public ListDrop() { JFrame f = new JFrame(); JList list = new JList(model); list.setDropMode(DropMode.INSERT); list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); list.setTransferHandler(new ListHandler()); field.setDragEnabled(true);/*from www . j a v a2 s. com*/ f.setLayout(new FlowLayout()); f.add(new JScrollPane(list)); f.add(field); f.pack(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); }
From source file:dnd.ChooseDropActionDemo.java
public ChooseDropActionDemo() { super("ChooseDropActionDemo"); for (int i = 15; i >= 0; i--) { from.add(0, "Source item " + i); }/*from w ww. j av a2 s. com*/ for (int i = 2; i >= 0; i--) { copy.add(0, "Target item " + i); move.add(0, "Target item " + i); } JPanel p = new JPanel(); p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); dragFrom = new JList(from); dragFrom.setTransferHandler(new FromTransferHandler()); dragFrom.setPrototypeCellValue("List Item WWWWWW"); dragFrom.setDragEnabled(true); dragFrom.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); JLabel label = new JLabel("Drag from here:"); label.setAlignmentX(0f); p.add(label); JScrollPane sp = new JScrollPane(dragFrom); sp.setAlignmentX(0f); p.add(sp); add(p, BorderLayout.WEST); JList moveTo = new JList(move); moveTo.setTransferHandler(new ToTransferHandler(TransferHandler.COPY)); moveTo.setDropMode(DropMode.INSERT); JList copyTo = new JList(copy); copyTo.setTransferHandler(new ToTransferHandler(TransferHandler.MOVE)); copyTo.setDropMode(DropMode.INSERT); p = new JPanel(); p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); label = new JLabel("Drop to COPY to here:"); label.setAlignmentX(0f); p.add(label); sp = new JScrollPane(moveTo); sp.setAlignmentX(0f); p.add(sp); label = new JLabel("Drop to MOVE to here:"); label.setAlignmentX(0f); p.add(label); sp = new JScrollPane(copyTo); sp.setAlignmentX(0f); p.add(sp); p.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 0)); add(p, BorderLayout.CENTER); ((JPanel) getContentPane()).setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2)); getContentPane().setPreferredSize(new Dimension(320, 315)); }
From source file:ChooseDropActionDemo.java
public ChooseDropActionDemo() { super("ChooseDropActionDemo"); for (int i = 15; i >= 0; i--) { from.add(0, "Source item " + i); }// w ww . j ava 2 s .c o m for (int i = 2; i >= 0; i--) { copy.add(0, "Target item " + i); move.add(0, "Target item " + i); } JPanel p = new JPanel(); p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); dragFrom = new JList(from); dragFrom.setTransferHandler(new FromTransferHandler()); dragFrom.setPrototypeCellValue("List Item WWWWWW"); dragFrom.setDragEnabled(true); dragFrom.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); JLabel label = new JLabel("Drag from here:"); label.setAlignmentX(0f); p.add(label); JScrollPane sp = new JScrollPane(dragFrom); sp.setAlignmentX(0f); p.add(sp); add(p, BorderLayout.WEST); JList moveTo = new JList(move); moveTo.setTransferHandler(new ToTransferHandler(TransferHandler.COPY)); moveTo.setDropMode(DropMode.INSERT); JList copyTo = new JList(copy); copyTo.setTransferHandler(new ToTransferHandler(TransferHandler.MOVE)); copyTo.setDropMode(DropMode.INSERT); p = new JPanel(); p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); label = new JLabel("Drop to COPY to here:"); ; label.setAlignmentX(0f); p.add(label); sp = new JScrollPane(moveTo); sp.setAlignmentX(0f); p.add(sp); label = new JLabel("Drop to MOVE to here:"); label.setAlignmentX(0f); p.add(label); sp = new JScrollPane(copyTo); sp.setAlignmentX(0f); p.add(sp); p.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 0)); add(p, BorderLayout.CENTER); ((JPanel) getContentPane()).setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2)); getContentPane().setPreferredSize(new Dimension(320, 315)); }
From source file:ExtendedDnDDemo.java
private JPanel createList() { DefaultListModel listModel = new DefaultListModel(); listModel.addElement("List 0"); listModel.addElement("List 1"); listModel.addElement("List 2"); listModel.addElement("List 3"); listModel.addElement("List 4"); listModel.addElement("List 5"); listModel.addElement("List 6"); listModel.addElement("List 7"); listModel.addElement("List 8"); JList list = new JList(listModel); list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); JScrollPane scrollPane = new JScrollPane(list); scrollPane.setPreferredSize(new Dimension(400, 100)); list.setDragEnabled(true);//w ww .j a v a 2s . c o m list.setTransferHandler(new ListTransferHandler()); JPanel panel = new JPanel(new BorderLayout()); panel.add(scrollPane, BorderLayout.CENTER); panel.setBorder(BorderFactory.createTitledBorder("List")); return panel; }
From source file:ListCutPaste.java
public ListCutPaste() { super(new BorderLayout()); lh = new ListTransferHandler(); JPanel panel = new JPanel(new GridLayout(1, 3)); DefaultListModel list1Model = new DefaultListModel(); list1Model.addElement("alpha"); list1Model.addElement("beta"); list1Model.addElement("gamma"); list1Model.addElement("delta"); list1Model.addElement("epsilon"); list1Model.addElement("zeta"); JList list1 = new JList(list1Model); list1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); JScrollPane sp1 = new JScrollPane(list1); sp1.setPreferredSize(new Dimension(400, 100)); list1.setDragEnabled(true);/*from w w w .jav a 2s .c om*/ list1.setTransferHandler(lh); list1.setDropMode(DropMode.ON_OR_INSERT); setMappings(list1); JPanel pan1 = new JPanel(new BorderLayout()); pan1.add(sp1, BorderLayout.CENTER); pan1.setBorder(BorderFactory.createTitledBorder("Greek Alphabet")); panel.add(pan1); DefaultListModel list2Model = new DefaultListModel(); list2Model.addElement("uma"); list2Model.addElement("dois"); list2Model.addElement("tres"); list2Model.addElement("quatro"); list2Model.addElement("cinco"); list2Model.addElement("seis"); JList list2 = new JList(list2Model); list2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); list2.setDragEnabled(true); JScrollPane sp2 = new JScrollPane(list2); sp2.setPreferredSize(new Dimension(400, 100)); list2.setTransferHandler(lh); list2.setDropMode(DropMode.INSERT); setMappings(list2); JPanel pan2 = new JPanel(new BorderLayout()); pan2.add(sp2, BorderLayout.CENTER); pan2.setBorder(BorderFactory.createTitledBorder("Portuguese Numbers")); panel.add(pan2); DefaultListModel list3Model = new DefaultListModel(); list3Model.addElement("adeen"); list3Model.addElement("dva"); list3Model.addElement("tri"); list3Model.addElement("chyetirye"); list3Model.addElement("pyat"); list3Model.addElement("shest"); JList list3 = new JList(list3Model); list3.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); list3.setDragEnabled(true); JScrollPane sp3 = new JScrollPane(list3); sp3.setPreferredSize(new Dimension(400, 100)); list3.setTransferHandler(lh); list3.setDropMode(DropMode.ON); setMappings(list3); JPanel pan3 = new JPanel(new BorderLayout()); pan3.add(sp3, BorderLayout.CENTER); pan3.setBorder(BorderFactory.createTitledBorder("Russian Numbers")); panel.add(pan3); setPreferredSize(new Dimension(500, 200)); add(panel, BorderLayout.CENTER); }
From source file:DragListDemo.java
public DragListDemo() { arrayListHandler = new ArrayListTransferHandler(); JList list1, list2; DefaultListModel list1Model = new DefaultListModel(); list1Model.addElement("0 (list 1)"); list1Model.addElement("1 (list 1)"); list1Model.addElement("2 (list 1)"); list1Model.addElement("3 (list 1)"); list1Model.addElement("4 (list 1)"); list1Model.addElement("5 (list 1)"); list1Model.addElement("6 (list 1)"); list1 = new JList(list1Model); list1.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); list1.setTransferHandler(arrayListHandler); list1.setDragEnabled(true);/*from w w w. j av a 2s. c om*/ JScrollPane list1View = new JScrollPane(list1); list1View.setPreferredSize(new Dimension(200, 100)); JPanel panel1 = new JPanel(); panel1.setLayout(new BorderLayout()); panel1.add(list1View, BorderLayout.CENTER); panel1.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); DefaultListModel list2Model = new DefaultListModel(); list2Model.addElement("0 (list 2)"); list2Model.addElement("1 (list 2)"); list2Model.addElement("2 (list 2)"); list2Model.addElement("3 (list 2)"); list2Model.addElement("4 (list 2)"); list2Model.addElement("5 (list 2)"); list2Model.addElement("6 (list 2)"); list2 = new JList(list2Model); list2.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); list2.setTransferHandler(arrayListHandler); list2.setDragEnabled(true); JScrollPane list2View = new JScrollPane(list2); list2View.setPreferredSize(new Dimension(200, 100)); JPanel panel2 = new JPanel(); panel2.setLayout(new BorderLayout()); panel2.add(list2View, BorderLayout.CENTER); panel2.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); setLayout(new BorderLayout()); add(panel1, BorderLayout.LINE_START); add(panel2, BorderLayout.LINE_END); setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); }