Hey guys, I'm here to ask you a question. I have a constructor to get the highest ID (first int) from my Employee data file (int string string string int double), but I have no idea on how to actually display it within my JTextField, with a +1, so I don't have to add the ID in by myself. My Source code is below if anyone is able to actually help me with this.

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 = 550;
	private static final int HEIGHT = 470;

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

	private ExitButtonHandler ebHandler;
	private AddToFileHandler atfHandler;
	private ClearButtonHandler cbHandler;

	String[] rank = {"", "Trainee", "Crew", "Senior Crew", "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(getId());
		  idTF.setEditable(false);
		nameTF = new JTextField(25);
		titleCB = new JComboBox(rank);
		hireyearTF = new JTextField(4);
		salaryTF = new JTextField(12);

		JMenuBar mb = new JMenuBar();
	        JMenu me = new JMenu("Menu"); 
		JMenu se = new JMenu("Settings");
        	close = new JMenuItem("Close");
		view = new JMenuItem("View Employees");
		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(clearB);

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

	public int getId() {
	  String fname;
	  int counter = 0, ID = 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 e) {
	            System.out.println("There was a problem:" + e);
	        }

		return ID;
	}

	public int setId() {
		return getId();
	}
		

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

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

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

and my .txt file

1 Nathan Kreider Own 2000 200000.00

Recommended Answers

All 4 Replies

how to actually display it within my JTextField

Can you explain how you want the data to be displayed?
Show what the program currently does and explain what you want to change.

I assume you have tried the setText() method?

also, could you please explain what you are trying to do with this method:

public int setId() {
  return getId();
}

Sorry, I was in a bit of a rush when I was posting this as my laptop was dying and I didn't have my charger. What I wanted this to do was run my rectangle.txt file through a Scanner, then have the greatest Employee ID (First integer) outputted into a non-editable ID JTextField within my GUI, just as a way to make the whole system easier to run. I have the getId() method, but I don't know how to actually display it within the JTextField, as what I am doing now, it just shows up blank.

how to actually display it within the JTextField

Use the set method to put it into the text field.

it just shows up blank.

Of course if the value is an empty String, nothing will show.

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.