Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java and collecting GUI objects in a vector

  • 18-03-2005 10:51pm
    #1
    Closed Accounts Posts: 1,325 ✭✭✭


    Hey everyone,

    I'm a little stumped and can't for the live of me think of a way of rectifying the following problem. If someone could throw me some suggestions it'd be great. I'll write some pseudcode below (very rough)...
    Vector one;
    
    for(one.size)
    {
        Create new label;
        Add new label to panel;
    }
    

    Above is pretty simple. What I want to do is keep an instance of the different labels alive, so to speak, by perhaps putting them in a Vector.

    My problem starts when I try to loop through the Vector, which I would now think contain my nice GUI objects but it doesn't, the garbage collector has been busy :)

    Basically, I want to collect all my GUI objects and be able to display them at a later stage. What am I missing cause I know I'm missing something?


    Thanks for any help or advise,
    A.


Comments

  • Closed Accounts Posts: 1,325 ✭✭✭b3t4


    Fixed the problem.

    I needed to use the class Collection to, funnily enough, collect the GUI objects :)

    I don't quite understand why a collection can so this and a Vector can't. I say this as because to use the collection I did the following...

    Collection guiCollection = new Vector();

    If someone would like to indulge my curiosity I'd be very happy.

    A.


  • Registered Users, Registered Users 2 Posts: 4,003 ✭✭✭rsynnott


    How were you adding the labels to the vector? The garbage collector shouldn't do its thing until ALL references to the offending items are gone... It SHOULD make no difference whether you use a collection or vector, I think...


  • Registered Users, Registered Users 2 Posts: 885 ✭✭✭clearz


    Are you creating the labels inside the for loop. If you are you will not be able to access them once you leave it.


  • Closed Accounts Posts: 1,325 ✭✭✭b3t4


    Clearz,

    Yup, I am creating them in the for loop. I don't see how creating a label outside the for loop will help. I need to create a new instance of a label for every for loop iteration. If I have one label and keep overwriting it will I not still run into problems as the label will be only as good as the last iteration, if you get me.

    Just thought, if I was to create an array of labels the same size as the for loop interations, outside the for loop. Then instantiate them in the for loop would that help matters.

    A.


  • Registered Users, Registered Users 2 Posts: 4,003 ✭✭✭rsynnott


    Do something like:
    mylabel = new Label("Demo text");

    That'll create a new Label in memory at each iteration.


  • Advertisement
  • Closed Accounts Posts: 1,502 ✭✭✭MrPinK


    It's hard to see what was going wrong by just looking at 4 lines of rough pseudcode. Can you post the actual code that wasn't working for you?


  • Registered Users, Registered Users 2 Posts: 307 ✭✭Thordon


    In the psuedocode, you never add the labels to the vector, nor do you specify the size of the vector, if you do Vector one = new Vector(), I think default size is 0, and the for loop would not loop at all because of that, heres how I would do it:
    Vector v = new Vector();
    int numLabels = 5;
    
    for(int i = 0; i < numLabels; i += 1)
    {
    	Label label = new Label("Label " + (i + 1));
    	v.add(label);
    	getContentPane().add(label);
    }
    

    Edit: I think I misunderstood what you wanted to do, can you show the code where you add the labels/GUI elements to the vector initially, I imagine that the problem is there.


  • Closed Accounts Posts: 1,325 ✭✭✭b3t4


    I was adding the labels to the vector in the same way that I was adding them to the collection. The collection worked, the vector didn't

    The code is fixed now and working so I'm not too worried about it. :)

    Thanks for all the feedback though.

    A.


  • Closed Accounts Posts: 1,502 ✭✭✭MrPinK


    Fair enough. But the fact that you made the reference a Collection rather than a Vector wouldn't have been what fixed it.

    You could have "Collection guiCollection = new Vector()" or "Vector guiCollection = new Vector()", guiCollection will still perform in exactly the same.


  • Closed Accounts Posts: 324 ✭✭madramor


    your theory of storing GUI elements in a vector is incorrect

    vectors are synchronised,
    so if you use a loop to store stuff in vector it will keep
    changing all elements of the vector because they are synced

    you should use an array of labels


  • Advertisement
  • Closed Accounts Posts: 92 ✭✭tempest


    madramor wrote:
    your theory of storing GUI elements in a vector is incorrect

    vectors are synchronised,
    so if you use a loop to store stuff in vector it will keep
    changing all elements of the vector because they are synced

    you should use an array of labels
    :eek:
    a) Makes no sense
    b) The only part that's true is that vectors are synchronized. Please read the JLS to determine the meaning of the synchronized keyword before making statements like this.


  • Registered Users, Registered Users 2 Posts: 4,003 ✭✭✭rsynnott


    madramor wrote:
    your theory of storing GUI elements in a vector is incorrect

    vectors are synchronised,
    so if you use a loop to store stuff in vector it will keep
    changing all elements of the vector because they are synced

    you should use an array of labels

    Wuh? Nonsense.

    Die wectors ARE synchronised, but that's not what synchronised means (not in this context...) Honestly, all Java books should be required to start off with a little section on what the word means from Java's point of view; it seems to confuse the hell out of people, for some reason.


  • Closed Accounts Posts: 324 ✭✭madramor


    // part 1
    JLabel l[] = new JLab..
    for(i){
       l[i] = new JLa...
       l[i].setFont(..
       l[i].setSize..
       l[i].setBack..
       l[i].setAli...
    }
    // part 2
    getEmployeePanel(JLabel[] l){
       String ln[] = {"Name","DOB","Addres".........};
       for(i){
          l[i].setText(ln[i]);
    }
    getJobPanel(JLabel[] l){
       String ln[] = {"Boss","Start","Location".........};
       for(i){
          l[i].setText(ln[i]);
    }
    // part 3
    getHolidayPanel(JLabel[] l){
       String ln[] = {"Date","Period","Remaining".........};
       for(i){
          l[i].setText(ln[i]);
          l[i].setFont.....
    }
    
    
    sometimes you use loops in GUI to preprocess objects
    it looks like the poster is doing something like this but only storing them
    in a vector not arrays so last line in loop is v.add(l); no l.

    then when you use the array you have part 2

    now if you had a vector to store objects instread of an array, if you
    had to do a change for one usage see part 3 it would effect the vector
    for all other instances

    why else would you want to keep gui objects alive unless to reuse them

    b3t4 wrote:
    Above is pretty simple. What I want to do is keep an instance of the different labels alive, so to speak, by perhaps putting them in a Vector.


  • Registered Users, Registered Users 2 Posts: 4,003 ✭✭✭rsynnott


    You certainly COULD do this (the renaming of the labels); it's questionable whether it would ever be the right thing to do tho, and once you get into adding usable components, tooltips, etc, it might be better just go with a load of switchable panels.


  • Closed Accounts Posts: 92 ✭✭tempest


    // part 1
    Vector v = new Vector
    for(i){
       JLabel label = new JLa...
       label .setFont(..
       label .setSize..
       label .setBack..
       label .setAli...
       v.add(label)
    }
    // part 2
    getEmployeePanel(Vector vecLabels){
       String ln[] = {"Name","DOB","Addres".........};
       for(i){
          ((JLabel)(vecLabels.get(i))).setText(ln[i]);
    }
    getJobPanel(Vector vecLabels){
       String ln[] = {"Boss","Start","Location".........};
       for(i){
          ((JLabel)(vecLabels.get(i))).setText(ln[i]);
    }
    // part 3
    getHolidayPanel(Vector vecLabels){
       String ln[] = {"Date","Period","Remaining".........};
       for(i){
          ((JLabel)(vecLabels.get(i))).setText(ln[i]);
          ((JLabel)(vecLabels.get(i))).setFont.....
    }
    

    How is this (functionally) different? Besides typesafety and a slight (negligible) performance hit??

    Synchronization certainly has nothing to do with it!!


  • Closed Accounts Posts: 324 ✭✭madramor


    tempest wrote:
    // part 1
    Vector v = new Vector
    for(i){
       JLabel label = new JLa...
       label .setFont(..
       label .setSize..
       label .setBack..
       label .setAli...
       v.add(label)
    }
    // part 2
    getEmployeePanel(Vector vecLabels){
       String ln[] = {"Name","DOB","Addres".........};
       for(i){
          ((JLabel)(vecLabels.get(i))).setText(ln[i]);
    }
    getJobPanel(Vector vecLabels){
       String ln[] = {"Boss","Start","Location".........};
       for(i){
          ((JLabel)(vecLabels.get(i))).setText(ln[i]);
    }
    // part 3
    getHolidayPanel(Vector vecLabels){
       String ln[] = {"Date","Period","Remaining".........};
       for(i){
          ((JLabel)(vecLabels.get(i))).setText(ln[i]);
          ((JLabel)(vecLabels.get(i))).setFont.....
    }
    

    How is this (functionally) different? Besides typesafety and a slight (negligible) performance hit??

    Synchronization certainly has nothing to do with it!!


    your template that you create in part 1
    is changed in part 3
    all subsequent panels that use the template will have the new
    font that you used in part 3


  • Closed Accounts Posts: 92 ✭✭tempest


    madramor wrote:
    your template that you create in part 1
    is changed in part 3
    all subsequent panels that use the template will have the new
    font that you used in part 3

    And why will that not happen with an array?

    Both the Vector and the array contain references to the objects. Therefore the changes that are made via one reference are visible to all objects with the same reference. It makes no difference if they are stored in an array or in a vector. All that matters is where the reference points and in both cases the reference points to the original object that was created.

    The template which is changed in part 3 is also changed in part3 when you use an array.


  • Closed Accounts Posts: 1,325 ✭✭✭b3t4


    rsynnott wrote:
    it might be better just go with a load of switchable panels.

    I will indead be hoping to add tool tips and that to the labels/any gui object created in a loop of some sort.

    What do you mean by switchable panels??

    A.


Advertisement