java - JTable copy and paste using Clipboard and AbstractAction -
when try paste jtable
cell using table.setvalueat()
, cell pasting in remains blank, setvalueat()
seems working. also, when try cut or copy 1 cell, paste option on jpopupmenu
remains disabled when want paste cell. i'm not sure why. code below.
class copyaction extends abstractaction { private jtable table; public copyaction(jtable table) { this.table = table; } @override public void actionperformed(actionevent e) { int row = table.getselectedrow(); int col = table.getselectedcolumn(); clipboard cb = toolkit.getdefaulttoolkit().getsystemclipboard(); cb.setcontents(new celltransferable(table.getvalueat(row, col)), null); } } class cutaction extends abstractaction { private jtable table; public cutaction(jtable table) { this.table = table; } @override public void actionperformed(actionevent e) { int row = table.getselectedrow(); int col = table.getselectedcolumn(); clipboard cb = toolkit.getdefaulttoolkit().getsystemclipboard(); cb.setcontents(new celltransferable(table.getvalueat(row, col)), null); } } class pasteaction extends abstractaction { private jtable table; private gridmodel gridmodel; public pasteaction(jtable tbl, gridmodel gm) { gridmodel = gm; table = tbl; final clipboard cb = toolkit.getdefaulttoolkit().getsystemclipboard(); cb.addflavorlistener(new flavorlistener() { @override public void flavorschanged(flavorevent e) { setenabled(cb.isdataflavoravailable(celltransferable.cell_data_flavor)); } }); setenabled(cb.isdataflavoravailable(celltransferable.cell_data_flavor)); } @override public void actionperformed(actionevent e) { int row = table.getselectedrow(); int col = table.getselectedcolumn(); clipboard cb = toolkit.getdefaulttoolkit().getsystemclipboard(); if (cb.isdataflavoravailable(celltransferable.cell_data_flavor)) { try { object value = cb.getdata(celltransferable.cell_data_flavor); system.out.println(value); table.setvalueat(value, row, col); } catch (unsupportedflavorexception | ioexception ex) { ex.printstacktrace(); } } } } class celltransferable implements transferable { public static final dataflavor cell_data_flavor = new dataflavor(object.class, "application/x-cell-value"); private object cellvalue; public celltransferable(object cellvalue) { this.cellvalue = cellvalue; } @override public dataflavor[] gettransferdataflavors() { return new dataflavor[]{cell_data_flavor}; } @override public boolean isdataflavorsupported(dataflavor flavor) { return cell_data_flavor.equals(flavor); } @override public object gettransferdata(dataflavor flavor) throws unsupportedflavorexception, ioexception { if (!isdataflavorsupported(flavor)) { throw new unsupportedflavorexception(flavor); } return cellvalue; } }
here's code setvalueat()
class mymodel extends abstracttablemodel { private string[] names = {"1", "2", "3", "4", "5" }; private string[][] values = new string[5][5]; //...other methods public void setvalueat(object value, int row, int col) { if (value instanceof double || value instanceof integer) values[row][col] = value.tostring(); else { values[row][col] = (string) value; } firetablecellupdated(row, col); } }
based on example code , having fill in blanks, works me...
import java.awt.borderlayout; import java.awt.eventqueue; import java.awt.point; import java.awt.toolkit; import java.awt.datatransfer.clipboard; import java.awt.datatransfer.dataflavor; import java.awt.datatransfer.flavorevent; import java.awt.datatransfer.flavorlistener; import java.awt.datatransfer.transferable; import java.awt.datatransfer.unsupportedflavorexception; import java.awt.event.actionevent; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import java.io.ioexception; import javax.swing.abstractaction; import javax.swing.jframe; import javax.swing.jpopupmenu; import javax.swing.jtable; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; import javax.swing.table.abstracttablemodel; public class tableexample { public static void main(string[] args) { new tableexample(); } public tableexample() { eventqueue.invokelater(new runnable() { @override public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { } jtable table = new jtable(new mymodel()); final jpopupmenu pm = new jpopupmenu(); pm.add(new copyaction(table)); pm.add(new pasteaction(table)); table.addmouselistener(new mouseadapter() { @override public void mouseclicked(mouseevent e) { if (e.ispopuptrigger()) { highlightrow(e); dopopup(e); } } @override public void mousereleased(mouseevent e) { if (e.ispopuptrigger()) { highlightrow(e); dopopup(e); } } protected void dopopup(mouseevent e) { pm.show(e.getcomponent(), e.getx(), e.gety()); } protected void highlightrow(mouseevent e) { jtable table = (jtable) e.getsource(); point point = e.getpoint(); int row = table.rowatpoint(point); int col = table.columnatpoint(point); table.setrowselectioninterval(row, row); table.setcolumnselectioninterval(col, col); } }); jframe frame = new jframe("testing"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlayout(new borderlayout()); frame.add(table); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } public class mymodel extends abstracttablemodel { private string[] names = {"1", "2", "3", "4", "5"}; private string[][] values = new string[5][5]; public mymodel() { values = new string[10][names.length]; (int row = 0; row < 10; row++) { (int col = 0; col < names.length; col++) { values[row][col] = string.valueof((char) ((row * names.length) + col + 65)); } } } @override public void setvalueat(object value, int row, int col) { if (value instanceof double || value instanceof integer) { values[row][col] = value.tostring(); } else { values[row][col] = (string) value; } firetablecellupdated(row, col); } @override public int getrowcount() { return values.length; } @override public int getcolumncount() { return names.length; } @override public object getvalueat(int rowindex, int columnindex) { return values[rowindex][columnindex]; } } class copyaction extends abstractaction { private jtable table; public copyaction(jtable table) { this.table = table; putvalue(name, "copy"); } @override public void actionperformed(actionevent e) { int row = table.getselectedrow(); int col = table.getselectedcolumn(); clipboard cb = toolkit.getdefaulttoolkit().getsystemclipboard(); cb.setcontents(new celltransferable(table.getvalueat(row, col)), null); } } class cutaction extends abstractaction { private jtable table; public cutaction(jtable table) { this.table = table; } @override public void actionperformed(actionevent e) { int row = table.getselectedrow(); int col = table.getselectedcolumn(); clipboard cb = toolkit.getdefaulttoolkit().getsystemclipboard(); cb.setcontents(new celltransferable(table.getvalueat(row, col)), null); } } class pasteaction extends abstractaction { private jtable table; public pasteaction(jtable tbl) { putvalue(name, "paste"); table = tbl; final clipboard cb = toolkit.getdefaulttoolkit().getsystemclipboard(); cb.addflavorlistener(new flavorlistener() { @override public void flavorschanged(flavorevent e) { setenabled(cb.isdataflavoravailable(celltransferable.cell_data_flavor)); } }); setenabled(cb.isdataflavoravailable(celltransferable.cell_data_flavor)); } @override public void actionperformed(actionevent e) { int row = table.getselectedrow(); int col = table.getselectedcolumn(); clipboard cb = toolkit.getdefaulttoolkit().getsystemclipboard(); if (cb.isdataflavoravailable(celltransferable.cell_data_flavor)) { try { object value = cb.getdata(celltransferable.cell_data_flavor); system.out.println(value); table.setvalueat(value, row, col); } catch (unsupportedflavorexception | ioexception ex) { ex.printstacktrace(); } } } } public static class celltransferable implements transferable { public static final dataflavor cell_data_flavor = new dataflavor(object.class, "application/x-cell-value"); private object cellvalue; public celltransferable(object cellvalue) { this.cellvalue = cellvalue; } @override public dataflavor[] gettransferdataflavors() { return new dataflavor[]{cell_data_flavor}; } @override public boolean isdataflavorsupported(dataflavor flavor) { return cell_data_flavor.equals(flavor); } @override public object gettransferdata(dataflavor flavor) throws unsupportedflavorexception, ioexception { if (!isdataflavorsupported(flavor)) { throw new unsupportedflavorexception(flavor); } return cellvalue; } } }
now, maybe you'd provide runnable example demonstrates not working...
Comments
Post a Comment