java - Is it possible to disable drag and drop for specific cell in JTable? -



java - Is it possible to disable drag and drop for specific cell in JTable? -

i'm trying drag , drop cells in jtable. works know possible disable drag , drop first cell illustration because don't want cell modified?

here code

public class test extends jframe { public test() { setsize(500, 200); setlocationrelativeto(null); setdefaultcloseoperation(jframe.exit_on_close); jpanel pan = new jpanel(); pan.setlayout(new gridlayout(1, 1, 5, 5)); object[][] info = new object[][] { { "00", "10", null }, { "01", "11", null }, { "02", "20", null } }; string[] name = new string[] { "a", "b", "c" }; defaulttablemodel model = new defaulttablemodel(data, name); jtable jt = new jtable(model); pan.add(jt); jt.setrowheight(24); jt.setrowselectionallowed(false); jt.setfillsviewportheight(true); jt.setselectionmode(listselectionmodel.single_selection); jt.setdragenabled(true); jt.setdropmode(dropmode.use_selection); jt.settransferhandler(new mytransferhandlert()); setcontentpane(pan); setvisible(true); } public static void main(string[] args) { new test(); } }

and code transferhandler

public class mytransferhandlert extends transferhandler { private jtable table; private defaulttablemodel model; private int rowindex; private int colindex; @override public int getsourceactions(jcomponent c) { homecoming move; } @override protected transferable createtransferable(jcomponent source) { table= (jtable)source; model = (defaulttablemodel) table.getmodel(); rowindex = table.getselectedrow(); colindex = table.getselectedcolumn(); model.getvalueat(rowindex, colindex); string value = (string)model.getvalueat(rowindex, colindex); transferable t = new stringselection(value); homecoming t; } @override protected void exportdone(jcomponent source, transferable data, int action) { table= (jtable)source; model = (defaulttablemodel) table.getmodel(); rowindex = table.getselectedrow(); colindex = table.getselectedcolumn(); model.setvalueat("", rowindex, colindex); } @override public boolean canimport(transfersupport support) { if (!support.isdataflavorsupported(dataflavor.stringflavor)) { homecoming false; } homecoming true; } @override public boolean importdata(transfersupport support) { table = (jtable) support.getcomponent(); object data= null; int row = table.getselectedrow(); int col = table.getselectedcolumn(); seek { info = (object) support.gettransferable().gettransferdata(dataflavor.stringflavor); } grab (unsupportedflavorexception e) { system.out.println("unsupported flavor exception"); e.printstacktrace(); } grab (ioexception e) { system.out.println("io exception"); e.printstacktrace(); } model.setvalueat(data, row, col); model.firetablestructurechanged(); homecoming false; } }

thanks.

model.setvalueat(data, row, col); //model.firetablestructurechanged();

do not invoke firetablestructurechanged() method directly. responsibility of tablemodel.

is possible disable drag , drop first cell

see section swing tutorial on location sensitive drop. has illustration disable drop on first column.

java swing drag-and-drop jtable

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

c# - Create a Notification Object (Email or Page) At Run Time -- Dependency Injection or Factory -

Set Up Of Common Name Of SSL Certificate To Protect Plesk Panel -