caleb wheeler
caleb wheeler

Reputation: 57

Easier way to call same method for multiple variables

Is it possible to not have to re type all of these almost identical procedures?

cancelSaveContactBtn.setVisible(false);
saveContactBtn.setVisible(false);
addContactBtn.setVisible(true);

and

selectContactCBox.addActionListener(this);
addContactBtn.addActionListener(this);
personalRadio.addActionListener(this);
businessRadio.addActionListener(this);

Upvotes: 2

Views: 158

Answers (1)

rze
rze

Reputation: 248

Technically if you intend to call the same method with the same parameter on a set of objects you could use a List to store your objects, iterate through and set an addActionListener to each one.

List<Object> objects = new ArrayList<Object>();

objects.add(selectContactCBox);
objects.add(addContactBtn);
objects.add(personalRadio);
objects.add(businessRadio);

for(object o: objects){
o.addActionListener(this);
}

Upvotes: 3

Related Questions