Hey guys, I'm trying to build an Employee system GUI, and right now i'm trying to create a JComboBox to display all of the Employees, but whenever I compile, I get the following errors..
nathan@ubuntu:~/Desktop/Employee GUI [Java]$ javac RectangleProgram.java
RectangleProgram.java:42: cannot find symbol
symbol : constructor JComboBox(java.lang.String)
location: class javax.swing.JComboBox
employeesCB = new JComboBox(getEmployees());
^
1 error
my code is as 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 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());
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 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 String 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;
employee = id + " " + fname + " " + title + " " + hireYear + " " + salary;
counter++;
}
} catch(FileNotFoundException e) {
System.out.println("There was a problem:" + e);
}
return employee;
}
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 ShowEmployeesHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFrame frame = new JFrame("TextDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add contents to the window.
frame.add(new TextDemo());
//Display the window.
frame.pack();
frame.setVisible(true);
TextDemo.textArea.setText(getEmployees());
}
}*/
public static void main(String[] args) {
new RectangleProgram();
}
}