Java Swing JLabels show in a buildGUI method but not if added from another method -
i'm amateur writing archery score card. programme works @ cost of 19 sections of identical code each of 18 lines. i'm trying condense code using method call. i'm using java se6 , mig layout
here section of code in gui works. gui called below
homepage (containing main method) -> choicegui -> buildscorespanel
public void buildscorespanelmig(jpanel scorespanel) { (row = 0; row<(int)numberofrows; row++){ scorespanel.add(scorelabel1[row],"gapleft 0,w 35px, hmin 35px,split 18"); scorespanel.add(scorelabel2[row],"gap before 0px,gapleft 0,w 35px, hmin 35px"); scorespanel.add(scorelabel3[row],"gap before 0px,gapleft 0,w 35px, hmin 35px"); scorespanel.add(scorelabel4[row],"gap before 0px,gapleft 0,w 35px, hmin 35px"); scorespanel.add(scorelabel5[row],"gap before 0px,gapleft 0,w 35px, hmin 35px"); scorespanel.add(scorelabel6[row],"gap before 0px,gapleft 0,w 35px, hmin 35px"); //another 12 jlabels } }
if, put code in method , call below jlabels won't show though i've tried revalidate() repaint() , setvisible(true)
public void buildscorespanelmig(jpanel scorespanel) { (row = 0; row<(int)numberofrows; row++){ addscorelabels(); } } public void addscorelabels(){ scorespanel.add(scorelabel1[row],"gapleft 0,w 35px, hmin 35px,split 18"); scorespanel.add(scorelabel2[row],"gap before 0px,gapleft 0,w 35px, hmin 35px"); scorespanel.add(scorelabel3[row],"gap before 0px,gapleft 0,w 35px, hmin 35px"); scorespanel.add(scorelabel4[row],"gap before 0px,gapleft 0,w 35px, hmin 35px"); scorespanel.add(scorelabel5[row],"gap before 0px,gapleft 0,w 35px, hmin 35px"); scorespanel.add(scorelabel6[row],"gap before 0px,gapleft 0,w 35px, hmin 35px"); //another 12 labels //scorespanel.revalidate(); //scorespanel.repaint(); //scorespanel.setvisible(true); }
i have trawled internet quite while trying solve problem , realise have fundamental misunderstanding of how swing components work , grateful if explain.
try passing scorespanel
argument addscorelabels()
method too:
addscorelabels(scorespanel); ... public void addscorelabels(jpanel scorespanel) { ...
as chris cooney points out in comments, have different panel stored in scorespanel
field variable, being hidden local variable in first method, not in second.
Comments
Post a Comment