Hey guys, right now I have an Employee style java program that is basically for adding Employee data to a .txt file, I have all of that down pat, I have also got my program to automatically set the highest ID + 1, so I don't have to find the highest ID then add 1 myself. but right now I am trying to create a method so that when the Title is selected from the drop down box, it automatically sets the title to whatever is selected (Trainee, Crew, Senior, Manager, Owner). But right now the Salary doesn't update as it should, so I have tried to create a Refresh() method, so that when the title is selected it gives a preset salary that I can change as needed, but I'm not sure on how I would implement this to my source code, if anyone is able to help that would be great.

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

import java.io.*;
import java.util.*;

public class RectangleProgram extends JFrame {
	private static final int WIDTH = 230;
	private static final int HEIGHT = 220;

	private JLabel idL, nameL, titleL, hireyearL, salaryL;
	private JTextField idTF, nameTF, hireyearTF, salaryTF;
	private JComboBox titleCB, employeesCB;
	private JButton addToFile, exitB, clearB;
	private JMenuItem close, view;

	private ExitButtonHandler ebHandler;
	private AddToFileHandler atfHandler;
	private ClearButtonHandler cbHandler;
	//private ShowEmployeesHandler seHandler;

	String[] rank = {"", "Trainee", "Crew", "Senior", "Manager", "Owner"};
	
	public RectangleProgram() {
	   super("Welcome");
		Container pane = getContentPane();

		idL = new JLabel("ID: ", SwingConstants.RIGHT);
		nameL = new JLabel("Name: ", SwingConstants.RIGHT);
		titleL = new JLabel("Title: ", SwingConstants.RIGHT);
		hireyearL = new JLabel("Hire Year: ", SwingConstants.RIGHT);
		salaryL = new JLabel("Salary: ", SwingConstants.RIGHT);

		idTF = new JTextField(4);
		  idTF.setEditable(false);
		  idTF.setText(getId());
		nameTF = new JTextField(25);
		titleCB = new JComboBox(rank);
		hireyearTF = new JTextField(4);
		salaryTF = new JTextField(12);
		//employeesCB = new JComboBox(getEmployees());
			//employeesCB.setModel(getEmployees());

		JMenuBar mb = new JMenuBar();
	        JMenu me = new JMenu("Menu"); 
		JMenu se = new JMenu("Settings");
        	close = new JMenuItem("Close");
		view = new JMenuItem("View Employees");
		   /*seHandler = new ShowEmployeesHandler();
		   view.addActionListener(seHandler);*/
		mb.add(me);
		mb.add(se);
		me.add(view);
		me.add(close);
		setJMenuBar(mb);

		addToFile = new JButton("Add To File");
		atfHandler = new AddToFileHandler();
		addToFile.addActionListener(atfHandler);
		exitB = new JButton("Exit");
		ebHandler = new ExitButtonHandler();
		exitB.addActionListener(ebHandler);
		clearB = new JButton("Clear");
		cbHandler = new ClearButtonHandler();
		clearB.addActionListener(cbHandler);

		pane.setLayout(new GridLayout(7,2,1,1));

		pane.add(idL);
		pane.add(idTF);
		pane.add(nameL);
		pane.add(nameTF);
		pane.add(titleL);
		pane.add(titleCB);
		pane.add(hireyearL);
		pane.add(hireyearTF);
		pane.add(salaryL);
		pane.add(salaryTF);
		pane.add(addToFile);
		pane.add(exitB);
		//pane.add(employeesCB);

		setSize(WIDTH, HEIGHT);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

	public void refresh() {
			if(titleCB.getSelectedItem().toString() == "Trainee") {
				salaryTF.setText("25000.00");
			} if(titleCB.getSelectedItem().toString() == "Crew") {
				salaryTF.setText("32000.00");
			} if(titleCB.getSelectedItem().toString() == "Senior") {
				salaryTF.setText("40000.00");
			} if(titleCB.getSelectedItem().toString() == "Manager") {
				salaryTF.setText("60000.00");
			} if(titleCB.getSelectedItem().toString() == "Owner") {
				salaryTF.setText("100000.00");
			}
	}

	public String getId() {
  	  String fname, leId = "";
  	  int counter = 0, ID = 0, employee = 0;

	ArrayList<Integer> Id = new ArrayList<Integer>();
	try {
	Scanner s = new Scanner(new File("rectangle.txt"));
		while(s.hasNext()) {
  			int id = s.nextInt();
		        String name = s.next();
			String lname = s.next();
  			String title = s.next();
  			int hireYear = s.nextInt();
  			double salary = s.nextDouble();
				fname = name + " " + lname;

		if(id > ID) {
			ID = id;
		}
		    counter++;
		}
	} catch(FileNotFoundException exc) {
            System.out.println("There was a problem:" + exc);
        }
		leId = Integer.toString(ID+1);
	return leId;
	}

	/*public Object[] getEmployees() {
        String read, fname, employee;
        int counter = 0;

        //Employee[] employees = new Employee[10];
        ArrayList<Employee> employees = new ArrayList<Employee>();
        try {
            Scanner s = new Scanner(new File("rectangle.txt"));
            while (s.hasNext()) {
                int id = s.nextInt();
                String name = s.next();
                String lname = s.next();
                int title = s.nextInt();
                int hireYear = s.nextInt();
                double salary = s.nextDouble();
	                fname = name + " " + lname;
		System.out.println("[" + id + "] Name: " + fname + " | Title: " + title + " | Hire Year: " + hireYear + " | Salary: " + salary);

                counter++;
            }
        } catch (FileNotFoundException e) {
            System.out.println("There was a problem:" + e);
        }
        return employee.toObject();
    	}*/

	public class ExitButtonHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			System.exit(0);
		}
	}

	private class AddToFileHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
		try {
		  int id, hireYear;
		  String name, title;
		  double salary;
			id = Integer.parseInt(idTF.getText());
			name = nameTF.getText();
			title = titleCB.getSelectedItem().toString();
			hireYear = Integer.parseInt(hireyearTF.getText());
			salary = Double.parseDouble(salaryTF.getText());

		   FileWriter first = new FileWriter("rectangle.txt", true);
			  first.write(id + " " + name + " " + title + " " + hireYear + " " + salary + "\n");
		   first.close();
		} catch (IOException ex) {
			System.out.println("There was a problem: " + e);
		}
			idTF.setText(getId());
			nameTF.setText("");
			titleCB.setSelectedItem("");
			hireyearTF.setText("");
			salaryTF.setText("");
		}
	}	

	private class ClearButtonHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			idTF.setText(getId());
			nameTF.setText("");
			titleCB.setSelectedItem("");
			hireyearTF.setText("");
			salaryTF.setText("");
		}
	}

	public static void main(String[] args) {
		new RectangleProgram();
	}
}

Recommended Answers

All 2 Replies

This is exactly what I need. Thank you a lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.