my dog learned polymorphism*
The moose likes Java in General and the fly likes how to refer to JLabels as strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to refer to JLabels as strings" Watch "how to refer to JLabels as strings" New topic
Author

how to refer to JLabels as strings

Babis Papadopoulos
Greenhorn

Joined: Dec 11, 2012
Posts: 17
I want to make a method that set images in labels.. this method I will call many times in my program .. My prloblem is how I make something like this:

(Ofcourse this isn't work...)
for not writing a screed like this:


because I want for example 20 JLabel...and you understand that will become very large
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

You can't have variable names that are dynamically created like that, but you can use a collection. For example, you could create a Map<String, JLabel> and add your labels to that. Then you can refer to individual labels using the String you used as a key, or you can easily iterate across all of them.
Babis Papadopoulos
Greenhorn

Joined: Dec 11, 2012
Posts: 17
Thanks for your reply.. It is a help..


Therefore there is no way to create you #(a given number from user) labels, right?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

Oh, creating a given number isn't a problem - you just do it in a loop like your first example. But instead of assigning to a different variable every time, you put them in a collection that you've already created.
Babis Papadopoulos
Greenhorn

Joined: Dec 11, 2012
Posts: 17
Yeah!! Thanks again!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to refer to JLabels as strings
 
Similar Threads
Thread.sleep problem (change image) Java
JARS and Manifest files
running a class a particular number of times
Putting an image in a JLabel
Non-static method in static context