Hey guys, I'm currently creating an Employee style GUI program in Java, and I have a .txt file with several Employee details (int, string, string, string, int, double), right now I want to implement a way to display all objects within a JComboBox at the very bottom of my program. If anyone is able to link me to previous tutorials about this, I will gladly take it, but if you can help me personally that would be even better! My full source code is below.
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 ExitButtonHandler ebHandler;
private AddToFileHandler atfHandler;
private ClearButtonHandler cbHandler;
private UpdateSalaryHandler usHandler;
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);
usHandler = new UpdateSalaryHandler();
titleCB.addItemListener(usHandler);
hireyearTF = new JTextField(4);
salaryTF = new JTextField(12);
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);
setSize(WIDTH, HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
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 class Employee {
int id, hireYear;
double salary;
String name, title;
public Employee() {
id = 1;
name = "Nathan Kreider";
title = "Owner";
hireYear = 2011;
salary = 100000.00;
}
public Employee(int id, String name, String title, int hireYear, double salary) {
id = id;
name = name;
title = title;
hireYear = hireYear;
salary = salary;
}
}
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("");
}
}
private class UpdateSalaryHandler implements ItemListener {
public void itemStateChanged(ItemEvent e) {
if(titleCB.getSelectedItem().toString() == "") {
salaryTF.setText("");
} 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 static void main(String[] args) {
new RectangleProgram();
}
}