• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

making up the name of a Jlabel  RSS feed

 
Ranch Hand
Posts: 147
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi every one I have a general method


controlling this i have a switch statement


i was wondering if it is possible to make up the name of the label by some mean concatenating the value of "i"to the word cover and then turning into the object so i can pass it to the general method and remove the need for the switch statement




 
Rancher
Posts: 3119
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A simple way is to have a Map<String, JLabel>, where you put your labels when you create them.
In your switch, all you have to do is

In fact you do not need the switch.
 
Marshal
Posts: 63795
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean about names of labels? Do you mean the text on it? That is easy enough to alter. I am not quite sure what that switch is intending to achieve.
 
Sheriff
Posts: 24290
55
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or in this case you could put the labels in an array, so that your code would look like this:



Again, no switch would be needed.
 
Paul Clapham
Sheriff
Posts: 24290
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or it would be much nicer and more object-oriented if the thing calling that switch-case code had an actual reference to the JLabel to be worked on, rather than just an int value representing that JLabel. That's what I would urge you to try, really.
 
Bartender
Posts: 20568
120
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
File note: you don''t need to use double slashes when coding filepaths as strings in universal form. Forward ("real") slashes are OK with just 1 slash between levels, although URLs have a different and unrelated convention.

The reason you have to double up on backslashes is because in Java and in the Unix/Linux OS world, backslash has an alternative meaning that has to be escaped.

I second the idea of having an array for the label controls and image paths. But if you're actually changing the same point on the screen, what you probably should be doing isn't swapping in JLabels, but rather simply updating the JLabel that's already there.
 
If we don't do the shopping, we won't have anything for dinner. And I've invited this tiny ad:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!