• 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
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

making up the name of a Jlabel  RSS feed

 
Ranch Hand
Posts: 138
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




 
Master Rancher
Posts: 3080
108
  • 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: 62819
203
  • 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: 24080
54
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: 24080
54
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: 20310
110
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.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!