Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-31-2008, 08:27 PM
Member
 
Join Date: Jan 2008
Posts: 7
Cymro is on a distinguished road
[Resolved] Putting controls in arrays?
Hello all, I couldn't find anything under "control array(s)", so I hope you can answer this for me.

Is it possible to put controls in arrays (ie,
Code:
public static someSortOfDataType[] myArray = {jLabel1,jLabel2,etc};
), and then modify them (myArray[1].setText)? If so, what sort of data type would the array be?

I am aware of a fairly long winded bit of code that creates the controls at runtime (which would be a little annoying to use), but I was wondering if there was another way.

Thanks in advance.

Last edited by Cymro : 02-02-2008 at 11:21 AM. Reason: Found an answer
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-01-2008, 12:56 AM
scuffia's Avatar
Member
 
Join Date: Jan 2008
Location: Cagliari (Italy)
Posts: 7
scuffia is on a distinguished road
the easiest way is to declare Object[] array if you have different kinds of controls (if all are JLabel there is no problem ,simply declase JLabel[] ), and each time you need to use a method, you first check if current element is of the class you wish to use like:
Code:
if(myArray[i].getClass().equals(JLabel.getClass()) ) //do what you want
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-01-2008, 01:28 AM
Member
 
Join Date: Jan 2008
Posts: 7
Cymro is on a distinguished road
Thanks. Before I start doing a test program (tomorrow, as it happens), is there a way of using JLabel[] for existing jLabels, or must they be added at runtime?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 02-01-2008, 01:37 AM
scuffia's Avatar
Member
 
Join Date: Jan 2008
Location: Cagliari (Italy)
Posts: 7
scuffia is on a distinguished road
Indeed if you do not know the precise number of JLabels, you can use the java.util.ArrrayList class, which allow you to add an object at runtime, each time you want:
Code:
ArrayList<JLabel> array = new ArrayList<JLabel>(); . . . array.add(jLabel1); array.add(jLabel2); array.add(jLabel3);
Usign this object (google it and you'll find how to use it) you have no problem of what you add runtime or not.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 02-01-2008, 08:54 PM
Member
 
Join Date: Jan 2008
Posts: 7
Cymro is on a distinguished road
Thanks very much again, and it seems that I can do that.

However, if I try something like this, attatched to a button:
Code:
for (int i = 0 ; i < 3 ; i++){ labRay[i].setText("This label is in the array"); }
It gives me an error of "Array required, but java.util.ArrayList<javax.swing.JLabel> found". I can't quite comprehend what it's trying to tell me


EDIT: Aha! Got it. I had to use labRay.get(i).setText instead. Thanks again for your help.

Last edited by Cymro : 02-02-2008 at 11:20 AM. Reason: found an answer
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Putting code together. newbee New To Java 3 04-17-2008 05:53 AM
Putting your own type in a Set Java Tip java.lang 0 04-15-2008 09:32 PM
new to arrays jimJohnson New To Java 1 04-08-2008 04:45 PM
2D-Arrays kbyrne New To Java 1 02-08-2008 12:08 AM
Tree controls using Swing kabir AWT / Swing 1 01-05-2008 11:48 AM


All times are GMT +3. The time now is 02:06 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org