java - Access the method inside the addMouseListener() method -
i want return count of mouse clicks every time panel clicked. have basepanel class has code snippet:
basepanel(){ //inside basepanel class addmouselistener(new mouseadapter(){ public void mousereleased(mouseevent e){ if(clicked == troop1){ count1--; count = count1; //i want count accessible in class system.out.println("troop1 count: "+count); }else if(clicked == troop2){ count2--; count = count2; system.out.println("troop2 count: "+count); } } public int getcount(){ //how can method accessible in class return count; } }); }
here, wanted return count variable. want buttonspanel class able access getcount() method in basepanel class.
buttonspanel(){ //inside buttonspanel class basepanel pane = new basepanel(); troop1 = new jbutton(""+pane.getcount()); troop2 = new jbutton(""+pane.getcount()); }
but think, way access getcount() method in buttonspanel class wrong.
getcount()
method of anonymous class implementation of mouseadapter
.
if want able access it, you'll have pull out of anonymity:
basepanel(){ mouseadapter mouseadapter = new mouseadapter(){ public void mousereleased(mouseevent e){ if(clicked == troop1){ count1--; count = count1; //i want count accessible in class system.out.println("troop1 count: "+count); }else if(clicked == troop2){ count2--; count = count2; system.out.println("troop2 count: "+count); } } public int getcount(){ return count; } }); addmouselistener(mouseadapater); }
and add getter method class:
public mouseadapter getmouseadapater() { return mouseadapter; }
and can access such:
buttonspanel(){ //inside buttonspanel class basepanel pane = new basepanel(); mouseadapter adapater = pane.getmouseadapater(); troop1 = new jbutton(""+adapater.getcount()); troop2 = new jbutton(""+adapater.getcount()); }
Comments
Post a Comment