Results 1 to 5 of 5
  1. #1
    Cymro is offline Member
    Join Date
    Jan 2008
    Posts
    7
    Rep Power
    0

    Default [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,
    Java Code:
    public static [i]someSortOfDataType[/i][] 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 10:21 AM. Reason: Found an answer

  2. #2
    scuffia's Avatar
    scuffia is offline Member
    Join Date
    Jan 2008
    Location
    Cagliari (Italy)
    Posts
    7
    Rep Power
    0

    Default

    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:
    Java Code:
    if(myArray[i].getClass().equals(JLabel.getClass()) )
         //do what you want

  3. #3
    Cymro is offline Member
    Join Date
    Jan 2008
    Posts
    7
    Rep Power
    0

    Default

    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?

  4. #4
    scuffia's Avatar
    scuffia is offline Member
    Join Date
    Jan 2008
    Location
    Cagliari (Italy)
    Posts
    7
    Rep Power
    0

    Default

    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:
    Java 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.

  5. #5
    Cymro is offline Member
    Join Date
    Jan 2008
    Posts
    7
    Rep Power
    0

    Default

    Thanks very much again, and it seems that I can do that.

    However, if I try something like this, attatched to a button:
    Java 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 :confused:


    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 10:20 AM. Reason: found an answer

Similar Threads

  1. Putting code together.
    By newbee in forum New To Java
    Replies: 3
    Last Post: 04-17-2008, 04:53 AM
  2. Putting your own type in a Set
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 08:32 PM
  3. new to arrays
    By jimJohnson in forum New To Java
    Replies: 1
    Last Post: 04-08-2008, 03:45 PM
  4. 2D-Arrays
    By kbyrne in forum New To Java
    Replies: 1
    Last Post: 02-07-2008, 11:08 PM
  5. Tree controls using Swing
    By kabir in forum AWT / Swing
    Replies: 1
    Last Post: 01-05-2008, 10:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •