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

I want the "Add To File" button to be full length (across the GUI)

do you mean that you want to edit the button size?
if so you can use this to change the button size
ButtonName.setPreferredSize(new Dimension(buttonWidth, buttonHeight));

No, the edit button is to edit the Employee details within the .txt file. The "Add To File" Button size I want to be preset in the java class.

No, the edit button is to edit the Employee details within the .txt file. The "Add To File" Button size I want to be preset in the java class.

So that means that you want to set the button size for "Add To File" Button right?
then, still with my earlier post

ButtonName.setPreferredSize(new Dimension(buttonWidth, buttonHeight));

Or maybe I'm still misunderstanding what you really mean,then sorry :$

For some reason I can't make the "Add To File" button have it's own line. Is there any way to make it have it's own row?
Also, for some reason the code you posted doesn't make the button any bigger.

where exactly did you put the code?
could you post the code

I posted it directly below the ActionHandlers for my AddToFile button.

addToFile = new JButton("Add To File");
		atfHandler = new AddToFileHandler();
		addToFile.addActionListener(atfHandler);
		addToFile.setPreferredSize(new Dimension(65, 60));

so I just need to make a...

Container add = getContainerPane();
add.setlayout(new GridLayout(1, 0, 1, 1));

? Because if so I have done this, and it makes my whole entire GUI conform to that layout.

How about creating a Jpanel then add the "add to file" button to it then add the Jpanel to pane that way the size of that button is set

I created the Jpanel, but when I put

longPane.add(addToFile);

the Add To File button doesn't appear.

Did you add the JPanel to your Container pane?

I did

pane.add(longPane.add(addToFile));

pane being my container, and longPane being my JPanel.

Try it like this

JPanel longPane = new JPanel(); 
    addToFile = new JButton("Add To File");
    addToFile.setPreferredSize(new Dimension(105,25));
.
.
.
    longPane.add(addToFile);
    pane.add(longPane);

Unfortunately, no difference. :( The button still says the same size. Starting to get a bit frustrated.

That is weird, no matter what compiler, IDE, OS I use I am able to change the button size


I tried the program at ubuntu 11.10 built in compiler(gedit/terminal), geany, windows 7 netbeans and eclipse... I have no idea anymore what advice to give :(

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.