Help coderanch get a
new server
by contributing to the fundraiser
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Accessing a jLabel with % reference?

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure what kind of referencing (if that's the right word) this is called but I will have many jLabels, say jLabel1, jLabel2....jLabel5. (I am using Netbeans drag and drop design UI). How can I get something like this to work:

int Number = ran.nextInt(5)+1;
jLabel%Number%.setIcon(new javax.swing.ImageIcon("/home/batman/Desktop/Viper-Orange.jpg"));

You know, because the jLabel I want the image assigned to will be random. At the risk of being redundant, if for example, the int variable Number = 2, how can I get jLabel%Number% to work, you know, to "equal" jLabel2?
 
Marshal
Posts: 28271
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instead of giving the labels those names, you should have an array of labels. Then your code would look like this:
 
Dick Hammer
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much. Netbeans doesn't let me touch generated code, and "jLabel1", and other things like "jButton1" are hard-coded or whatever, but as far as syntax, the brackets would work with a single (non-array) int variable too right?
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So drop the NetBeans visual designer. As you've discovered the hard way, it's not a beginners' tool -- regardless of what Oracle would have you believe.

Or -- the choice is yours --expend several times more time and effort learning to use the visual designer effectively. That's only worthwhile if you're aiming for a career in GUI designing. For mere non-specialists, it's faster and easier to learn to hand-code a Swing GUI.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
100% agree with Darryl, but if you really want to modify the generated code:

1) add this line to the section // Variables declaration - do not modify
JLabel[] labels;

2) add the indicated line


unless the 'Variables declaration' section is regenerated, you will now be able to access the labels via
labels[number].doSomething();
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's right.
http://wiki.netbeans.org/FaqGuiControlArray
 
Dick Hammer
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michael Dunn wrote:100% agree with Darryl, but if you really want to modify the generated code:

1) add this line to the section // Variables declaration - do not modify
JLabel[] labels;

2) add the indicated line


unless the 'Variables declaration' section is regenerated, you will now be able to access the labels via
labels[number].doSomething();



It gives me errors. Something about not finding symbol? Oh and if I figure out how to edit that closed section of code of "private void InitComponents" would I have to change jLabel1 = new javax.swing.JLabel(); to labels[0] = new javax.swing.JLabel();? And also, there is no "Variable Declarations" section.




 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> would I have to change jLabel1 = new javax.swing.JLabel(); to labels[0] = new javax.swing.JLabel();?

no, the lines like this
jLabel1 = new javax.swing.JLabel();
are in initComponents()

this line is wrong (for what you're trying to do)
labels[0] = new javax.swing.JLabel();
the array of labels are references to already created labels
you could have either
labels = new JLabel[]{jLabel1,jLabel2, ..etc..};
or
labels = new JLabel[number]; then
labels[0] = jLabel1; labels[1] = jLabel2; etc
and this will be in the constructor, *after* the call to initComponents().

> And also, there is no "Variable Declarations" section.

copied from your other post - see line 115 of the posted code in the link

https://coderanch.com/t/565811/GUI/java/Newbie-just-trying-change-icon
 
Dick Hammer
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks I think it worked
 
You guys wanna see my fabulous new place? Or do you wanna look at this tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic