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();
	}
}

What do you mean by that? What I had in mind was a scanner reading it from my .txt file, and then adding to an ArrayList, then printing out within the JComboBox, but I'm not sure how it would all fall together.

What do you mean by that? What I had in mind was a scanner reading it from my .txt file, and then adding to an ArrayList, then printing out within the JComboBox, but I'm not sure how it would all fall together.

check these links for help maybe:http://java.sun.com/developer/onlineTraining/collections/magercises/ComboBox/index.html and :http://www.dreamincode.net/forums/topic/166333-how-do-i-populate-jcombobox-with-arraylist/ and this too:http://www.java2s.com/Code/Java/Swing-JFC/ArrayListComboBoxModelDemo.htm

Edited 4 Years Ago by DavidKroukamp: n/a

you want a combobox at the bottom of your frame with "all" the content related to an employee? id + name + etc etc ? and why?

btw i made your program crash :(

there are InputMismatchExceptions when reading the file, at first because i only entered 1 name not name + lname , so it put the year in lname and salary in year's "int" then i tried again with 2 names and i still get the same exception but this time from loading the salary into the salary's "double" so go figure lol , also when first writing the file you catch a filenotfound exception but still write in it, and there is another InputMismatchException there also.

Edited 4 Years Ago by Philippe.Lahaie: n/a

JComboBox allows you to pass a list of objects to its constructor, and uses those for its data values (calling their toString methods to display them). But for historical reasons the list has to be in a Vector, not an ArrayList. If you change your ArrayList to a Vector (no other code changes needed) you can simply use that to populate the whole combo box in one go.

as for your gui and combo box, what i would suggest is disabling all fields but the ID field at runtime, add a "search" button and a "edit" button, and change the "add" button to "new"

type any id in the ID field and search button will read the file and load that profile , still with all other fields disabled untill you press the edit button (that was originaly disabled till a user profile was found)

pressing new would enable the other fields and disable ID and set its Id to the next logical like you are doing. it would also change the text on the new button for "add to file" , now clicking it again in that state would add the new profile and return to runtime settings with the new profile loaded.


Edit : alternatively to the searchbox + button set the ID text field to your combo box, instead of having a separate combobox at the bottom where you already see all the info.

Edited 4 Years Ago by Philippe.Lahaie: n/a

This article has been dead for over six months. Start a new discussion instead.