Results 1 to 2 of 2
  1. #1
    Vampiricx3 is offline Member
    Join Date
    Feb 2012
    Posts
    31
    Rep Power
    0

    Default GUI Layout problems.

    Hey guys, I'm fairly new to Java, and I'm trying to create an Employee program that saves employee details to a .txt file (Soon to be a proper MySQL database), but right now I've run into some problems with the GridLayout. My current GUI looks as so.
    but I want the combobox & edit button to be on the bottom line, clear and exit, and then one line dedicated to the Add To File button, but for some reason I don't know how to dedicate one line to the add to file button, and it just stays the same. I also keep getting an InputMismatchException error when I hit the "Edit" button (which isn't quite completed yet, as it doesn't allow me to edit the data yet), but to me there doesn't seem to be anything wrong with it. My code & scanner file (rectangle.txt) are as below.
    Java Code:
    // rectangle.txt
    1 Nathan Kreider Owner 1994 1200000.0
    2 Jai Mason Senior 1998 20000.0
    3 John Citizen Senior 2008 12000.0
    4 Nathan Kreider Owner 2010 15000.0
    5 John Hurpdurp Senior 2011 12000.0
    6 Nathan Kreider Senior 2011 40000.0
    7 John Todd Senior 2008 40000.0
    8 Christopher Poole Senior 1998 40000.0
    Java Code:
    //RectangleProgram.java
    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;
    
    
            private Employee employee = new Employee();
    
            private ArrayList arrayList = new ArrayList();
    
    	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();
    		JPanel longPane = new JPanel();
    
    		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);
    		addToFile.setPreferredSize(new Dimension(120, 60)); 
    		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));
    		longPane.setLayout(new GridLayout(1, 1, 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(longPane.add(addToFile));
    		pane.add(editB);
    		pane.add(clearB);
    		pane.add(exitB);
    
    		combobox.add(employeesCB);
    		combobox.add(editB);
    
    		setSize(WIDTH, HEIGHT);
    		setVisible(true);
    		longPane.setVisible(true);
    		setResizable(false);
    		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 fname;
    	    int counter = 0;
    		public void actionPerformed(ActionEvent e) {
    			idTF.setEditable(false);
    			nameTF.setEditable(true);
    			hireyearTF.setEditable(true);
    			salaryTF.setEditable(true);
    		try {
    	Scanner scan = new Scanner("rectangle.txt");
    		while(scan.hasNext()) {
      			int id = scan.nextInt();
    		        String name = scan.next();
    			String lname = scan.next();
      			String title = scan.next();
      			int hireYear = scan.nextInt();
      			double salary = scan.nextDouble();
    				fname = name + " " + lname;
    				    counter++;
    			  /*employee = new Employee(id, fname, title, hireYear, salary);
    
    			        arrayList.add(employee);*/
    				}
    			/*for(int i = 0; i < arrayList.size(); i++) {
    			    if(employeesCB.getSelectedItem() == scan.match) {
    				nameTF.setText();
    			    }
    			}*/
    		scan.close();
    		} 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();
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: GUI Layout problems.

    There are a lot more layouts available than just GridLayout. I suggest that you study the layout tutorial which you can find here, and then nesting JPanels, each one using its own layout, in order to achieve your desired result. My only caveat is that you may want to avoid GridBagLayout and GroupLayout until you've gotten more familiar with the simpler layout managers.

Similar Threads

  1. JAVA GUI Layout problems...
    By arcelivez in forum AWT / Swing
    Replies: 1
    Last Post: 11-25-2011, 01:48 AM
  2. Replies: 13
    Last Post: 12-06-2010, 05:57 AM
  3. Edit layout Layout please help me
    By manhtungtnk28@gmail.com in forum New To Java
    Replies: 4
    Last Post: 11-23-2009, 08:41 AM
  4. Keyboard Layout Problems
    By FrederikNS in forum Eclipse
    Replies: 0
    Last Post: 10-28-2008, 04:03 PM
  5. JPanel / layout problems
    By Warhorsei in forum AWT / Swing
    Replies: 4
    Last Post: 06-04-2008, 05:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •