Hello,
My previous post told of how I was learning about threads, objects, swings and more. After learning studying I made my own simple program using basic swing and I was wondering if I could have made it simpler?What this program does is allows the user to enter the employee's first and last name along with age. Then allows them to select their gender. If needed the user can print all employees he/she has made and even remove employees from the list. I'm not posting all of it just the "core".
Thanks to anyone that just looks and BIG thanks to those who reply.
The parts I cut out were the variable initializing, creating the jframe, panels, etc... and the main method. This program uses a custom Object called Employee which holds First/Last name, gender and age.public void addEmployee() { getName = Name.getText(); getLName = LName.getText(); getGender = (String)gender.getSelectedItem(); area.setText("You added " + getName + " " + getLName + " Age " + getAge + " Gender " + getGender); arrfill++; employList.add(new Employee(getName,getLName,getAge,getGender)); list.addItem(getName + " " + getLName); } public void removeEmployee() { if (list.getItemCount() == 0) { area.setText("Nothing to Remove!"); } else { int count = 0; String compare1; String FName; String LName; Employee getcompare; String compare2; while(true) { compare1 = (String)list.getSelectedItem(); getcompare = employList.get(count); FName = getcompare.getName(); LName = getcompare.getLastName(); compare2 = FName + " " + LName; if (compare2.equals(compare1)) { employList.remove(count); list.removeItemAt(count); arrfill--; break; } else { count++; } } } } public void printAllemps() { String print; Employee temp; area.setText(""); for (int count = 0; count <= arrfill; count++) { temp = employList.get(count); print = temp.getName()+ " " + temp.getLastName() + " " + temp.getAge() + " " + temp.getGender(); area.append(print + "\n"); //creates new line } } public boolean isInt(JTextField f) { try { Integer.parseInt(f.getText()); return true; } catch(NumberFormatException e) { area.setText("Re enter age"); f.requestFocus(); return false; } } public void actionPerformed(ActionEvent e) { if(e.getSource() == add) { if (isInt(Age)) { getAge = Integer.parseInt(Age.getText()); addEmployee(); } } else if (e.getSource() == remove) { removeEmployee(); } else if (e.getSource() == all) { printAllemps(); } }
Any questions just ask.
lil_misfit