java - How to use the value of the variable from "for loop" for mouseEntered? -
how "i" variable used in "for loop" mouseentered inside panelresult[i].addmouselistener? thanks!
buttoncategory[0].addmouselistener(new mouseadapter() { @override public void mouseclicked(mouseevent e) { //hidden codes here... panelcategory.remove(scrollpane); panelcategory.add(scrollpane); panelarray.removeall(); panelresult = new jpanel[totalcount]; int y = 0; for(int = 0; < panelresult.length; i++){ panelresult[i] = new jpanel(); panelresult[i].setlayout(null); panelresult[i].setbackground(new color(27, 182, 32)); panelresult[i].setbounds(0, y, 730, 200); panelarray.add(panelresult[i]); //hidden codes here... panelresult[i].add(labelarrayresult); panelresult[i].addmouselistener(new mouseadapter() { @override public void mouseentered(mouseevent e) { panelresult[0].setbackground(color.black); //if change panelresult[i].setbackground(color.black); "i" error. want value of panelresult[i]. revalidate(); repaint(); } }); } revalidate(); repaint(); } revalidate(); repaint(); } });
i'm planning use mouseclicked , mouseexited. list of result that's why it's arrayed , looped.
the exception is:
exception in thread "awt-eventqueue-0" java.lang.arrayindexoutofboundsexception: 7 @ ph.edu.tip.cpedepartment.cpepds.projectdesignpage$1$1.mouseentered(projectdesignpage.java:233)
also number of panels total count database that's why need use index. again!
i believe can use mouseevent's souce source of event:
public void mouseentered(mouseevent e) { ((jpanel)e.getsource()).setbackground(color.black); //the index here error revalidate(); repaint(); }
if doesn't work, i'd try :
for(int = 0; < panelresult.length; i++){ //some hidden codes here... final jpanel panel = panelresult[i]; panel.add(labelarrayresult); panel.addmouselistener(new mouseadapter() { @override public void mouseentered(mouseevent e) { panel.setbackground(color.black); //the index here error revalidate(); repaint(); } }); }
Comments
Post a Comment