• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
He was expelled for perverse baking experiments. This tiny ad is a model student:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic