swing - java Jbutton icon, move is okay but when I cancel the move, the source button's icon goes blank -
how code: if drop position same source position nothing.
i'm using jbuttons squares, , when moved(dragged) piece, move problem is, if drag piece on same position, meaning changed mind , decided move piece icon changes blank icon.
public class board extends jpanel { private static final string imagefolderpath = "src/resources/images/"; private arraylist<piece>list = new arraylist<piece>(); private imageicon img; private gridbagconstraints gbc; private dragmouseadapter listener = new dragmouseadapter(); jbutton space; jbutton p; imageicon blankimg = new imageicon(imagefolderpath+"/pieces/blank.png"); public board(){ //set layout of each panel , make bg transparent this.setopaque(true); this.setbackground(new color(0,0,0,0)); this.setlayout(new gridbaglayout()); gbc = new gridbagconstraints(); //create 7x9 empty panels for(int x = 0; x<7; x++){ for(int y = 0; y<9; y++){ space = new jbutton(blankimg); space.settransferhandler(new transferhandler("icon"){ @override protected void exportdone(jcomponent source, transferable data, int action) { if (action == move){ system.out.print("hereup"); if(source.getlocation() == space.getlocation()){ return; } else { ((jbutton) source).seticon(blankimg); } } } @override public int getsourceactions(jcomponent c) { return copy | move; } }); space.addmouselistener(listener); gbc.gridx = x; gbc.gridy = y; gbc.ipadx = 21; gbc.ipady = 2; this.add(space,gbc,0); } } ///create pieces , put them on board for(int i=0; i<list.size(); i++){ img = new imageicon(imagefolderpath+"/pieces/"+list.get(i).getimg()); p = new jbutton(img); p.settransferhandler(new transferhandler("icon"){ @override protected void exportdone(jcomponent source, transferable data, int action) { if (action == move){ if(source.getlocation() == p.getlocation()){ return; } else { ((jbutton) source).seticon(blankimg); } } } @override public int getsourceactions(jcomponent c) { return copy | move; } }); p.addmouselistener(listener); gbc.gridx = list.get(i).getx(); gbc.gridy = list.get(i).gety(); gbc.ipadx = 21; gbc.ipady = 2; this.add(p,gbc,0); } } class dragmouseadapter extends mouseadapter { @override public void mousepressed(mouseevent e) { jcomponent c = (jcomponent) e.getsource(); transferhandler handler = c.gettransferhandler(); handler.exportasdrag(c, e, transferhandler.move); } } }
if(source.getlocation() == space.getlocation())
guess should use equals()
instead of ==
Comments
Post a Comment