I currently have a set of buttons which when pressed, update an element of an array with their value and then close the frame which contains them.
Currently, their actions are set out like this:
...
svwnb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
input[5] = svwnb.getText();
dftframe.setVisible(false);
}
});
blypb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
input[5] = blypb.getText();
dftframe.setVisible(false);
}
});
pw91b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
input[5] = pw91b.getText();
dftframe.setVisible(false);
}
});
b97db.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
input[5] = b97db.getText();
dftframe.setVisible(false);
}
});
pbepbesolb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
input[5] = pbepbesolb.getText();
dftframe.setVisible(false);
}
});
...
Is there a way of simplifying this into a simple method? I’ve got a feeling I’ll need to use the getSource() method but I have no experience in constructing an action method like this.
To be more succinct, I have an idea that the code will look something like the following pseudocode:
public void actionPerformed(ActionEvent e){
input[5] = e.getSource().getText();
dftframe.setVisible(false);
}
I just don’t know exactly how to generate this code.
You can define the ActionListener in the following way:
And use it by adding it to each component: