java - How to swap value of cell in jtable by drag and drop -
i swap info of table 1 table in same column (note: have 2 columns).
my problem cannot swap value. also, want swapping enabled on same column, otherwise, table values reset original values.
here code:
jtable table_1 = new jtable(model); table_1.setpreferredscrollableviewportsize(new dimension(300, 120)); table_1.setdragenabled(true); table_1.setdropmode(dropmode.use_selection); table_1.settransferhandler(new transferhelper()); table_1.setrowselectionallowed(false); table_1.setcellselectionenabled(true); my transferhelper class:
class transferhelper extends transferhandler { private static final long serialversionuid = 1l; public transferhelper() { } @override public int getsourceactions(jcomponent c) { homecoming move; } @override protected transferable createtransferable(jcomponent source) { string info = (string) ((jtable) source).getmodel().getvalueat(((jtable) source).getselectedrow(), ((jtable) source).getselectedcolumn()); homecoming new stringselection(data); } @override protected void exportdone(jcomponent source, transferable data, int action) { ((jtable) source).getmodel().setvalueat("", ((jtable) source).getselectedrow(), ((jtable) source).getselectedcolumn()); } @override public boolean canimport(transfersupport support) { homecoming true; } @override public boolean importdata(transfersupport support) { jtable jt = (jtable) support.getcomponent(); seek { jt.setvalueat(support.gettransferable().gettransferdata(dataflavor.stringflavor), jt.getselectedrow(), jt.getselectedcolumn()); } grab (unsupportedflavorexception ex) { } grab (ioexception ex) { } homecoming super.importdata(support); } }
okay, rather involved process.
drag'n'drop not simple process, it's quite complex , involved. complexity comes flexibility
swapping values in way isn't same "moving" per say. when moving something, take source , place in target, here swapping values between source , target, means need more info available via api
first of all, you're going need custom class hold info exported, because we're moving data, going require additional information, in particular, source component...
the next simple wrapper. export jtable entirely, wanted demonstrate basic concept of drag-n-drop...
import javax.swing.jtable; public class celldata { private jtable table; public celldata(jtable table) { this.table = table; } public int getcolumn() { homecoming table.getselectedcolumn(); } public string getvalue() { int row = table.getselectedrow(); int col = table.getselectedcolumn(); homecoming (string) table.getvalueat(row, col); } public jtable gettable() { homecoming table; } } next, need custom transferable manage our data...
import java.awt.datatransfer.dataflavor; import java.awt.datatransfer.transferable; import java.awt.datatransfer.unsupportedflavorexception; import java.io.ioexception; public class celldatatransferable implements transferable { public static final dataflavor cell_data_flavor = createconstant(celldata.class, "application/x-java-celldata"); private celldata celldata; public celldatatransferable(celldata celldata) { this.celldata = celldata; } @override public dataflavor[] gettransferdataflavors() { homecoming new dataflavor[]{cell_data_flavor}; } @override public boolean isdataflavorsupported(dataflavor flavor) { boolean supported = false; (dataflavor available : gettransferdataflavors()) { if (available.equals(flavor)) { supported = true; } } homecoming supported; } @override public object gettransferdata(dataflavor flavor) throws unsupportedflavorexception, ioexception { homecoming celldata; } static protected dataflavor createconstant(class clazz, string name) { seek { homecoming new dataflavor(clazz, name); } grab (exception e) { e.printstacktrace(); homecoming null; } } } and finally, transferhandler....
public class transferhelper extends transferhandler { private static final long serialversionuid = 1l; public transferhelper() { } @override public int getsourceactions(jcomponent c) { homecoming move; } @override protected transferable createtransferable(jcomponent source) { // create transferable // because i'm hacking little, i've included source table... jtable table = (jtable) source; homecoming new celldatatransferable(new celldata(table)); } @override protected void exportdone(jcomponent source, transferable data, int action) { } @override public boolean canimport(transfersupport support) { // reject import default... boolean canimport = false; // can import jtable component comp = support.getcomponent(); if (comp instanceof jtable) { jtable table = (jtable) comp; // need location drop might occur droplocation dl = support.getdroplocation(); point dp = dl.getdroppoint(); // column @ drop point int dragcolumn = table.columnatpoint(dp); seek { // transferable, need check // constraints transferable t = support.gettransferable(); celldata cd = (celldata) t.gettransferdata(celldatatransferable.cell_data_flavor); // create sure we're not dropping onto ourselves... if (cd.gettable() != table) { // columns match...? if (dragcolumn == cd.getcolumn()) { canimport = true; } } } grab (unsupportedflavorexception | ioexception ex) { ex.printstacktrace(); } } homecoming canimport; } @override public boolean importdata(transfersupport support) { // import failed reason... boolean imported = false; // import jtables... component comp = support.getcomponent(); if (comp instanceof jtable) { jtable target = (jtable) comp; // need know importing to... droplocation dl = support.getdroplocation(); point dp = dl.getdroppoint(); int dropcol = target.columnatpoint(dp); int droprow = target.rowatpoint(dp); seek { // transferable @ heart of transferable t = support.gettransferable(); celldata cd = (celldata) t.gettransferdata(celldatatransferable.cell_data_flavor); if (cd.gettable() != target) { // create sure columns match if (dropcol == cd.getcolumn()) { // info "dropped" table string exportvalue = (string) target.getvalueat(droprow, dropcol); // info "dragged" table string importvalue = cd.getvalue(); // swap values... // set target/dropped tables value target.setvalueat(importvalue, droprow, dropcol); // set source/dragged tables values jtable source = cd.gettable(); int row = source.getselectedrow(); int col = source.getselectedcolumn(); source.setvalueat(exportvalue, row, col); imported = true; } } } grab (unsupportedflavorexception | ioexception ex) { ex.printstacktrace(); } } homecoming imported; } } read comments :p
and finally, runnable example...
import java.awt.component; import java.awt.eventqueue; import java.awt.gridlayout; import java.awt.point; import java.awt.datatransfer.dataflavor; import java.awt.datatransfer.transferable; import java.awt.datatransfer.unsupportedflavorexception; import java.io.ioexception; import java.util.logging.level; import java.util.logging.logger; import javax.swing.dropmode; import javax.swing.jcomponent; import javax.swing.jframe; import javax.swing.jscrollpane; import javax.swing.jtable; import javax.swing.transferhandler; import static javax.swing.transferhandler.move; import javax.swing.transferhandler.transfersupport; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; import javax.swing.table.defaulttablemodel; public class tableswap { public static void main(string[] args) { new tableswap(); } public tableswap() { eventqueue.invokelater(new runnable() { @override public void run() { seek { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } grab (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { } jtable t1 = createtable(0); jtable t2 = createtable(20); jframe frame = new jframe("testing"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlayout(new gridlayout(0, 2)); frame.add(new jscrollpane(t1)); frame.add(new jscrollpane(t2)); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } protected jtable createtable(int startat) { defaulttablemodel model = new defaulttablemodel(0, 2); (int index = 0; index < 10; index++) { model.addrow(new object[]{"0x" + (index + startat), "1x" + (index + startat)}); } jtable table = new jtable(model); table.setdragenabled(true); table.setdropmode(dropmode.use_selection); table.settransferhandler(new transferhelper()); table.setrowselectionallowed(false); table.setcellselectionenabled(true); homecoming table; } } updated back upwards single table only
import java.awt.component; import java.awt.eventqueue; import java.awt.gridlayout; import java.awt.point; import java.awt.datatransfer.dataflavor; import java.awt.datatransfer.transferable; import java.awt.datatransfer.unsupportedflavorexception; import java.io.ioexception; import javax.swing.dropmode; import javax.swing.jcomponent; import javax.swing.jframe; import javax.swing.jscrollpane; import javax.swing.jtable; import javax.swing.transferhandler; import static javax.swing.transferhandler.move; import javax.swing.transferhandler.transfersupport; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; import javax.swing.table.defaulttablemodel; public class tableswap { public static void main(string[] args) { new tableswap(); } public tableswap() { eventqueue.invokelater(new runnable() { @override public void run() { seek { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } grab (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { } jtable t1 = createtable(0); jframe frame = new jframe("testing"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.add(new jscrollpane(t1)); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } protected jtable createtable(int startat) { defaulttablemodel model = new defaulttablemodel(0, 2); (int index = 0; index < 10; index++) { model.addrow(new object[]{"0x" + (index + startat), "1x" + (index + startat)}); } jtable table = new jtable(model); table.setdragenabled(true); table.setdropmode(dropmode.use_selection); table.settransferhandler(new transferhelper()); table.setrowselectionallowed(false); table.setcellselectionenabled(true); homecoming table; } public class celldata { private final object value; private final int col; private final jtable table; private final int row; public celldata(jtable source) { this.col = source.getselectedcolumn(); this.row = source.getselectedrow(); this.value = source.getvalueat(row, col); this.table = source; } public int getcolumn() { homecoming col; } public object getvalue() { homecoming value; } public jtable gettable() { homecoming table; } public boolean swapvalueswith(int targetrow, int targetcol) { boolean swapped = false; if (targetcol == col) { object exportvalue = table.getvalueat(targetrow, targetcol); table.setvalueat(value, targetrow, targetcol); table.setvalueat(exportvalue, row, col); swapped = true; } homecoming swapped; } } public static final dataflavor cell_data_flavor = createconstant(celldata.class, "application/x-java-celldata"); public class celldatatransferable implements transferable { private celldata celldata; public celldatatransferable(celldata celldata) { this.celldata = celldata; } @override public dataflavor[] gettransferdataflavors() { homecoming new dataflavor[]{cell_data_flavor}; } @override public boolean isdataflavorsupported(dataflavor flavor) { boolean supported = false; (dataflavor available : gettransferdataflavors()) { if (available.equals(flavor)) { supported = true; } } homecoming supported; } @override public object gettransferdata(dataflavor flavor) throws unsupportedflavorexception, ioexception { homecoming celldata; } } static protected dataflavor createconstant(class clazz, string name) { seek { homecoming new dataflavor(clazz, name); } grab (exception e) { e.printstacktrace(); homecoming null; } } public class transferhelper extends transferhandler { private static final long serialversionuid = 1l; public transferhelper() { } @override public int getsourceactions(jcomponent c) { homecoming move; } @override protected transferable createtransferable(jcomponent source) { // create transferable jtable table = (jtable) source; int row = table.getselectedrow(); int col = table.getselectedcolumn(); object value = table.getvalueat(row, col); homecoming new celldatatransferable(new celldata(table)); } @override protected void exportdone(jcomponent source, transferable data, int action) { } @override public boolean canimport(transfersupport support) { // reject import default... boolean canimport = false; // can import jtable component comp = support.getcomponent(); if (comp instanceof jtable) { jtable target = (jtable) comp; // need location drop might occur droplocation dl = support.getdroplocation(); point dp = dl.getdroppoint(); // column @ drop point int dragcolumn = target.columnatpoint(dp); seek { // transferable, need check // constraints transferable t = support.gettransferable(); celldata cd = (celldata) t.gettransferdata(cell_data_flavor); // create sure we're not dropping onto ourselves... if (cd.gettable() == target) { // columns match...? if (dragcolumn == cd.getcolumn()) { canimport = true; } } } grab (unsupportedflavorexception | ioexception ex) { ex.printstacktrace(); } } homecoming canimport; } @override public boolean importdata(transfersupport support) { // import failed reason... boolean imported = false; // import jtables... component comp = support.getcomponent(); if (comp instanceof jtable) { jtable target = (jtable) comp; // need know importing to... droplocation dl = support.getdroplocation(); point dp = dl.getdroppoint(); int dropcol = target.columnatpoint(dp); int droprow = target.rowatpoint(dp); seek { // transferable @ heart of transferable t = support.gettransferable(); celldata cd = (celldata) t.gettransferdata(cell_data_flavor); if (cd.gettable() == target) { if (cd.swapvalueswith(droprow, dropcol)) { imported = true; } } } grab (unsupportedflavorexception | ioexception ex) { ex.printstacktrace(); } } homecoming imported; } } } java swing drag-and-drop
No comments:
Post a Comment