I am creating a simple program with multiple buttons. Is this way of checking which button was pressed is ok? Is it better to use switch..case.. statement?
public class KennelGUI extends JFrame implements ActionListener{
private String filename; // holds the name of the file
private Kennel kennel; // holds the kennel
int buttonsNumber = 10;
private JButton[] buttons = new JButton[buttonsNumber];
private KennelGUI() {
super("Kennel with GUI");
do{
filename = JOptionPane.showInputDialog("Please enter the filename of kennel information: ");
}while(filename==null || filename.isEmpty()); //not good
kennel = new Kennel();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocation(300,100);
setSize(200,500);
setVisible(true);
setLayout(new FlowLayout());
buttons[0] = new JButton("Add dog");
buttons[1] = new JButton("Add cat");
buttons[2] = new JButton("Add monkey");
buttons[3] = new JButton("Set capacity");
buttons[4] = new JButton("Print dogs with bones");
buttons[5] = new JButton("Print all animals");
buttons[6] = new JButton("Remove dog");
buttons[7] = new JButton("Set kennel name");
buttons[8] = new JButton("Search for pet");
buttons[9] = new JButton("Quit");
for(int i=0;i<buttonsNumber;i++)
{
add(buttons[i]);
buttons[i].addActionListener(this);
buttons[i].setPreferredSize(new Dimension(190, 30));
}
}
/*
* initialise() method runs from the main and reads from a file
*/
private void initialise() {
JOptionPane.showMessageDialog(null, "Using file " + filename);
try(FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
Scanner infile = new Scanner(br)){
Animal newAnimal = null;
String kennelName = infile.nextLine();
kennel.setName(kennelName);
int kennelSize = infile.nextInt();
infile.nextLine();
kennel.setCapacity(kennelSize);
int numPets = infile.nextInt();
infile.nextLine();
for(int i=0; i < numPets; i++){
String type = infile.nextLine();
switch(type)
{
case "Dog":
newAnimal = new Dog();
break;
case "Cat":
newAnimal = new Cat();
break;
case "Monkey":
newAnimal = new Monkey();
break;
default:
throw new IOException("Type problem");//GUI?
}
newAnimal.load(infile);
kennel.addAnimal(newAnimal);
}
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(null,"The file: " + filename + " does not exist.\nAssuming first use and an empty file.\n" +
"If this is not the first use then have you \naccidentally deleted the file?","Warning",JOptionPane.WARNING_MESSAGE);
} catch (IOException e) {
JOptionPane.showMessageDialog(null,"An unexpected error occurred when trying to open the file " + filename,"Warning",JOptionPane.WARNING_MESSAGE);
}
}
@Override
public void actionPerformed(ActionEvent e) {
switch(e.getSource()){
case buttons[0]:
admitDog();
break;
case buttons[1]:
admitCat();
break;
case buttons[2]:
admitDog();
break;
case buttons[3]:
setKennelCapacity();
break;
case buttons[4]:
printDogsWithBones();
break;
case buttons[5]:
printAll();
break;
case buttons[6]:
removePet();
break;
case buttons[7]:
changeKennelName();
break;
case buttons[8]:
searchForPet();
break;
case buttons[9]:
save();
System.exit(0);
break;
}
}
}