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?
Instead of giving the labels those names, you should have an array of labels. Then your code would look like this:
Dick Hammer
Greenhorn
Joined: Jan 28, 2012
Posts: 13
posted
0
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?
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.
luck, db
There are no new questions, but there may be new answers.
Michael Dunn
Rancher
Joined: Jun 09, 2003
Posts: 4041
posted
0
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();
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.
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