5 Replies - 5387 Views - Last Post: 18 November 2011 - 09:48 PM Rate Topic: -----

#1 ajs1351  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-November 11

Displaying an ArrayList into labels using a for loop in a gui.

Posted 18 November 2011 - 05:34 PM

I have an ArrayList of Integers . 0-9 in different order. i have a gui with 10 labels and 1 button. my question is

how do i display the arraylist into those labels using a for loop?



i know how to display each array index to each label individually but not in a for loop. do i have also have an array list for the labels as well?
Is This A Good Question/Topic? 0
  • +

Replies To: Displaying an ArrayList into labels using a for loop in a gui.

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10567
  • View blog
  • Posts: 39,117
  • Joined: 27-December 08

Re: Displaying an ArrayList into labels using a for loop in a gui.

Posted 18 November 2011 - 07:02 PM

This is how you would use a loop to access each element in an ArrayList.
for(int i = 0; i < listName.size(); i++){
    //set the text of the appropriate JLabel to listName.get(i)
}



For more on ArrayLists, check out Locke's tutorial.
Was This Post Helpful? 0
  • +
  • -

#3 ajs1351  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-November 11

Re: Displaying an ArrayList into labels using a for loop in a gui.

Posted 18 November 2011 - 09:00 PM

View Postmacosxnerd101, on 18 November 2011 - 07:02 PM, said:

This is how you would use a loop to access each element in an ArrayList.
for(int i = 0; i < listName.size(); i++){
    //set the text of the appropriate JLabel to listName.get(i)
}




SO i would set all 10 labels to ListName.get(i) within loop?

like
for (int i=0; i < ListName.size(); i++)
{
  lbl1.setText(ListName.get(i)));
  lbl2.setText(ListName.get(i)));
   .
   .
   .
   lbl10 .setText(ListName.get(i));
}



By doing that i would just be iterating 10 times setting all 10 labels to ListName.get(i) ?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10567
  • View blog
  • Posts: 39,117
  • Joined: 27-December 08

Re: Displaying an ArrayList into labels using a for loop in a gui.

Posted 18 November 2011 - 09:03 PM

Let's ask a better question- what information are you storing? Also, why aren't you using a JLabel[]? I have a feeling your design could be improved. Please post your relevant code as well.
Was This Post Helpful? 0
  • +
  • -

#5 ajs1351  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-November 11

Re: Displaying an ArrayList into labels using a for loop in a gui.

Posted 18 November 2011 - 09:42 PM

View Postmacosxnerd101, on 18 November 2011 - 09:03 PM, said:

Let's ask a better question- what information are you storing? Also, why aren't you using a JLabel[]? I have a feeling your design could be improved. Please post your relevant code as well.



What I'm doing is. I'm using netbeans, and i have a jform, jpanel,button , 10 labels.

I have an Array of Integers values 0-9 in non sequence. these 10 integers will go into these 10 labels.

then i would for a loop that will have start index, and a constant count, which will iterate through the list and removing an a integer from the list at count given . does this all the way until its fullfill.

i can do all this as a console app. but cant figure it out to do it in a gui.


like i said i can set it individually like

ArrayList nums = new ArrayList();

nums.add(0,0);
.
.
.
nums.add(9,8);


lbl1.setText(nums.get(1));




But then i cant get this to do what i want it to do.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10567
  • View blog
  • Posts: 39,117
  • Joined: 27-December 08

Re: Displaying an ArrayList into labels using a for loop in a gui.

Posted 18 November 2011 - 09:48 PM

First, please avoid the NetBeans GUI Builder. It produces horrid, unmaintainable code that we cannot read, and therefore do not support. Second, ArrayLists are generic. So if you are storing Integers in it, you should use an ArrayList<Integer>. Third, you really just need a loop to set the text of the JLabels. My point about using a JLabel[] still stands.

for(int i = 0; i < labels.length; i++){
    labels[i] = new JLabel(i + "");
    //add labels[i] to the appropriate Container
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1