Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I triggers object and add them to the collection?

 
kei hosima
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello.
I'm using TreeMap for a collection.
In a single source file named App.java, there is an Employee class to encapsulate employee data and an App class defining an applet to maintain a collection of Employee objects.
private instance variables for name(string) and pay rate (double) are in the Employee class.
There is an Add button.
I want to triggers the construction of an Employee object having the currently displayed name and pay rate and the addition of the object to the collection when clicking 'Add' button.
The following is the part of my code.
<CODE>
SortedMap m = new TreeMap();
addBtn = new Button ("Add");
addBtn.setBackground(Color.red);
addBtn.setForeground(Color.black);
addBtn.addActionListener(
new ActionListener() {
public void actionPerformed (ActionEvent e){
// check to see if name field is empty or not.
if (e.getSource().equals(nameField)){
if ((nameField.getText()).equals("")){
msg.setText ("Please Enter an Employee Name");
}}
// check to see if name field is empty or not.
if (e.getSource().equals(payRateField)){
if ((payRateField.getText()).equals("")){
msg.setText ("Please Enter a Pay Rate");
}}
// check to see if the name is already in the collection or not.
// If it's not in the collection, add the record.
if (m.containsKey(nameField.getText())){
msg.setText ("The employee is already in our collection");
}
else {
// add the employee record.
m.put(name, new String(setName));
}
</CODE>
This is not working at all.
I would appreciate any suggestion/help/advice.
Thank you very much in advance.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi kie,
if i'd have been there i'd have used Employee Bean instead that notifies my Applet object upon ,hitting "OK" button say, with the Employee object created and then my applet will add that to its private collection..
roughly i mean following...
1. say you have a JFrame (i assume u use Swing) where you have put text box for Emplyee name and pay rate..
2. you have an employee class as you mentioned..

as u extend JFrame u don't have to implement addPropertyChangeListener() etc methods...
now,
ur Applet will implement PropertyChangeListener and will have,

of course u 've to do lot of little things like adding applet as a propertychange listener to the EmployeeFrame object etc...but that u can learn more via sun tutorial or something about using Beans...
regards
maulin.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
btw,
u'll have to create object of EmployeeFrame and then show it on say "Add Employee" button click on the Applet then it would go form there as it will act as a bean and notify the applet upon "addEmployee" button on the EmployeeFrame..
regards
maulin...
 
kei hosima
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Maulin.
Thank you very much for your suggestion.
I'll try to develop my code based on your suggestion.
thank you for your time.
Regard
Kei
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic