aspose file tools*
The moose likes Java in General and the fly likes How do I triggers object and add them to the collection? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How do I triggers object and add them to the collection?" Watch "How do I triggers object and add them to the collection?" New topic
Author

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

kei hosima
Greenhorn

Joined: Feb 23, 2003
Posts: 13
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

Joined: Nov 04, 2001
Posts: 1871
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

Joined: Nov 04, 2001
Posts: 1871
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

Joined: Feb 23, 2003
Posts: 13
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do I triggers object and add them to the collection?
 
Similar Threads
HashMap Issues and Such
TreeMap: why aren't these code working?
If-then conversion of textfields for calc
Identifier expected
Adding Exception error problems