Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing a jLabel with % reference?

 
Dick Hammer
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • 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?
 
Paul Clapham
Sheriff
Posts: 21319
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • 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: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's right.
http://wiki.netbeans.org/FaqGuiControlArray
 
Dick Hammer
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • 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
  • 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

http://www.coderanch.com/t/565811/GUI/java/Newbie-just-trying-change-icon
 
Dick Hammer
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks I think it worked
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic