My Button isn't working properly

JButton doesnt work properly

Page 1 of 1

2 Replies - 1469 Views - Last Post: 18 May 2010 - 01:29 AM Rate Topic: -----

#1 kiph   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 15-May 10

My Button isn't working properly

Posted 17 May 2010 - 11:07 PM

Hi there, these are the following java files ive created so far:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;
import java.io.*;

public class AdminSystem
{
	public static void main(String[] args)
	{
		JFrame frame = new AdminSystemFrame();
		frame.setVisible(true);
	}
}

class AdminSystemFrame extends JFrame
{
	public AdminSystemFrame()
	{
		setTitle("Human Resources Graphical Administration System");
		setSize(500,500);
		centerWindow(this);
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.add(new AdminSystemPanel());
		this.pack();
	}
	
	private void centerWindow(Window w)
	{
		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension d = tk.getScreenSize();
		setLocation((d.width-w.getWidth())/2, (d.height-w.getHeight())/2);
	}
}

class AdminSystemPanel extends JPanel
{
	ArrayList<Department> departments = new ArrayList<Department>();
	ArrayList<Employee> employees = new ArrayList<Employee>();
	departmentComboBoxPanel newDepartmentComboBoxPanel;
	departmentPanel newDepartmentPanel;
	nextEmployeePanel ep2;
	employeeComboBoxPanel newEmployeeComboBoxPanel;
	employeePanel newEmployeePanel;
	bottomPanel newBottomPanel;
	employeeButtonsPanel newEmployeeButtonsPanel;
	Department newDepartment = null;
	Employee newEmployee = null;
	
	private GridBagConstraints getConstraints(int gridx, int gridy,
    int gridwidth, int gridheight, int anchor)
    {
        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(5, 5, 5, 5);
        c.ipadx = 0;
        c.ipady = 0;
        c.gridx = gridx;
        c.gridy = gridy;
        c.gridwidth = gridwidth;
        c.gridheight = gridheight;
        c.anchor = anchor;
        return c;
    }
	
	public AdminSystemPanel()
	{
		setLayout(new GridBagLayout());
		newDepartmentComboBoxPanel = new departmentComboBoxPanel();
		add(newDepartmentComboBoxPanel, getConstraints(0,0,1,1, GridBagConstraints.WEST));
        newDepartmentPanel = new departmentPanel();
        add(newDepartmentPanel, getConstraints(0,1,1,1, GridBagConstraints.WEST));
        newEmployeeComboBoxPanel = new employeeComboBoxPanel();
        add(newEmployeeComboBoxPanel, getConstraints(0,2,1,1, GridBagConstraints.WEST));
        newEmployeePanel = new employeePanel();
        add(newEmployeePanel, getConstraints(0,3,1,1, GridBagConstraints.WEST));
        ep2 = new nextEmployeePanel();
        add(ep2, getConstraints(0,4,1,1, GridBagConstraints.WEST));
        newEmployeeButtonsPanel = new employeeButtonsPanel();
        add(newEmployeeButtonsPanel, getConstraints(0,5,1,1, GridBagConstraints.WEST));
        newBottomPanel = new bottomPanel();
        add(newBottomPanel, getConstraints(0,6,1,1, GridBagConstraints.WEST));
        
 //       newEmployeePanel.showEmployee(employees.get(0));
 //       newEmployeePanel.selectEmployee(employees.get(0));
 		newDepartmentPanel.setEditableFields(false);
	}
	
	class departmentPanel extends JPanel
	{
		private JTextField departmentNameTextField, departmentLocationTextField, departmentHeadTextField;
		private JLabel departmentNameLabel, departmentLocationLabel, departmentHeadLabel;
		private JButton addNewDepartmentButton, setDepartmentHeadButton, acceptNewDepartmentButton, deleteDepartmentButton, cancelButton;
		
		
		
		
		public departmentPanel()
		{
			setLayout(new FlowLayout(FlowLayout.LEFT));
			
			departmentNameLabel = new JLabel("Department Name: ");
			add(departmentNameLabel);
		
			departmentNameTextField = new JTextField(10);
			add(departmentNameTextField);
		
			departmentLocationLabel = new JLabel("Department Location: ");
			add(departmentLocationLabel);
		
			departmentLocationTextField = new JTextField(10);
			add(departmentLocationTextField);
			
			departmentHeadLabel = new JLabel("Department Head: ");
			add(departmentHeadLabel);
			
			departmentHeadTextField = new JTextField(10);
			departmentHeadTextField.setEditable(false);
			add(departmentHeadTextField);
		
			JPanel buttonPanel = new JPanel();
			buttonPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		
			addNewDepartmentButton = new JButton("Add New Department");
			addNewDepartmentButton.addActionListener(new addNewDepartmentListener());
			add(addNewDepartmentButton);
			
			setDepartmentHeadButton = new JButton("Set Department Head");
			setDepartmentHeadButton.addActionListener(new setDepartmentHeadListener());
			add(setDepartmentHeadButton);
		
			deleteDepartmentButton = new JButton("Delete Department");
			deleteDepartmentButton.addActionListener(new deleteDepartmentListener());
			add(deleteDepartmentButton);
			
			acceptNewDepartmentButton = new JButton("Accept");
			acceptNewDepartmentButton.addActionListener(new acceptNewDepartmentListener());
			acceptNewDepartmentButton.setEnabled(false);
			add(acceptNewDepartmentButton);
			
			cancelButton = new JButton("Cancel");
			cancelButton.addActionListener(new departmentCancelListener());
			cancelButton.setEnabled(false);
			add(cancelButton);
		}
        
        public Department getDepartment()
        {
            Department d = new Department();
            d.setDepartmentName(departmentNameTextField.getText());
            d.setDepartmentLocation(departmentLocationTextField.getText());
            return d;
        }
        
        public void showDepartment(Department d)
        {
            departmentNameTextField.setText(d.getDepartmentName());
            departmentLocationTextField.setText(d.getDepartmentLocation());
            departmentHeadTextField.setText(d.getDepartmentHead());
        }
        
        public void setAddEditMode(boolean e)
        {
            departmentNameTextField.setEditable(e);
            departmentNameTextField.setFocusable(e);
            departmentNameTextField.requestFocusInWindow();
            departmentLocationTextField.setEditable(e);
            departmentLocationTextField.setFocusable(e);
            addNewDepartmentButton.setEnabled(!e);
            setDepartmentHeadButton.setEnabled(!e);
            deleteDepartmentButton.setEnabled(!e);
            acceptNewDepartmentButton.setEnabled(e);
            cancelButton.setEnabled(e);
        }
        
        public void setHeadMode(boolean e)
        {
        	addNewDepartmentButton.setEnabled(!e);
        	departmentHeadTextField.setEditable(!e);
        	acceptNewDepartmentButton.setEnabled(e);
        	setDepartmentHeadButton.setEnabled(!e);
        	deleteDepartmentButton.setEnabled(!e);
            cancelButton.setEnabled(e);
        }
        
        public void disableHeadMode(boolean e)
        {
        	departmentHeadTextField.setEditable(!e);
        }
        
        public void clearFields()
        {
            departmentNameTextField.setText("");
            departmentLocationTextField.setText("");
            departmentHeadTextField.setText("");
        }
        
        public void setEditableFields(boolean e)
        {
        	departmentNameTextField.setEditable(e);
			departmentNameTextField.setFocusable(e);
			departmentLocationTextField.setEditable(e);
            departmentLocationTextField.setFocusable(e);
        }
	}
	
	class departmentComboBoxPanel extends JPanel implements ActionListener
	{
		private JLabel departmentComboBoxLabel;
		private JComboBox departmentComboBox;
		
		boolean filling = false;
		
		public departmentComboBoxPanel()
		{
			setLayout(new FlowLayout(FlowLayout.LEFT));
			
			departmentComboBoxLabel = new JLabel("Select Department: ");
			add(departmentComboBoxLabel);
		
			departmentComboBox = new JComboBox();
			fillComboBox(departments);
			departmentComboBox.addActionListener(this);
			add(departmentComboBox);
		}
		
		public void actionPerformed(ActionEvent e)
		{
			if (!filling)
            {
                Department d = (Department)departmentComboBox.getSelectedItem();
                newDepartmentPanel.showDepartment(d);
            }
		}
		
		public Department getCurrentDepartment()
        {
            return (Department) departmentComboBox.getSelectedItem();
        }
        
        public void fillComboBox(ArrayList<Department> departments)
        {
            filling = true;
            departmentComboBox.removeAllItems();
            for (Department d : departments)
                departmentComboBox.addItem(d);
            filling = false;
        }
        
        public void selectDepartment(Department d)
        {
            departmentComboBox.setSelectedItem(d);
        }
	}
	
	class employeePanel extends JPanel
	{
		
		public JTextField employeeFirstNameTextField, employeeSurnameTextField, employeeAddressTextField;
		public JLabel employeeFirstNameLabel, employeeSurnameLabel, employeeAddressLabel, employeeGenderLabel, employeePayLevelLabel;
		public JPanel genderPanel;
		public JComboBox sexbox, paybox;
		
		public employeePanel()
		{
			setLayout(new FlowLayout(FlowLayout.LEFT));
			
			employeeFirstNameLabel = new JLabel("Employee First Name: ");
			add(employeeFirstNameLabel);
		
			employeeFirstNameTextField = new JTextField(10);
			employeeFirstNameTextField.setEditable(false);
			add(employeeFirstNameTextField);
		
			employeeSurnameLabel = new JLabel("Employee Surname: ");
			add(employeeSurnameLabel);
		
			employeeSurnameTextField = new JTextField(10);
			employeeSurnameTextField.setEditable(false);
			add(employeeSurnameTextField);
		
			employeeAddressLabel = new JLabel("Employee Address: ");
			add(employeeAddressLabel);
		
			employeeAddressTextField = new JTextField(30);
			employeeAddressTextField.setEditable(false);
			add(employeeAddressTextField);
			
			
		}
		
		public Employee getEmployee()
        {
            Employee emp = new Employee();
            emp.setFirstName(employeeFirstNameTextField.getText());
            emp.setSurname(employeeSurnameTextField.getText());
            emp.setAddress(employeeAddressTextField.getText());
            emp.setGender(ep2.employeeGenderTextField.getText());
            emp.setPayLevel(ep2.employeePayLevelTextField.getText());
            return emp;
        }
		
		public void clearFields()
        {
            employeeFirstNameTextField.setText("");
            employeeSurnameTextField.setText("");
            employeeAddressTextField.setText("");
        }
        
        public void showEmployee(Employee e)
        {
            employeeFirstNameTextField.setText(e.getEmployeeFirstName());
            employeeSurnameTextField.setText(e.getEmployeeSurname());
            employeeAddressTextField.setText(e.getEmployeeAddress());
        }
            	
		public void setAddEditMode(boolean e)
    	{
        	employeeFirstNameTextField.setEditable(e);
        	employeeFirstNameTextField.setFocusable(e);
        	employeeFirstNameTextField.requestFocusInWindow();
        	employeeSurnameTextField.setEditable(e);
        	employeeSurnameTextField.setFocusable(e);
        	employeeAddressTextField.setEditable(e);
        	employeeAddressTextField.setFocusable(e);
    	}
        
        public void setEditableFields(boolean e)
        {
        	employeeFirstNameTextField.setEditable(e);
        	employeeFirstNameTextField.setFocusable(e);
        	employeeSurnameTextField.setEditable(e);
        	employeeSurnameTextField.setFocusable(e);
        	employeeAddressTextField.setEditable(e);
        	employeeAddressTextField.setFocusable(e);
        }
	}
	
	class nextEmployeePanel extends JPanel
	{
		public JTextField employeeGenderTextField, employeePayLevelTextField;
		public JLabel employeeGenderLabel, employeePayLevelLabel;
		
		public nextEmployeePanel()
		{
			setLayout(new FlowLayout(FlowLayout.LEFT));
			
			employeeGenderLabel = new JLabel("Employee Gender: ");
			add(employeeGenderLabel);
			
			employeeGenderTextField = new JTextField(1);
			employeeGenderTextField.setEditable(false);
			add(employeeGenderTextField);
			
			employeePayLevelLabel = new JLabel("Payment Level: ");
			add(employeePayLevelLabel);
			
			employeePayLevelTextField = new JTextField(1);
			employeePayLevelTextField.setEditable(false);
			add(employeePayLevelTextField);
		}
		
		public void setAddEditMode(boolean e)
    	{
        	employeePayLevelTextField.setEditable(e);
        	employeePayLevelTextField.setFocusable(e);
        	employeeGenderTextField.setEditable(e);
        	employeeGenderTextField.setFocusable(e);
    	}
    	
    	public void clearFields()
        {
            employeeGenderTextField.setText("");
            employeePayLevelTextField.setText("");
        }
    	
    	public void showEmployeeSexPay(Employee e)
        {
            employeePayLevelTextField.setText(e.getEmployeePayLevel());
            employeeGenderTextField.setText(e.getEmployeeGender());
        }
	}
	
	class employeeComboBoxPanel extends JPanel implements ActionListener
	{
		boolean filling = false;
		public JLabel employeeComboBoxLabel;
		public JComboBox employeeComboBox;
		
		public employeeComboBoxPanel()
		{
			employeeComboBoxLabel = new JLabel("Select Employee: ");
			add(employeeComboBoxLabel);
			
			employeeComboBox = new JComboBox();
			add(employeeComboBox);
		}
		
		public Employee getCurrentEmployee()
        {
            return (Employee) employeeComboBox.getSelectedItem();
        }
		
		public void actionPerformed(ActionEvent e)
        {
            if (!filling)
            {
                Employee currentEmployee = (Employee)employeeComboBox.getSelectedItem();
                newEmployeePanel.showEmployee(currentEmployee);
            }
        }
        
        public void selectEmployee(Employee e)
        {
            employeeComboBox.setSelectedItem(e);
        }
        
        public void fillComboBox(ArrayList<Employee> employees)
        {
            filling = true;
            employeeComboBox.removeAllItems();
            for (Employee e : employees)
                employeeComboBox.addItem(e);
            filling = false;
        }
	}
	
	class employeeButtonsPanel extends JPanel
	{
		public JButton addNewEmployeeButton, changeEmployeeDetailsButton, listEmployeesButton, deleteEmployeeButton, acceptEmployeeButton, setDepartmentButton, cancelButton;
		
		public employeeButtonsPanel()
		{
			setLayout(new FlowLayout(FlowLayout.LEFT));
			
			addNewEmployeeButton = new JButton("Add New Employee");
			addNewEmployeeButton.addActionListener(new addEmployeeListener());
			add(addNewEmployeeButton);
		
			changeEmployeeDetailsButton = new JButton("Modify Employee Details");
			changeEmployeeDetailsButton.addActionListener(new employeeDetailsListener());
			add(changeEmployeeDetailsButton);
			
			setDepartmentButton = new JButton("Assign Department");
			setDepartmentButton.addActionListener(new setDepartmentListener());
			add(setDepartmentButton);
		
			deleteEmployeeButton = new JButton("Delete Employee");
			deleteEmployeeButton.addActionListener(new deleteEmployeeListener());
			add(deleteEmployeeButton);
		
			listEmployeesButton = new JButton("List Employees");
			listEmployeesButton.addActionListener(new listEmployeesListener());
			add(listEmployeesButton);
			
			acceptEmployeeButton = new JButton("Accept");
			listEmployeesButton.addActionListener(new acceptNewEmployeeListener());
			acceptEmployeeButton.setEnabled(false);
			add(acceptEmployeeButton);
			
			cancelButton = new JButton("Cancel");
			cancelButton.addActionListener(new employeeCancelListener());
			cancelButton.setEnabled(false);
			add(cancelButton);
		}
		
		public void setAddEditMode(boolean e)
    	{
    		addNewEmployeeButton.setEnabled(!e);
    		changeEmployeeDetailsButton.setEnabled(!e);
    		setDepartmentButton.setEnabled(!e);
    		deleteEmployeeButton.setEnabled(!e);
    		listEmployeesButton.setEnabled(!e);
    		acceptEmployeeButton.setEnabled(e);
    		cancelButton.setEnabled(e);
    	}
	}
	
	class bottomPanel extends JPanel
	{
		public JButton createReportButton, quitButton;
		
		public bottomPanel()
		{
			JPanel quitPanel = new JPanel();
			quitPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		
			quitButton = new JButton("Quit Program");
			quitButton.addActionListener(new quitListener());
			quitPanel.add(quitButton);
			
			JPanel otherButtonsPanel = new JPanel();
			quitPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
			
			createReportButton = new JButton("Create Report");
			createReportButton.addActionListener(new createReportListener());
			otherButtonsPanel.add(createReportButton);
			
			setLayout(new BorderLayout());
			add(otherButtonsPanel, BorderLayout.CENTER);
			add(quitPanel, BorderLayout.SOUTH);
		}
	}

	class addNewDepartmentListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			newDepartmentPanel.clearFields();
			newDepartment = new Department();
			newDepartmentPanel.setAddEditMode(true);
		}
		
		
	}
	
	class acceptNewDepartmentListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			if (isValidData())
            {
                if (newDepartment != null)
                {
                    newDepartment = newDepartmentPanel.getDepartment();
                    departments.add(newDepartment);
                    newDepartmentComboBoxPanel.fillComboBox(departments);
                    newDepartmentComboBoxPanel.selectDepartment(newDepartment);
                    newDepartment = null;
                }
                else
                {
                    Department d = newDepartmentComboBoxPanel.getCurrentDepartment();
                    Department newDepartment = newDepartmentPanel.getDepartment();
                    d.setDepartmentName(newDepartment.getDepartmentName());
                    d.setDepartmentLocation(newDepartment.getDepartmentLocation());
                    newDepartmentComboBoxPanel.fillComboBox(departments);
                    newDepartmentComboBoxPanel.selectDepartment(d);
                 	newDepartmentPanel.showDepartment(newDepartmentComboBoxPanel.getCurrentDepartment());
                }
                newEmployeePanel.setAddEditMode(false);
                newDepartmentPanel.setAddEditMode(false);
            }
		}
		
		public boolean isValidData()
        {
            return SwingValidator.isPresent(newDepartmentPanel.departmentNameTextField, "Department Name")
                    && SwingValidator.isPresent(newDepartmentPanel.departmentLocationTextField, "Department Location");
        }
	}
	
	class setDepartmentHeadListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			Employee emp = newEmployeeComboBoxPanel.getCurrentEmployee();
			Department d = newDepartmentComboBoxPanel.getCurrentDepartment();
			d.setDepartmentHead(emp.getName());
			newDepartmentPanel.showDepartment(newDepartmentComboBoxPanel.getCurrentDepartment());
		}
	}
	
	class listEmployeesListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			
		}
	}
	
	class quitListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			System.exit(0);
		}
	}
	
	class acceptNewEmployeeListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			if (isValidData())
            {
                if (newEmployee != null)
                {
                    newEmployee = newEmployeePanel.getEmployee();
                    employees.add(newEmployee);
                    newEmployeeComboBoxPanel.fillComboBox(employees);
                    newEmployeeComboBoxPanel.selectEmployee(newEmployee);
                    newEmployee = null;
                }
                else
                {
                      Employee emp = newEmployeeComboBoxPanel.getCurrentEmployee();
                    Employee newEmployee = newEmployeePanel.getEmployee();
                    emp.setFirstName(newEmployee.getEmployeeFirstName());
                    emp.setSurname(newEmployee.getEmployeeFirstName());
                    emp.setAddress(newEmployee.getEmployeeAddress());
                    newEmployeeComboBoxPanel.fillComboBox(employees);
                    newEmployeeComboBoxPanel.selectEmployee(emp);
                 	newEmployeePanel.showEmployee(newEmployeeComboBoxPanel.getCurrentEmployee());
                }
                newEmployeePanel.setAddEditMode(false);
                ep2.setAddEditMode(false);
                newDepartmentPanel.setAddEditMode(false);
            }
		}
		
		public boolean isValidData()
        {
            return SwingValidator.isPresent(newEmployeePanel.employeeFirstNameTextField, "First Name")
                    && SwingValidator.isPresent(newEmployeePanel.employeeSurnameTextField, "Surname")
                    && SwingValidator.isPresent(newEmployeePanel.employeeAddressTextField, "Address")
                    && SwingValidator.isValidSex(ep2.employeeGenderTextField, "Gender")
                    && SwingValidator.isValidPayLevel(ep2.employeePayLevelTextField, "Pay Level");
        }
	}
	
	class addEmployeeListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			newEmployee = new Employee();
			newEmployeePanel.clearFields();
			ep2.clearFields();
			newEmployeePanel.setAddEditMode(true);
			ep2.setAddEditMode(true);
			newEmployeeButtonsPanel.setAddEditMode(true);
		}
	}
	
	class setDepartmentListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			
		}
	}
	
	class deleteDepartmentListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            Department d = newDepartmentComboBoxPanel.getCurrentDepartment();
            departments.remove(d);
            newDepartmentComboBoxPanel.fillComboBox(departments);
            newDepartmentComboBoxPanel.selectDepartment(departments.get(0));
            newDepartmentPanel.showDepartment(departments.get(0));
            newDepartmentComboBoxPanel.departmentComboBox.requestFocusInWindow();
        }
    }
    
    class deleteEmployeeListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            Employee emp = newEmployeeComboBoxPanel.getCurrentEmployee();
            employees.remove(emp);
            newEmployeeComboBoxPanel.fillComboBox(employees);
            newEmployeeComboBoxPanel.selectEmployee(employees.get(0));
            newEmployeePanel.showEmployee(employees.get(0));
            newEmployeeComboBoxPanel.employeeComboBox.requestFocusInWindow();
        }
    }
	
	class EditListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            newEmployeePanel.setAddEditMode(true);
        }
    }
    
    class employeeDetailsListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            
        }
    }
    
    class createReportListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            
        }
    }
    
    class departmentCancelListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            if (newDepartment != null)
            {
                newDepartment = null;
            }
            newDepartmentPanel.setAddEditMode(false);
            newDepartmentPanel.disableHeadMode(true);
            newDepartmentPanel.showDepartment(newDepartmentComboBoxPanel.getCurrentDepartment());
        }
    }
    
    class employeeCancelListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            if (newEmployee != null)
            {
                newEmployee = null;
            }
            newEmployeePanel.setAddEditMode(false);
            newDepartmentPanel.setAddEditMode(false);
            newEmployeePanel.showEmployee(newEmployeeComboBoxPanel.getCurrentEmployee());
            newEmployeeComboBoxPanel.employeeComboBox.requestFocusInWindow();
        }
    }
}


import java.util.*;
import javax.swing.*;
import javax.swing.text.*;
import java.text.*;
import java.awt.*;
import java.awt.event.*;

public class Employee
{
	private String firstName, surname, address;
	private String gender;
	private String payLevel;
	private int intlevel;
	private double payAmount;
	
	final double payLevel1 = 44245.75;
	final double payLevel2 = 48670.32;
	final double payLevel3 = 53537.35;
	final double payLevel4 = 58891.09;
	final double payLevel5 = 64780.20;
	final double payLevel6 = 71258.22;
	final double payLevel7 = 80946.95;
	final double payLevel8 = 96336.34;
	
	public Employee()
	{
	}
	
	public Employee(String fname, String sname, String adress, String sex, String level)
	{
		firstName = fname;
		surname = sname;
		this.address = adress;
		gender = sex;
		payLevel = level;
		if (this.payLevel.equals("1"))
		{
			this.payAmount = payLevel1;
		}
		if (this.payLevel.equals("2"))
		{
			this.payAmount = payLevel2;
		}
		if (this.payLevel.equals("3"))
		{
			this.payAmount = payLevel3;
		}
		if (this.payLevel.equals("4"))
		{
			this.payAmount = payLevel4;
		}
		if (this.payLevel.equals("5"))
		{
			this.payAmount = payLevel5;
		}
		if (this.payLevel.equals("6"))
		{
			this.payAmount = payLevel6;
		}
		if (this.payLevel.equals("7"))
		{
			this.payAmount = payLevel7;
		}
		if (this.payLevel.equals("8"))
		{
			this.payAmount = payLevel8;
		}
	}
	
	public void setFirstName(String fname)
	{
		this.firstName = fname;
	}
	
	public void setSurname(String sname)
	{
		surname = sname;
	}
	
	public void setAddress(String adress)
	{
		address = adress;
	}
	
	public void setGender(String a)
	{
		gender = a;
	}
	
	public void setPayLevel(String level)
	{
		this.payLevel = level;
		if (this.payLevel.equals("1"))
		{
			this.payAmount = payLevel1;
		}
		if (this.payLevel.equals("2"))
		{
			this.payAmount = payLevel2;
		}
		if (this.payLevel.equals("3"))
		{
			this.payAmount = payLevel3;
		}
		if (this.payLevel.equals("4"))
		{
			this.payAmount = payLevel4;
		}
		if (this.payLevel.equals("5"))
		{
			this.payAmount = payLevel5;
		}
		if (this.payLevel.equals("6"))
		{
			this.payAmount = payLevel6;
		}
		if (this.payLevel.equals("7"))
		{
			this.payAmount = payLevel7;
		}
		if (this.payLevel.equals("8"))
		{
			this.payAmount = payLevel8;
		}
	}
	
	public String getFormattedSalary()
	{
		NumberFormat currency = NumberFormat.getCurrencyInstance();
		return currency.format(payAmount);	
	}
	
	public String getEmployeeFirstName()
	{
		return firstName;
	}
	
	public String getEmployeeSurname()
	{
		return surname;
	}
	
	public String getName()
	{
		return firstName + " " + surname;
	}
	
	public String getEmployeeAddress()
	{
		return address;
	}
	
	public String getEmployeePayLevel()
	{
		return payLevel;
	}
	
	public String getEmployeeGender()
	{
		return gender;
	}
	
	public String toString()
	{
		return getName();
	}
}


import java.util.*;
import javax.swing.*;
import javax.swing.text.*;
import java.text.*;
import java.awt.*;
import java.awt.event.*;

public class Department
{
	private String departmentName, departmentLocation, departmentHead = null;
	private HashMap<Integer, Employee> employees1;
	private static int uniqueKey = 10000;
	
	
	public Department()
	{
		
	}
	
	public Department (String name, String location)
	{
		departmentName = name;
		departmentLocation = location;
		employees1 = new HashMap<Integer, Employee>();
	}
	
	//This method lets me add a completely new Employee.
//	public void addEmployee(String fname, String sname, String adress, char sex, int level)
//	{
//		this.employees.add(new Employee(fname, sname, adress,sex, level));
//	}

	public void addEmployee(Employee details)
	{
		this.employees1.put(uniqueKey, details);
		uniqueKey++;
	}
	
	public void setDepartmentName(String name)
	{
		this.departmentName = name;
	}
	
	public void setDepartmentLocation(String location)
	{
		this.departmentLocation = location;
	}
	
	public void setDepartmentHead(String head)
	{
		this.departmentHead = head;
	}
	
	public String getDepartmentName()
	{
		return departmentName;
	}
	
	public String getDepartmentLocation()
	{
		return departmentLocation;
	}
	
	public int getUniqueKey()
	{
		return uniqueKey;
	}
	
	public String getDepartmentHead()
	{
		return departmentHead;
	}
		
	public void incUniqueKey(int amt)
	{
		uniqueKey += amt;
	}
	
	public String toString()
	{
		return departmentName;
	}
}


import java.util.*;
import javax.swing.*;
import javax.swing.text.*;
import java.text.*;
import java.awt.*;
import java.awt.event.*;

public class SwingValidator
{
	public static boolean isPresent(JTextComponent c, String title)
	{
		if (c.getText().length() == 0)
		{
			showMessage(c, title + " is a required field.\nPlease re-enter.");
			c.requestFocusInWindow();
			return false;
		}
		return true;
	}
	
	public static boolean isInteger(JTextComponent c, String title)
	{
		try
		{
			int i = Integer.parseInt(c.getText());
			return true;
		}
		catch (NumberFormatException e)
		{
			showMessage(c, title + " must be an integer.\n Please re-enter.");
			c.requestFocusInWindow();
			return false;
		}
	}
	
	public static boolean isDouble(JTextComponent c, String title)
	{
		try
		{
			double d = Double.parseDouble(c.getText());
			return true;
		}
		catch (NumberFormatException e)
		{
			showMessage(c, title + " must be a double value.\n Please re-enter.");
			c.requestFocusInWindow();
			return false;
		}
	}
	
	public static boolean isValidSex(JTextComponent c, String title)
	{
		if (c.getText().equalsIgnoreCase("m") || c.getText().equalsIgnoreCase("f"))
			return true;
		else
		{
			showMessage(c, title + " must be either 'M' or 'F'.\nPlease re-enter.");
			c.requestFocusInWindow();
			return false;
		}
	}
	
	public static boolean isValidPayLevel(JTextComponent c, String title)
	{
		if (c.getText().equalsIgnoreCase("1") || c.getText().equalsIgnoreCase("2") || c.getText().equalsIgnoreCase("3") || c.getText().equalsIgnoreCase("4") || c.getText().equalsIgnoreCase("5") || c.getText().equalsIgnoreCase("6") || c.getText().equalsIgnoreCase("7") || c.getText().equalsIgnoreCase("8"))
			return true;
		else
		{
			showMessage(c, title + " must be between 1 and 8.\nPlease re-enter.");
			c.requestFocusInWindow();
			return false;
		}
	}
	
	private static void showMessage(JTextComponent c, String message)
	{
		JOptionPane.showMessageDialog(c, message, "Invalid Entry", JOptionPane.ERROR_MESSAGE);
	}
}


The program compiles fine, but for some reason the accept button for the employee panel isn't working properly. When I click "Add New Employee", it works fine, but when I try to click "Accept", nothing happens. What's supposed to happen when the "Accept" button is clicked is the accept and cancel buttons are supposed to go back to being greyed out, and the new employee details are supposed to be added to the employee combo box, but for some reason this isn't happening. The department panel's accept button is working perfectly fine, in that the new department is successfully added to the department combo box, but for some reason the employee accept button doesnt work even though it shares the same sort of functionality as the department panel's accept button. Can someone please look at my code and tell me what I'm doing wrong? Thanks in advance.

This post has been edited by kiph: 17 May 2010 - 11:23 PM


Is This A Good Question/Topic? 0
  • +

Replies To: My Button isn't working properly

#2 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: My Button isn't working properly

Posted 18 May 2010 - 01:13 AM

Here:

...
acceptEmployeeButton = new JButton("Accept");
//listEmployeesButton.addActionListener(new acceptNewEmployeeListener());
acceptEmployeeButton.addActionListener(new acceptNewEmployeeListener());
acceptEmployeeButton.setEnabled(false);
add(acceptEmployeeButton);
...


Was This Post Helpful? 1
  • +
  • -

#3 kiph   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 15-May 10

Re: My Button isn't working properly

Posted 18 May 2010 - 01:29 AM

View Postn8wxs, on 18 May 2010 - 12:13 AM, said:

Here:

...
acceptEmployeeButton = new JButton("Accept");
//listEmployeesButton.addActionListener(new acceptNewEmployeeListener());
acceptEmployeeButton.addActionListener(new acceptNewEmployeeListener());
acceptEmployeeButton.setEnabled(false);
add(acceptEmployeeButton);
...



*smacks self on head* Oh my god, I can't believe I made such a simple mistake, I'm such an idiot. I guess that'll teach me for just copy-pasting large chunks of code all over the place. Oh well, thanks for the help.

This post has been edited by kiph: 18 May 2010 - 01:36 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1