Results 1 to 13 of 13
  1. #1
    simonsays415 is offline Member
    Join Date
    Jul 2010
    Posts
    7
    Rep Power
    0

    Default NOOB needs help with buttons and events, GUIs.

    Ok, so i have been spending the past week looking for direct answers to my problems and I have been able to get some answers from senor Google, but they are only bits and pieces of the whole answer that i am looking for.......in other words. i'm still STUCK!

    Please bear with me, here.

    to begin:

    I have a superclass (Employee) with subclasses (Secretary and Supervisor) and a CompanyApp that is a GUI window.

    This GUI window accepts user input for the employee's name, id, salary, etc. I am trying to have my buttons do the following things:

    add - add new employee object to arrayList <--I think I have this figured out, i think

    delete - delete employee from arrayList after employee has been searched and found (i know that I am supposed to use the .remove(index) method, but what if I don't know where the employee is? i.e. an employee that is found after a search)

    edit - after searching for an employee, i want to edit the info and have it saved so when i search again, the new information will be there

    search - i want to search an employee using "id" as a parameter. but how?!

    cancel - this will clear the text fields, i think that i should be good on this.....

    i have tried the different methods in the arraylist class and keep on coming up with "identifier expected" "can't find variable", etc etc....

    i have attached the code. did i mention that i'm a noob? flame when appropriate. THANKS!

    simon

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.TitledBorder;
    
    public class CompanyApp extends JFrame {
      
        // Create employees, secretaries, and supervisors
      Employee employee1 = 
      new Employee("John", "Smith", "Worker", 1, 40000, 234, "Active", "none");
      Employee employee2 = 
      new Employee("Miike", "Snow", "Rapper", 2, 50000, 233, "Inactive", "none");
      
      Secretary secretary1 = 
      new Secretary("Albert", "Commons", "Secretary", 11, 60000, 334, "Active", "none");
      Secretary secretary2 = 
      new Secretary("Jonah", "Johnson", "Secretary", 17, 35000, 235, "Active", "none");
      
      Supervisor supervisor1 = 
      new Supervisor("Barack", "Obama", "President", 1600, 300000, 324, "Active", "Jonah Johnson");
      Supervisor supervisor2 = 
      new Supervisor("Joe", "Biden", "Vice-President", 1600, 250000, 322, "Active", "Julius Caesar");
      
      
    
      // Create an employeeArray to hold the employees
      java.util.ArrayList<Employee> employeeList = new java.util.ArrayList<Employee>();
      
    
      
      
     
      /** Create text fields for employee information
       *  First name,last name, title, id, salary, office, status, secretary 
       */
      private JTextField jtfFName = new JTextField();  // add first name
      private JTextField jtfLName = new JTextField();  // add last name
      private JTextField jtfTitle = new JTextField(); // add title
      private JTextField jtfID= new JTextField();  // add ID
      private JTextField jtfSalary = new JTextField(); // add salary  
      private JTextField jtfOffice = new JTextField(); // add office
      private JTextField jtfStatus = new JTextField(); // add Status
      private JTextField jtfSecretary = new JTextField(); // add Secretary
    
      // Create add, delete, edit, submit, search and cancel buttons
      private JButton jbtAdd = new JButton("Add"); // add add button
      private JButton jbtDelete = new JButton("Delete"); // add delete button
      private JButton jbtEdit = new JButton("Edit"); // add edit button
      private JButton jbtSearch = new JButton("Search"); // add search button
      private JButton jbtCancel = new JButton("Cancel");  // add cancel button
      
      public CompanyApp () {
        // Panel p1 to hold labels and text fields
        JPanel p1 = new JPanel(new GridLayout(5, 2));
        p1.add(new JLabel("First Name: "));
        p1.add(jtfFName);
        p1.add(new JLabel("Last Name: "));
        p1.add(jtfLName);
        p1.add(new JLabel("Title: "));
        p1.add(jtfTitle);
        p1.add(new JLabel("ID: "));
        p1.add(jtfID);
        p1.add(new JLabel("Salary: "));    
        p1.add(jtfSalary);
        p1.add(new JLabel("Office: "));
        p1.add(jtfOffice);
        p1.add(new JLabel("Status: "));
        p1.add(jtfStatus);
        p1.add(new JLabel("Secretary: "));
        p1.add(jtfSecretary);
        p1.setBorder(new TitledBorder("Enter Employee Information"));
    
        // Panel p2 to hold the buttons
        JPanel p2 = new JPanel(new GridLayout(1, 2));
        p2.add(jbtAdd);
        p2.add(jbtDelete);
        p2.add(jbtEdit);
        p2.add(jbtSearch);
        p2.add(jbtCancel);
    
        // Add the panel to the frame
        add(p1, BorderLayout.CENTER);
        add(p2, BorderLayout.SOUTH);
      
        jbtAdd.addActionListener( 
          new ActionListener() {
              public void actionPerformed(ActionEvent e) {  
                  String firstName = jtfFName.getText();
                  String lastName = jtfLName.getText();
                  String title = jtfTitle.getText();
                  int id = Integer.parseInt(jtfID.getText());
                  double salary = Double.parseDouble(jtfSalary.getText());
                  int office = Integer.parseInt(jtfOffice.getText());
                  String status = jtfStatus.getText();
                  String secretary = jtfSecretary.getText();
                  
                  
                 
                  
                  // Create an employee object
                  if (id >= 10 && id > 0) {
                    Employee employee = new Employee(firstName, lastName, title, id, salary, 
                    office, status, secretary);     
                    
                  }
                  // Create a secretary object
                    else if (id >= 20 && id > 11) {
                    Secretary secretary1 = new Secretary(firstName, lastName, title, id, salary, 
                    office, status, secretary);
    
                  }
                  
                  // Create a supervisor object
                    else {
                    Supervisor supervisor = new Supervisor(firstName, lastName, title, id, salary, 
                    office, status, secretary);
    
                 }
                
                 
                 
                 employeeList.add(new Employee(firstName, lastName, title, id, salary, 
                   office, status,  secretary));
                   
                  // Clear text fields after a button is clicked
                  jtfFName.setText("");
                  jtfLName.setText("");
                  jtfTitle.setText("");
                  jtfID.setText("");
                  jtfSalary.setText("");
                  jtfOffice.setText("");
                  jtfStatus.setText("");
                  jtfSecretary.setText("");
                  
                  
                  
                  
                  
                  System.out.println("A new employee has been hired!");
                  System.out.println("There are now " + employeeList.size() + " employees in the company");
    
                 }
            }
            );
         
        jbtDelete.addActionListener(
          new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  
                  // Clear text fields after a button is clicked
                  jtfFName.setText("");
                  jtfLName.setText("");
                  jtfTitle.setText("");
                  jtfID.setText("");
                  jtfSalary.setText("");
                  jtfOffice.setText("");
                  jtfStatus.setText("");
                  jtfSecretary.setText("");
                  
                  System.out.println("Somebody isn't getting a paycheck anymore, " +
                  "because you just fired them!");
                  System.out.println("There are now " + employeeList.size() + " employees in the company");
    
    
                }
            }
            );
        
        jbtEdit.addActionListener(
          new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  String firstName = jtfFName.getText();
                  String lastName = jtfLName.getText();
                  String title = jtfTitle.getText();
                  int id = Integer.parseInt(jtfID.getText());
                  double salary = Double.parseDouble(jtfSalary.getText());
                  int office = Integer.parseInt(jtfOffice.getText());
                  String status = jtfStatus.getText();
                  String secretary = jtfSecretary.getText();
                  System.out.println("An employee's information has been edited!");
                  
                  // Clear text fields after a button is clicked
                  jtfFName.setText("");
                  jtfLName.setText("");
                  jtfTitle.setText("");
                  jtfID.setText("");
                  jtfSalary.setText("");
                  jtfOffice.setText("");
                  jtfStatus.setText("");
                  jtfSecretary.setText("");
                }
            }
            );
        jbtSearch.addActionListener(
          new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  int id = Integer.parseInt(jtfID.getText());
                  System.out.println("An employee's information has been searched!");
                  
                }
            }
            );
            
        jbtCancel.addActionListener(
          new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  System.out.println("The text fields are cleared!");
                  
                  
                  // Clear text fields after a button is clicked
                  jtfFName.setText("");
                  jtfLName.setText("");
                  jtfTitle.setText("");
                  jtfID.setText("");
                  jtfSalary.setText("");
                  jtfOffice.setText("");
                  jtfStatus.setText("");
                  jtfSecretary.setText("");
                }
            }
            );
            
            employeeList.add(employee1);
            employeeList.add(employee2);
            employeeList.add(secretary1);
            employeeList.add(secretary2);
            employeeList.add(supervisor1);
            employeeList.add(supervisor2);
      }
      
      public static void main(String[] args) {      
        CompanyApp frame = new CompanyApp();
        frame.setTitle("Employee Registration");
        frame.setLocationRelativeTo(null); // Center the frame
        frame.setSize(600, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
      }
    }


    Java Code:
    /**
     * This is my final project
     *
     * @author [moderator removed]
     * @version 1.0 July 7, 2010
     * 
     */
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.TitledBorder;
    
    public class CompanyApp extends JFrame {
      
        // Create employees, secretaries, and supervisors
      Employee employee1 = 
      new Employee("John", "Smith", "Worker", 1, 40000, 234, "Active", "none");
      Employee employee2 = 
      new Employee("Miike", "Snow", "Rapper", 2, 50000, 233, "Inactive", "none");
      
      Secretary secretary1 = 
      new Secretary("Albert", "Commons", "Secretary", 11, 60000, 334, "Active", "none");
      Secretary secretary2 = 
      new Secretary("Jonah", "Johnson", "Secretary", 17, 35000, 235, "Active", "none");
      
      Supervisor supervisor1 = 
      new Supervisor("Barack", "Obama", "President", 1600, 300000, 324, "Active", "Jonah Johnson");
      Supervisor supervisor2 = 
      new Supervisor("Joe", "Biden", "Vice-President", 1600, 250000, 322, "Active", "Julius Caesar");
      
      
    
      // Create an employeeArray to hold the employees
      java.util.ArrayList<Employee> employeeList = new java.util.ArrayList<Employee>();
      
    
      
      
     
      /** Create text fields for employee information
       *  First name,last name, title, id, salary, office, status, secretary 
       */
      private JTextField jtfFName = new JTextField();  // add first name
      private JTextField jtfLName = new JTextField();  // add last name
      private JTextField jtfTitle = new JTextField(); // add title
      private JTextField jtfID= new JTextField();  // add ID
      private JTextField jtfSalary = new JTextField(); // add salary  
      private JTextField jtfOffice = new JTextField(); // add office
      private JTextField jtfStatus = new JTextField(); // add Status
      private JTextField jtfSecretary = new JTextField(); // add Secretary
    
      // Create add, delete, edit, submit, search and cancel buttons
      private JButton jbtAdd = new JButton("Add"); // add add button
      private JButton jbtDelete = new JButton("Delete"); // add delete button
      private JButton jbtEdit = new JButton("Edit"); // add edit button
      private JButton jbtSearch = new JButton("Search"); // add search button
      private JButton jbtCancel = new JButton("Cancel");  // add cancel button
      
      public CompanyApp () {
        // Panel p1 to hold labels and text fields
        JPanel p1 = new JPanel(new GridLayout(5, 2));
        p1.add(new JLabel("First Name: "));
        p1.add(jtfFName);
        p1.add(new JLabel("Last Name: "));
        p1.add(jtfLName);
        p1.add(new JLabel("Title: "));
        p1.add(jtfTitle);
        p1.add(new JLabel("ID: "));
        p1.add(jtfID);
        p1.add(new JLabel("Salary: "));    
        p1.add(jtfSalary);
        p1.add(new JLabel("Office: "));
        p1.add(jtfOffice);
        p1.add(new JLabel("Status: "));
        p1.add(jtfStatus);
        p1.add(new JLabel("Secretary: "));
        p1.add(jtfSecretary);
        p1.setBorder(new TitledBorder("Enter Employee Information"));
    
        // Panel p2 to hold the buttons
        JPanel p2 = new JPanel(new GridLayout(1, 2));
        p2.add(jbtAdd);
        p2.add(jbtDelete);
        p2.add(jbtEdit);
        p2.add(jbtSearch);
        p2.add(jbtCancel);
    
        // Add the panel to the frame
        add(p1, BorderLayout.CENTER);
        add(p2, BorderLayout.SOUTH);
      
        jbtAdd.addActionListener( 
          new ActionListener() {
              public void actionPerformed(ActionEvent e) {  
                  String firstName = jtfFName.getText();
                  String lastName = jtfLName.getText();
                  String title = jtfTitle.getText();
                  int id = Integer.parseInt(jtfID.getText());
                  double salary = Double.parseDouble(jtfSalary.getText());
                  int office = Integer.parseInt(jtfOffice.getText());
                  String status = jtfStatus.getText();
                  String secretary = jtfSecretary.getText();
                  
                  
                 
                  
                  // Create an employee object
                  if (id >= 10 && id > 0) {
                    Employee employee = new Employee(firstName, lastName, title, id, salary, 
                    office, status, secretary);     
                    
                  }
                  // Create a secretary object
                    else if (id >= 20 && id > 11) {
                    Secretary secretary1 = new Secretary(firstName, lastName, title, id, salary, 
                    office, status, secretary);
    
                  }
                  
                  // Create a supervisor object
                    else {
                    Supervisor supervisor = new Supervisor(firstName, lastName, title, id, salary, 
                    office, status, secretary);
    
                 }
                
                 
                 
                 employeeList.add(new Employee(firstName, lastName, title, id, salary, 
                   office, status,  secretary));
                   
                  // Clear text fields after a button is clicked
                  jtfFName.setText("");
                  jtfLName.setText("");
                  jtfTitle.setText("");
                  jtfID.setText("");
                  jtfSalary.setText("");
                  jtfOffice.setText("");
                  jtfStatus.setText("");
                  jtfSecretary.setText("");
                  
                  
                  
                  
                  
                  System.out.println("A new employee has been hired!");
                  System.out.println("There are now " + employeeList.size() + " employees in the company");
    
                 }
            }
            );
         
        jbtDelete.addActionListener(
          new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  
                  // Clear text fields after a button is clicked
                  jtfFName.setText("");
                  jtfLName.setText("");
                  jtfTitle.setText("");
                  jtfID.setText("");
                  jtfSalary.setText("");
                  jtfOffice.setText("");
                  jtfStatus.setText("");
                  jtfSecretary.setText("");
                  
                  System.out.println("Somebody isn't getting a paycheck anymore, " +
                  "because you just fired them!");
                  System.out.println("There are now " + employeeList.size() + " employees in the company");
    
    
                }
            }
            );
        
        jbtEdit.addActionListener(
          new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  String firstName = jtfFName.getText();
                  String lastName = jtfLName.getText();
                  String title = jtfTitle.getText();
                  int id = Integer.parseInt(jtfID.getText());
                  double salary = Double.parseDouble(jtfSalary.getText());
                  int office = Integer.parseInt(jtfOffice.getText());
                  String status = jtfStatus.getText();
                  String secretary = jtfSecretary.getText();
                  System.out.println("An employee's information has been edited!");
                  
                  // Clear text fields after a button is clicked
                  jtfFName.setText("");
                  jtfLName.setText("");
                  jtfTitle.setText("");
                  jtfID.setText("");
                  jtfSalary.setText("");
                  jtfOffice.setText("");
                  jtfStatus.setText("");
                  jtfSecretary.setText("");
                }
            }
            );
        jbtSearch.addActionListener(
          new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  int id = Integer.parseInt(jtfID.getText());
                  System.out.println("An employee's information has been searched!");
                  
                }
            }
            );
            
        jbtCancel.addActionListener(
          new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  System.out.println("The text fields are cleared!");
                  
                  
                  // Clear text fields after a button is clicked
                  jtfFName.setText("");
                  jtfLName.setText("");
                  jtfTitle.setText("");
                  jtfID.setText("");
                  jtfSalary.setText("");
                  jtfOffice.setText("");
                  jtfStatus.setText("");
                  jtfSecretary.setText("");
                }
            }
            );
            
            employeeList.add(employee1);
            employeeList.add(employee2);
            employeeList.add(secretary1);
            employeeList.add(secretary2);
            employeeList.add(supervisor1);
            employeeList.add(supervisor2);
      }
      
      public static void main(String[] args) {      
        CompanyApp frame = new CompanyApp();
        frame.setTitle("Employee Registration");
        frame.setLocationRelativeTo(null); // Center the frame
        frame.setSize(600, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
      }
    }


    Java Code:
    /**
     * This is my final project
     *
     * @author ....
     * @version 1.0 July 7, 2010
     * 
     */
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.TitledBorder;
    
    public class CompanyApp extends JFrame {
      
        // Create employees, secretaries, and supervisors
      Employee employee1 = 
      new Employee("John", "Smith", "Worker", 1, 40000, 234, "Active", "none");
      Employee employee2 = 
      new Employee("Miike", "Snow", "Rapper", 2, 50000, 233, "Inactive", "none");
      
      Secretary secretary1 = 
      new Secretary("Albert", "Commons", "Secretary", 11, 60000, 334, "Active", "none");
      Secretary secretary2 = 
      new Secretary("Jonah", "Johnson", "Secretary", 17, 35000, 235, "Active", "none");
      
      Supervisor supervisor1 = 
      new Supervisor("Barack", "Obama", "President", 1600, 300000, 324, "Active", "Jonah Johnson");
      Supervisor supervisor2 = 
      new Supervisor("Joe", "Biden", "Vice-President", 1600, 250000, 322, "Active", "Julius Caesar");
      
      
    
      // Create an employeeArray to hold the employees
      java.util.ArrayList<Employee> employeeList = new java.util.ArrayList<Employee>();
      
    
      
      
     
      /** Create text fields for employee information
       *  First name,last name, title, id, salary, office, status, secretary 
       */
      private JTextField jtfFName = new JTextField();  // add first name
      private JTextField jtfLName = new JTextField();  // add last name
      private JTextField jtfTitle = new JTextField(); // add title
      private JTextField jtfID= new JTextField();  // add ID
      private JTextField jtfSalary = new JTextField(); // add salary  
      private JTextField jtfOffice = new JTextField(); // add office
      private JTextField jtfStatus = new JTextField(); // add Status
      private JTextField jtfSecretary = new JTextField(); // add Secretary
    
      // Create add, delete, edit, submit, search and cancel buttons
      private JButton jbtAdd = new JButton("Add"); // add add button
      private JButton jbtDelete = new JButton("Delete"); // add delete button
      private JButton jbtEdit = new JButton("Edit"); // add edit button
      private JButton jbtSearch = new JButton("Search"); // add search button
      private JButton jbtCancel = new JButton("Cancel");  // add cancel button
      
      public CompanyApp () {
        // Panel p1 to hold labels and text fields
        JPanel p1 = new JPanel(new GridLayout(5, 2));
        p1.add(new JLabel("First Name: "));
        p1.add(jtfFName);
        p1.add(new JLabel("Last Name: "));
        p1.add(jtfLName);
        p1.add(new JLabel("Title: "));
        p1.add(jtfTitle);
        p1.add(new JLabel("ID: "));
        p1.add(jtfID);
        p1.add(new JLabel("Salary: "));    
        p1.add(jtfSalary);
        p1.add(new JLabel("Office: "));
        p1.add(jtfOffice);
        p1.add(new JLabel("Status: "));
        p1.add(jtfStatus);
        p1.add(new JLabel("Secretary: "));
        p1.add(jtfSecretary);
        p1.setBorder(new TitledBorder("Enter Employee Information"));
    
        // Panel p2 to hold the buttons
        JPanel p2 = new JPanel(new GridLayout(1, 2));
        p2.add(jbtAdd);
        p2.add(jbtDelete);
        p2.add(jbtEdit);
        p2.add(jbtSearch);
        p2.add(jbtCancel);
    
        // Add the panel to the frame
        add(p1, BorderLayout.CENTER);
        add(p2, BorderLayout.SOUTH);
      
        jbtAdd.addActionListener( 
          new ActionListener() {
              public void actionPerformed(ActionEvent e) {  
                  String firstName = jtfFName.getText();
                  String lastName = jtfLName.getText();
                  String title = jtfTitle.getText();
                  int id = Integer.parseInt(jtfID.getText());
                  double salary = Double.parseDouble(jtfSalary.getText());
                  int office = Integer.parseInt(jtfOffice.getText());
                  String status = jtfStatus.getText();
                  String secretary = jtfSecretary.getText();
                  
                  
                 
                  
                  // Create an employee object
                  if (id >= 10 && id > 0) {
                    Employee employee = new Employee(firstName, lastName, title, id, salary, 
                    office, status, secretary);     
                    
                  }
                  // Create a secretary object
                    else if (id >= 20 && id > 11) {
                    Secretary secretary1 = new Secretary(firstName, lastName, title, id, salary, 
                    office, status, secretary);
    
                  }
                  
                  // Create a supervisor object
                    else {
                    Supervisor supervisor = new Supervisor(firstName, lastName, title, id, salary, 
                    office, status, secretary);
    
                 }
                
                 
                 
                 employeeList.add(new Employee(firstName, lastName, title, id, salary, 
                   office, status,  secretary));
                   
                  // Clear text fields after a button is clicked
                  jtfFName.setText("");
                  jtfLName.setText("");
                  jtfTitle.setText("");
                  jtfID.setText("");
                  jtfSalary.setText("");
                  jtfOffice.setText("");
                  jtfStatus.setText("");
                  jtfSecretary.setText("");
                  
                  
                  
                  
                  
                  System.out.println("A new employee has been hired!");
                  System.out.println("There are now " + employeeList.size() + " employees in the company");
    
                 }
            }
            );
         
        jbtDelete.addActionListener(
          new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  
                  // Clear text fields after a button is clicked
                  jtfFName.setText("");
                  jtfLName.setText("");
                  jtfTitle.setText("");
                  jtfID.setText("");
                  jtfSalary.setText("");
                  jtfOffice.setText("");
                  jtfStatus.setText("");
                  jtfSecretary.setText("");
                  
                  System.out.println("Somebody isn't getting a paycheck anymore, " +
                  "because you just fired them!");
                  System.out.println("There are now " + employeeList.size() + " employees in the company");
    
    
                }
            }
            );
        
        jbtEdit.addActionListener(
          new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  String firstName = jtfFName.getText();
                  String lastName = jtfLName.getText();
                  String title = jtfTitle.getText();
                  int id = Integer.parseInt(jtfID.getText());
                  double salary = Double.parseDouble(jtfSalary.getText());
                  int office = Integer.parseInt(jtfOffice.getText());
                  String status = jtfStatus.getText();
                  String secretary = jtfSecretary.getText();
                  System.out.println("An employee's information has been edited!");
                  
                  // Clear text fields after a button is clicked
                  jtfFName.setText("");
                  jtfLName.setText("");
                  jtfTitle.setText("");
                  jtfID.setText("");
                  jtfSalary.setText("");
                  jtfOffice.setText("");
                  jtfStatus.setText("");
                  jtfSecretary.setText("");
                }
            }
            );
        jbtSearch.addActionListener(
          new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  int id = Integer.parseInt(jtfID.getText());
                  System.out.println("An employee's information has been searched!");
                  
                }
            }
            );
            
        jbtCancel.addActionListener(
          new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  System.out.println("The text fields are cleared!");
                  
                  
                  // Clear text fields after a button is clicked
                  jtfFName.setText("");
                  jtfLName.setText("");
                  jtfTitle.setText("");
                  jtfID.setText("");
                  jtfSalary.setText("");
                  jtfOffice.setText("");
                  jtfStatus.setText("");
                  jtfSecretary.setText("");
                }
            }
            );
            
            employeeList.add(employee1);
            employeeList.add(employee2);
            employeeList.add(secretary1);
            employeeList.add(secretary2);
            employeeList.add(supervisor1);
            employeeList.add(supervisor2);
      }
      
      public static void main(String[] args) {      
        CompanyApp frame = new CompanyApp();
        frame.setTitle("Employee Registration");
        frame.setLocationRelativeTo(null); // Center the frame
        frame.setSize(600, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
      }
    }
    Last edited by Fubarable; 07-16-2010 at 10:31 PM. Reason: Moderator Edit: Code tags added. also email address removed

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,317
    Rep Power
    25

    Default

    keep on coming up with "identifier expected" "can't find variable", etc
    Please copy full text of error message and paste it here. Here is a sample:
    TestSorts.java:138: cannot find symbol
    symbol : variable var
    location: class TestSorts
    var = 2;
    ^
    Also Please put your code in code tags to preserve formatting: [ code] and [/ code] without spaces.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I'll add the code tags for you in the post above, but in future posts, please follow Norm's suggestion and place the tag [cod&#101;] above your code block and [/cod&#101;] below your code block. Much luck!

  4. #4
    simonsays415 is offline Member
    Join Date
    Jul 2010
    Posts
    7
    Rep Power
    0

    Default

    right now, the program should compile and runs fine.

    here are my issues:

    add button - I can add an employee to the employeeList array, but once i exit out of the program, any employee objects created are deleted and the count starts again from my initialized array.

    edit button - I tried employeeList.remove(Employee); <-- this provides an error message such as: "variable Employee not found". But I am not trying to delete a specific employee at a specific index. I would like to delete the employee that is searched and found

    search button - i'm not sure how to approach searching an Employee object (employee, secretary, supervisor) with specific parameters. from what my textbook has examples of, and from what i have found on the internet, i am only able to search with one (1) given int, double, char, etc. but i have an object with multiple parameters.

    it may be easier if i were just displaying all of these results onto a console, but I need all of this to be done within a GUI.

    edit button - after an employee has been searched and found, I would like to edit the certain information and keep other information untouched.

    my textbook provides loose examples of simple programs, i have read the chapters through and through, researched the internet, did troubleshooting and debugging so i have spent a lot of time finding these answers, but none of them point me in the right direction! thank you so much for any help and advice that is provided.

    cheers,

    simon


    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.TitledBorder;
    
    public class CompanyApp extends JFrame {
      
        // Create employees, secretaries, and supervisors
      Employee employee1 = 
      new Employee("John", "Smith", "Worker", 1, 40000, 234, "Active", "none");
      Employee employee2 = 
      new Employee("Miike", "Snow", "Rapper", 2, 50000, 233, "Inactive", "none");
      
      Secretary secretary1 = 
      new Secretary("Albert", "Commons", "Secretary", 11, 60000, 334, "Active", "none");
      Secretary secretary2 = 
      new Secretary("Jonah", "Johnson", "Secretary", 17, 35000, 235, "Active", "none");
      
      Supervisor supervisor1 = 
      new Supervisor("Barack", "Obama", "President", 1600, 300000, 324, "Active", "Jonah Johnson");
      Supervisor supervisor2 = 
      new Supervisor("Joe", "Biden", "Vice-President", 1600, 250000, 322, "Active", "Julius Caesar");
      
      
    
      // Create an employeeArray to hold the employees
      java.util.ArrayList<Employee> employeeList = new java.util.ArrayList<Employee>();
      
    
      
      
     
      /** Create text fields for employee information
       *  First name,last name, title, id, salary, office, status, secretary 
       */
      private JTextField jtfFName = new JTextField();  // add first name
      private JTextField jtfLName = new JTextField();  // add last name
      private JTextField jtfTitle = new JTextField(); // add title
      private JTextField jtfID= new JTextField();  // add ID
      private JTextField jtfSalary = new JTextField(); // add salary  
      private JTextField jtfOffice = new JTextField(); // add office
      private JTextField jtfStatus = new JTextField(); // add Status
      private JTextField jtfSecretary = new JTextField(); // add Secretary
    
      // Create add, delete, edit, submit, search and cancel buttons
      private JButton jbtAdd = new JButton("Add"); // add add button
      private JButton jbtDelete = new JButton("Delete"); // add delete button
      private JButton jbtEdit = new JButton("Edit"); // add edit button
      private JButton jbtSearch = new JButton("Search"); // add search button
      private JButton jbtCancel = new JButton("Cancel");  // add cancel button
      
      public CompanyApp () {
        // Panel p1 to hold labels and text fields
        JPanel p1 = new JPanel(new GridLayout(5, 2));
        p1.add(new JLabel("First Name: "));
        p1.add(jtfFName);
        p1.add(new JLabel("Last Name: "));
        p1.add(jtfLName);
        p1.add(new JLabel("Title: "));
        p1.add(jtfTitle);
        p1.add(new JLabel("ID: "));
        p1.add(jtfID);
        p1.add(new JLabel("Salary: "));    
        p1.add(jtfSalary);
        p1.add(new JLabel("Office: "));
        p1.add(jtfOffice);
        p1.add(new JLabel("Status: "));
        p1.add(jtfStatus);
        p1.add(new JLabel("Secretary: "));
        p1.add(jtfSecretary);
        p1.setBorder(new TitledBorder("Enter Employee Information"));
    
        // Panel p2 to hold the buttons
        JPanel p2 = new JPanel(new GridLayout(1, 2));
        p2.add(jbtAdd);
        p2.add(jbtDelete);
        p2.add(jbtEdit);
        p2.add(jbtSearch);
        p2.add(jbtCancel);
    
        // Add the panel to the frame
        add(p1, BorderLayout.CENTER);
        add(p2, BorderLayout.SOUTH);
      
        jbtAdd.addActionListener( 
          new ActionListener() {
              public void actionPerformed(ActionEvent e) {  
                  String firstName = jtfFName.getText();
                  String lastName = jtfLName.getText();
                  String title = jtfTitle.getText();
                  int id = Integer.parseInt(jtfID.getText());
                  double salary = Double.parseDouble(jtfSalary.getText());
                  int office = Integer.parseInt(jtfOffice.getText());
                  String status = jtfStatus.getText();
                  String secretary = jtfSecretary.getText();
                  
                  
                 
                  
                  // Create an employee object
                  if (id >= 10 && id > 0) {
                    Employee employee = new Employee(firstName, lastName, title, id, salary, 
                    office, status, secretary);     
                    
                  }
                  // Create a secretary object
                    else if (id >= 20 && id > 11) {
                    Secretary secretary1 = new Secretary(firstName, lastName, title, id, salary, 
                    office, status, secretary);
    
                  }
                  
                  // Create a supervisor object
                    else {
                    Supervisor supervisor = new Supervisor(firstName, lastName, title, id, salary, 
                    office, status, secretary);
    
                 }
                
                 
                 
                 employeeList.add(new Employee(firstName, lastName, title, id, salary, 
                   office, status,  secretary));
                   
                  // Clear text fields after a button is clicked
                  jtfFName.setText("");
                  jtfLName.setText("");
                  jtfTitle.setText("");
                  jtfID.setText("");
                  jtfSalary.setText("");
                  jtfOffice.setText("");
                  jtfStatus.setText("");
                  jtfSecretary.setText("");
                  
                  
                  
                  
                  
                  System.out.println("A new employee has been hired!");
                  System.out.println("There are now " + employeeList.size() + " employees in the company");
    
                 }
            }
            );
         
        jbtDelete.addActionListener(
          new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  
                  // Clear text fields after a button is clicked
                  jtfFName.setText("");
                  jtfLName.setText("");
                  jtfTitle.setText("");
                  jtfID.setText("");
                  jtfSalary.setText("");
                  jtfOffice.setText("");
                  jtfStatus.setText("");
                  jtfSecretary.setText("");
                  
                  System.out.println("Somebody isn't getting a paycheck anymore, " +
                  "because you just fired them!");
                  System.out.println("There are now " + employeeList.size() + " employees in the company");
    
    
                }
            }
            );
        
        jbtEdit.addActionListener(
          new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  String firstName = jtfFName.getText();
                  String lastName = jtfLName.getText();
                  String title = jtfTitle.getText();
                  int id = Integer.parseInt(jtfID.getText());
                  double salary = Double.parseDouble(jtfSalary.getText());
                  int office = Integer.parseInt(jtfOffice.getText());
                  String status = jtfStatus.getText();
                  String secretary = jtfSecretary.getText();
                  System.out.println("An employee's information has been edited!");
                  
                  // Clear text fields after a button is clicked
                  jtfFName.setText("");
                  jtfLName.setText("");
                  jtfTitle.setText("");
                  jtfID.setText("");
                  jtfSalary.setText("");
                  jtfOffice.setText("");
                  jtfStatus.setText("");
                  jtfSecretary.setText("");
                }
            }
            );
        jbtSearch.addActionListener(
          new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  int id = Integer.parseInt(jtfID.getText());
                  System.out.println("An employee's information has been searched!");
                  
                }
            }
            );
            
        jbtCancel.addActionListener(
          new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  System.out.println("The text fields are cleared!");
                  
                  
                  // Clear text fields after a button is clicked
                  jtfFName.setText("");
                  jtfLName.setText("");
                  jtfTitle.setText("");
                  jtfID.setText("");
                  jtfSalary.setText("");
                  jtfOffice.setText("");
                  jtfStatus.setText("");
                  jtfSecretary.setText("");
                }
            }
            );
            
            employeeList.add(employee1);
            employeeList.add(employee2);
            employeeList.add(secretary1);
            employeeList.add(secretary2);
            employeeList.add(supervisor1);
            employeeList.add(supervisor2);
      }
      
      public static void main(String[] args) {      
        CompanyApp frame = new CompanyApp();
        frame.setTitle("Employee Registration");
        frame.setLocationRelativeTo(null); // Center the frame
        frame.setSize(600, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
      }
    }
    \]

  5. #5
    simonsays415 is offline Member
    Join Date
    Jul 2010
    Posts
    7
    Rep Power
    0

    Default

    yikes, sorry for posting that code like 4 gazillion times.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    One of your problems seems to be that you want to change the data, and then you want these changes to persist after you exit the program. However you can't do this at present as your data is hard-coded into your program rather than read from a file, and that shows the key to a solution: read the data in from a file, and when you want to have changes persist, write the data back into a file. This will take some studying of file IO (input and output) on your part, including possibly using serialization to help you achieve this, but it is study that is worthwhile. A better eventual solution is to use a database, but this may be overkill in this simple program.

  7. #7
    simonsays415 is offline Member
    Join Date
    Jul 2010
    Posts
    7
    Rep Power
    0

    Default

    ok, so i should create another program titled EmployeeList.java is solely an arrayList of employees? how do i reference this list to my CompanyApp program?

    any advice or direction for the search button? i would like to search for an employee by name, id, etc. and have the search return the employee's information.

    thank you!

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    edit button - I tried employeeList.remove(Employee); <-- this provides an error message such as: "variable Employee not found". But I am not trying to delete a specific employee at a specific index. I would like to delete the employee that is searched and found

    search button - i'm not sure how to approach searching an Employee object (employee, secretary, supervisor) with specific parameters. from what my textbook has examples of, and from what i have found on the internet, i am only able to search with one (1) given int, double, char, etc. but i have an object with multiple parameters.

    it may be easier if i were just displaying all of these results onto a console, but I need all of this to be done within a GUI.
    Your best bet is to first solve this in a non-GUI program and then use the classes created in your GUI. This will greatly simplify your program as it allows you to divide and conquer -- to solve the searching, editing, replacing of Employees independent of debugging a complex GUI. I recommend creating a CompanyData class or something similar that holds the ArrayList<Employee>, and remove this same ArrayList from the GUI. Give this CompanyData class an
    Employee search(int id) {} method, and in this method loop through your ArrayList, and comparing if each Employee item in the list has an id that matches the method parameter. If so, return that Employee object from the method. If you've run through the list and no Employees have a matching id, then return null (or you can throw an exception, as long as you document your method's behavior).

    You will want to create methods in your CompanyData class that correspond to GUI buttons behavior.

    Then in the GUI, have as one field of the class a CompanyData object. The search button's ActionListener will then call this object's search method, etc...

  9. #9
    simonsays415 is offline Member
    Join Date
    Jul 2010
    Posts
    7
    Rep Power
    0

    Default

    thank you so much. the logic of your answer should work.

    so, i will go ahead and create a separate class, within this class, i will declare methods that add, edit, delete, and search for an employee.

    assign each of these methods to the GUI, one by one, to the assigned buttons.

    and that is that? ok, thanks again. will post results later.

    cheers!

  10. #10
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

  11. #11
    simonsays415 is offline Member
    Join Date
    Jul 2010
    Posts
    7
    Rep Power
    0

    Default

    i'm throwing in the towel. i can only get some of my buttons to work. and even then, not perfectly. my add, view, and cancel buttons are the only ones that work, lol.

    i can only delete specified index of employee object. i can't edit employee in the manner that i want it to. and the search button is a big dud.

    so frustrated, hopefully the fall semester will be better. thank you all for your help. :)

  12. #12
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Do not throw in the towel, edison made over 1000 prototypes for the lightbulb, okay you are not making a light bulb but you know what he said later, I did not fail 1000 times, I just found out 1000 ways not to make a light bulb, that kind of knowledge is just as good if not better than making the light bulb, you know why because there is not a man alive ever became anything without making mistakes to show him the right way.

    Do not give up....Programming is very rewarding to the persistent.

  13. #13
    simonsays415 is offline Member
    Join Date
    Jul 2010
    Posts
    7
    Rep Power
    0

    Default

    well, it turns out that half of the class didn't complete the project either and there were about six students who didn't present their projects. and the only ones who were able to complete this assignment who veterans who knew other languages such as fortran, c++, etc.

    the professor didn't seem too surprised that half the class didn't finish the project so i guess we did ok? the average for the final exam was about 65, do i don't know if it was a reflection of his instruction, or if it was the amount of stuff that we tried to cover in 8 weeks.

    either way, i learned more than i probably should have after coming into this class with no background in programming.

    i was able to copy some of the more successful students' code so that i can compare them with mine to see where i went wrong in my program. i'm still very excited for the other programming courses that i'm taking this fall, so i certainly haven't given up entirely.

    thanks for the encouragement though!.!.!

    simon.

    i'm still going to

Similar Threads

  1. Can you create two GUIs?
    By SNPTT in forum New To Java
    Replies: 4
    Last Post: 05-18-2010, 11:08 AM
  2. guis and php
    By paddala in forum Advanced Java
    Replies: 1
    Last Post: 02-18-2010, 02:43 AM
  3. typed events vs untyped events.
    By Drun in forum SWT / JFace
    Replies: 0
    Last Post: 11-23-2009, 12:22 PM
  4. Help im a noob.. a super noob on java..
    By critdevil in forum New To Java
    Replies: 12
    Last Post: 03-07-2009, 03:17 AM
  5. Noob
    By nokomis in forum Introductions
    Replies: 2
    Last Post: 03-06-2009, 05:10 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •