Patrick Reck
Patrick Reck

Reputation: 303

JList ArrayList 3 items from each object

I have this method that sets my JList of days to each Day object in my ArrayList

public void setCalender(ArrayList<Day> calender) {
    this.calender = calender;
    listDays.setListData(this.calender.toArray());
}

Each day contains 3 Period objects. Currently the list renders as:

Day1
Day2
Day3

However I want the user to also be able to select a period, by rendering

Day1: Period1
Day1: Period2
Day1: Period3
Day2: Period1
Day3: Period2
Day3: Period3

..and so on. How can I achieve this?

Upvotes: 0

Views: 118

Answers (1)

Juvanis
Juvanis

Reputation: 25950

Assuming you have a method like getPeriods() which returns the period list of a specific day, you can use this code:

ArrayList<Day> calender;

ArrayList<Period> periods = new ArrayList<Period>();

for(Day d: calender)
{
   for(Period p : d.getPeriods())
   {
       periods.add(p);
   }
}
listDays.setListData(periods.toArray());

Upvotes: 2

Related Questions