Results 1 to 2 of 2

Thread: Dran and Drop

  1. #1
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Rep Power

    Default Drag and Drop

    I have two JLists. I want to be able to drag a selection from one into the other and then do stuff with the indices that have been dragged over. For example, I drag the value at index 1 from myFirstJList to mySecondJList and then, for simplicities sake, print out "1"...

    This is what I tried based on some example from the Java tutorials:

    Java Code:
    myFirstJList.setTransferHandler(new TransferHandler() {
    	protected Transferable createTransferable(JComponent c) {
    		int[] indices = ((JList) c).getSelectedIndices();
    		String text = "";
    		for (int i = 0; i < indices.length; i++) {
    			text += indices[i];
    		return new StringSelection(text);
    	public int getSourceActions(JComponent c) {
    		return COPY_OR_MOVE;
    With this I can drag a cell or cells from the JList into a JTextArea or something and it will paste the cell indices. It doesn't work if I try to drag into another JList - it shows the "drag not supported" sign thingy... I didn't include the importData method because it didn't seem to be firing anyway... I know that I include include the code I want in the createTransferable method above, but it fires as soon as you start dragging, and I only want it to happen after the drop obviously.

    NOTE 2: I cut down the code to make it as simple a possible, so I could see exactly what was happening.

    To sum up:
    1) Why will it work with a JTextArea but not a JList?
    2) How I can I choose what happens at the drop? As I said, the importData method doesn't seem to fire at all...
    Last edited by carderne; 08-30-2009 at 08:27 AM.

  2. #2
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Rep Power


    And I've tried making a TransferHandler for the receiving object but then it won't let me drop there with a JList or even a JTextArea...

Similar Threads

  1. drop down value not setting in IE7
    By radhikavk in forum New To Java
    Replies: 0
    Last Post: 04-18-2009, 07:59 AM
  2. Replies: 1
    Last Post: 04-01-2009, 10:28 AM
  3. drop down menu
    By mike eriksen in forum AWT / Swing
    Replies: 2
    Last Post: 06-19-2008, 07:33 AM
  4. How to use a drop-down box in Struts
    By piterskiy74 in forum Web Frameworks
    Replies: 1
    Last Post: 02-13-2008, 12:42 PM
  5. Drop down menu
    By BenNeiderlander in forum New To Java
    Replies: 3
    Last Post: 02-05-2008, 08:35 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts