Results 1 to 6 of 6
  1. #1
    ajs1351 is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default Setting Array List to label within for loop

    k i have an array list of integers - 0-9 in different order. i have a gui with 10 labels and a button. what im tyring to do , is create a for loop that will set the array list to those 10 labels, . how would do this .? do i have to make an array list for the labels as well and combine them?

    i can set them up individually , but then this way it wont let me perform certain calucalation.

    Java Code:
    ArrayList nums = new ArrayList();
    
    nums.add(0,0);
    .
    .
    .nums.add(9,8);
    
    lbl1.setText(nums.get(0));


    what im trying to do with this. is have a 10 labels with these 10 integers, which then i create a for loop that has a specific start index, and a constant count , which then removes that lbl(i). and continues until all end of array.



    i can do this as a console app, but im trying to do it in a gui . just stuck

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Setting Array List to label within for loop

    Can you describe again the exact behavior of your program? It's not very clear to me.

  3. #3
    ajs1351 is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default Re: Setting Array List to label within for loop

    im using netbeans , i have jform,jpanel, button, 10 labels. what i want to do is . have this array List of integers 0 -9 in non sequence order. then put them in those 10 labels.

    then i would perform a calculation which removes a element from the array. given a start index and a constant counter.

    to illustrated .

    10 labels.

    in each label has a integer = 0 1 2 3 5 4 7 6 9 8

    Start index = 2
    count = 92

    start at 2 , go 92 times around, then stops at a the integer, then removes it. does this over and over until all odds numbers are remove.


    that what im trying to do. Like i said i can do this in a console applicaton. but im having a hard time doing it with a gui.

    this is my console app
    Java Code:
    ArrayList<Integer> num = new ArrayList<Integer>();
        num.add(0, 0);
        num.add(1, 1);
        num.add(2,2);
        num.add(3,3);
        num.add(4,5);
        num.add(5,4);
        num.add(6,7);
        num.add(7,6);
        num.add(8,9);
        num.add(9,8);
        
     
       
       
           int ii = 7;
      for(int i = 0; i < num.size(); i++)
       {
            
            ii = (ii + (110-1) ) % num.size();
         num.remove(ii);
       }
    trying to do the same in a gui , but cant get it to work.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Setting Array List to label within for loop

    In other words and to put it much more succinctly, you're trying to create a GUI solution to the Josephus Problem, correct?

    You know you can have an ArrayList that holds JLabels, in other words an ArrayList<JLabel>, and get ints completely out of the picture. And you can remove JLabels from this ArrayList, and at the same time remove them from the GUI container that holds them, but there are some trips and traps that you have to watch out for. For one, the container must be using a layout manager that would handle this well. Ones that come to mind include a FlowLayout, a GridLayout, or a BoxLayout. For another when you remove a component from a component from a container, you must be sure to call revalidate on the component and then repaint so that the component's layout manager can re-layout the existing labels and so that the component can repaint itself and the children it contains so that the removed JLabel can be erased.

  5. #5
    ajs1351 is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default Re: Setting Array List to label within for loop

    Yes im trying to do similar to the Josephus problem.


    Im already using netbeans gui builder for these labels that are in a jpanel. is there a way to get them into a array list ?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Setting Array List to label within for loop

    Quote Originally Posted by ajs1351 View Post
    Yes im trying to do similar to the Josephus problem.


    Im already using netbeans gui builder for these labels that are in a jpanel. is there a way to get them into a array list ?
    Yes, you can use the ArrayList's add(...) method, but I suggest that you don't create these labels with the code-generation tool, but rather create them and place them in a JPanel in your code that you've written yourself.

Similar Threads

  1. Replies: 1
    Last Post: 10-01-2011, 03:12 AM
  2. Linked List, Array List time complexity
    By Rick99771977 in forum New To Java
    Replies: 4
    Last Post: 08-18-2011, 06:37 AM
  3. Replies: 1
    Last Post: 03-08-2010, 05:14 PM
  4. Label in array SWT
    By ashin in forum SWT / JFace
    Replies: 0
    Last Post: 11-28-2009, 04:14 PM
  5. Setting the label of radio buttons from Resource bundle
    By rajeeshankar in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 12-17-2007, 10:23 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
  •