java - Implementing Drag And Drop on JTable to custom JComponent -


the program wanted solve problem first complex, decided write sample program solve drag , drop problem.

so have written program jtable, filled integers 0 100. in second colum, theres column1*2 displayed. table has dragenabled.

on same jpanel there's jtextarea onto rows can dragged.

if implement drag , drop mechanism myself, understand process enough implement in other application.

my problem dont know start. i've read lot in web (on official java tutorials too) wasn't able understand it. questions include:

  • do need have custom transferhandler class?
  • when implement interfaces droptargetlistener , transferhandler?

which method have override "send" data , 1 "receive" it?


edit


hi guys

sorry did not post code on first advance. because did not work thaught it's rubbish , not useful here. but:

heres code:

    import javax.swing.swingutilities;   public class main {  /**  * @param args  */ public static void main(string[] args) {     swingutilities.invokelater(new runnable() {          @override         public void run() {             new windows();          }     }); }  }      import java.util.arraylist;  import javax.swing.table.abstracttablemodel;   public class mytable extends abstracttablemodel {      private arraylist<tabledata> data;      public mytable(){         data = new arraylist<>();         (int i=0; i<100; i++){             data.add(new tabledata());         }     }      public tabledata gettabledata(int index){         return data.get(index);     }       @override     public int getcolumncount() {         return 2;     }      @override     public int getrowcount() {         return data.size();     }      @override     public object getvalueat(int arg0, int arg1) {         if (arg1 == 0){             return data.get(arg0).getvalue();         }         return data.get(arg0).getdoublevalue();     }      }      import java.awt.datatransfer.dataflavor; import java.awt.datatransfer.transferable; import java.awt.datatransfer.unsupportedflavorexception; import java.io.ioexception;  import javax.swing.jcomponent; import javax.swing.jtable; import javax.swing.transferhandler; import javax.swing.transferhandler.transfersupport;   public class mytransferhandler extends transferhandler {  public mytransferhandler(){     super(); }  @override public int getsourceactions(jcomponent c){     return copy; }  @override public transferable createtransferable(jcomponent c){     return null; //i have absolutely no idea how selected row out of jtable??? }  @override public void exportdone(jcomponent c, transferable t, int action){ }  public boolean canimport(transfersupport t){     return true; }  @override public boolean importdata(transfersupport t){     return true; }  }      import java.awt.datatransfer.dataflavor; import java.awt.datatransfer.transferable; import java.awt.datatransfer.unsupportedflavorexception; import java.io.ioexception;  public class tabledata implements transferable{         public static dataflavor flavor = new dataflavor(tabledata.class, "tabledata");          private static int history=0;         private int value;         private int doublevalue;          public tabledata(){             value = history;             doublevalue = history*2;             history++;         }          public int getvalue(){             return value;         }          public int getdoublevalue(){             return doublevalue;         }          @override         public object gettransferdata(dataflavor flavor){             return this;         }          @override         public dataflavor[] gettransferdataflavors() {             return new dataflavor[]{flavor};         }          @override         public boolean isdataflavorsupported(dataflavor flavor) {             return flavor.equals(flavor);         }     }      import java.awt.gridlayout;  import javax.swing.boxlayout; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.jtable; import javax.swing.jtextarea; import javax.swing.table.tablemodel;   public class windows extends jframe {  public windows(){     jpanel panel = new jpanel();     panel.setlayout(new gridlayout());     jtable table = new jtable(new mytable());     table.setdragenabled(true);      jscrollpane scrollpane = new jscrollpane(table);     panel.add(scrollpane);      jtextarea textarea = new jtextarea();     panel.add(textarea);        this.add(panel);     this.pack();     this.setvisible(true); }  } 

okay, managed solve issue myself. here how got work:

i implemented transferable in tabledata , wrote 2 transferhandler. 1 exporting data , 1 importing data.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -