Get your CodeRanch badge!*
The moose likes Swing / AWT / SWT and the fly likes Accessing a jLabel with % reference? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Accessing a jLabel with % reference?" Watch "Accessing a jLabel with % reference?" New topic
Author

Accessing a jLabel with % reference?

Dick Hammer
Greenhorn

Joined: Jan 28, 2012
Posts: 18
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
Bartender

Joined: Oct 14, 2005
Posts: 18135
    
    8

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: 18
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

Joined: May 03, 2008
Posts: 4523
    
    5

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
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
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

Joined: May 03, 2008
Posts: 4523
    
    5

That's right.
http://wiki.netbeans.org/FaqGuiControlArray
Dick Hammer
Greenhorn

Joined: Jan 28, 2012
Posts: 18
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

Joined: Jun 09, 2003
Posts: 4632
> 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

Joined: Jan 28, 2012
Posts: 18
thanks I think it worked
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing a jLabel with % reference?
 
Similar Threads
Html Labels
JLabel into a JTable field
push flowlayout panel to the left.
Getting a Component to show?
Java Board Game