Results 1 to 6 of 6
- 11-19-2011, 05:16 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 14
- Rep Power
- 0
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
-
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.
- 11-19-2011, 05:57 AM #3
Member
- Join Date
- Nov 2011
- Posts
- 14
- Rep Power
- 0
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
trying to do the same in a gui , but cant get it to work.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); }
-
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.
- 11-19-2011, 07:01 PM #5
Member
- Join Date
- Nov 2011
- Posts
- 14
- Rep Power
- 0
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 ?
-
Re: Setting Array List to label within for loop
Similar Threads
-
Is it Possible? Array elements Initialized in Loop, can it be viewed outside loop?
By JPH in forum New To JavaReplies: 1Last Post: 10-01-2011, 02:12 AM -
Linked List, Array List time complexity
By Rick99771977 in forum New To JavaReplies: 4Last Post: 08-18-2011, 05:37 AM -
Setting a variable to false with a keybinding to terminate a while loop by pressing
By MaJellin in forum Threads and SynchronizationReplies: 1Last Post: 03-08-2010, 04:14 PM -
Label in array SWT
By ashin in forum SWT / JFaceReplies: 0Last Post: 11-28-2009, 03:14 PM -
Setting the label of radio buttons from Resource bundle
By rajeeshankar in forum JavaServer Faces (JSF)Replies: 0Last Post: 12-17-2007, 09:23 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks