Hey guys, right now I'm trying to setup my GUI so that I can search for an Employee (which when clicked, disables all of the Text Fields, but not the ID, so I can search for the ID. But right now I have no idea on how to do this, and I also would like some help with fixing the layout of my GUI. Right now it is 2 buttons per line, and then at the bottom it only has one, so I want the "Add To File" button to be full length (across the GUI), I have tried to this myself by creating a new GridLayout for it, but then it ruins the whole design of my GUI. If anyone can help me with either of my dilemass that would be greatly appreciated.
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 = 280;
private JLabel idL, nameL, titleL, hireyearL, salaryL, errorL;
private JTextField idTF, nameTF, hireyearTF, salaryTF;
private JComboBox titleCB, employeesCB;
private JButton addToFile, exitB, clearB, editB, searchB;
private ExitButtonHandler ebHandler;
private AddToFileHandler atfHandler;
private ClearButtonHandler cbHandler;
private UpdateSalaryHandler usHandler;
private EditUserHandler euHandler;
private SearchUserHandler suHandler;
String[] rank = {"", "Trainee", "Crew", "Senior", "Manager", "Owner"};
String[] employees = {"Jai Mason", "Taylah Jayne", "Nathan Kreider"};
public RectangleProgram() {
super("Welcome");
Container c = getContentPane();
Container pane = getContentPane();
Container combobox = getContentPane();
Container longPane = 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);
errorL = new JLabel("");
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);
employeesCB = new JComboBox(employees);
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);
editB = new JButton("Edit");
euHandler = new EditUserHandler();
editB.addActionListener(euHandler);
searchB = new JButton("Search");
suHandler = new SearchUserHandler();
searchB.addActionListener(suHandler);
pane.setLayout(new GridLayout(8,2,1,1));
combobox.setLayout(new GridLayout(8,3,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(editB);
pane.add(clearB);
pane.add(searchB);
pane.add(exitB);
combobox.add(employeesCB);
combobox.add(editB);
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());
if(nameTF.getText().equals("")) {
JOptionPane.showMessageDialog(null,"Error in textFields");
} else {
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");
}
}
}
private class EditUserHandler implements ActionListener {
String name = "", title, fname, lname;
int id, hireYear, counter = 0;
double salary;
public void actionPerformed(ActionEvent e) {
idTF.setEditable(false);
nameTF.setEditable(true);
titleCB.setEditable(true);
hireyearTF.setEditable(true);
salaryTF.setEditable(true);
try {
Scanner s = new Scanner("rectangle.txt");
while(s.hasNext()) {
id = s.nextInt();
name = s.next();
lname = s.next();
title = s.next();
hireYear = s.nextInt();
salary = s.nextDouble();
fname = name + " " + lname;
counter++;
}
int leId = Integer.parseInt(idTF.getText());
if(leId == id) {
String hurId = idTF.getText();
idTF.setText(hurId);
nameTF.setText("name");
titleCB.setSelectedItem(title);
hireyearTF.setText("hireYear");
salaryTF.setText("salary");
}
} catch(InputMismatchException exc) {
System.out.println("There was a problem: " + exc);
}
}
}
public class SearchUserHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
idTF.setEditable(true);
nameTF.setEditable(false);
titleCB.setEditable(false);
hireyearTF.setEditable(false);
salaryTF.setEditable(false);
}
}
public static void main(String[] args) {
new RectangleProgram();
}
}