java - Generating JLabels on demand -



java - Generating JLabels on demand -

i able generate map, of sorts, places little jlabels @ coordinate locations on panel. problem need them randomly generated, don't know in advance how many have. there way that?

i hope isn't breaking java coding taboos - i'm self-taught.

*edit: know vague - programme huge , cumbersome , have developed own conventions (which i'm sure raise hackles of real java coders :-p) should have specified have class location, , can generate random locations. problem have in creating new jlabel each of locations. here's have:

//method called after new location has been created, add together map public void addlocation(location newlocation) { int xx = newlocation.getxloc(); int yy = newlocation.getyloc(); (int i=0;i<1;i++) { jlabel templabel = new jlabel(); //templabel instantiated elsewhere (is problem?) templabel.setbackground(color.black); templabel.setbounds(xx,yy,3,3); map.add(templabel); //map jpanel null layout manager templabel.setvisible(true); } }

the problem doesn't seem anything. no black dots appear on map. maybe it's simple wrong implementation of adding label panel?

but happen know if it's possible add together mouselistener each of these jlabels they're created?

sure possible. don't need create new listener every time. can create 1 listener used labels. within listener code use:

jlabel label = (jlabel)event.getsource();

and have access label generated mouseevent.

java swing jlabel

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

c# - Can ProtoBuf-Net deserialize to a flat class? -

javascript - Change element in each JQuery tab to dynamically generated colors -