Hey everyone,
I want to add action listeners to JButtons that were created dynamically by an ArrayList. Here is the code for the button creation.
numOfCourse = 6;
courseBtnArray.add(new JButton("Course 1"));
gbc.gridx = 2;
for (int i = 0; i < numOfCourses; i++) {
gbc.gridy++;
add(courseBtnArray.get(i), gbc);
courseBtnArray.add(new JButton("Course " + (i + 2)));
}
gbc.gridy++;
add(courseBtnArray.get(courseBtnArray.size() - 1), gbc);
System.out.println("the size of the textbox arraylist is:" + courseBtnArray.size());
So how can i refer to these buttons because they themseleves don't have a referance.
Here is one way: courseBtnArray.get(1).addActionListener(clickListener);
. This works, but the user does have the ability to add a greater number of courses than 6. So this way any buttons great than index 5 would be useless.
So what can I do?
Thanks