3 Replies - 1576 Views - Last Post: 28 March 2010 - 09:40 AM Rate Topic: -----

#1 ryan29121  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 28-March 10

Need help adding and saving using JFrame

Posted 28 March 2010 - 06:34 AM

What I am trying to do with this program is to prompt the user to enter in employee information by hitting the "add" button, then save the information with the "save" button. The file will then be saved to the computer. My program is comprised of three classes: EmployeeRecord, GUI, and label. I need help with designing a class to use the add and save buttons. The class that corresponds to this is Label class. Could you take a look at what I have and help me with these buttons? Any help that you provide would be greatly appreciated. Thank you very much in advance for any help you are willing to give.

----------EmployeeRecord-----------------
import java.io.Serializable;

public class EmployeeRecord implements Serializable{
	static final long serialVersionUID = 42L;

	private int EmpID;
	private String firstName;
	private String lastName;
	private String jobTitle;
	private int dept;
	private double pay;
	private int start;
	private int end;
	
	public EmployeeRecord(){
		this(0,"","","",0,0.0,0,0);
	}
	
	public EmployeeRecord(int ID, String first, String last, String job, int dep, double pa, int sta, int en){
		setEmpID(ID);
		setFirstName(first);
		setLastName(last);
		setJobTitle(job);
		setDept(dep);
		setPay(pa);
		setStart(sta);
		setEnd(en);
		
	}
	public void setEmpID(int ID){
		EmpID=ID;
	}
	
	public int getEmpID(){
		return EmpID;
	}
	
	public void setFirstName(String first){
		firstName=first;
	}
	
	public String getFirstName(){
		return firstName;
	}
	public void setLastName(String last){
		lastName=last;
	}
	
	public String getLastName(){
		return lastName;
	}
	
	public void setJobTitle(String job){
		jobTitle=job;
	}
	
	public String getJobTitle(){
		return jobTitle;
	}
	public void setDept(int dep){
		dept=dep;
	}
	
	public int getDept(){
		return dept;
	}
	public void setPay(double pa){
		pay=pa;
	}
	
	public double getPay(){
		return pay;
	}
	public void setStart(int sta){
		start=sta;
	}
	
	public int getStart(){
		return start;
	}
	public void setEnd(int en){
		end=en;
	}
	
	public int getEnd(){
		return end;
	}
}


---------GUI------------
import java.awt.BorderLayout;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
 import javax.swing.JFrame;
 import javax.swing.JScrollPane;
 import javax.swing.JTextArea;
 
public class GUI extends JFrame{
	private JTextArea outputArea;
	private JScrollPane scrollPane;
	private ObjectInputStream input;
	String myString;
	
	// set up GUI
	
	public GUI(){
		super("Testing class File");
		
		outputArea= new JTextArea();
		scrollPane = new JScrollPane (outputArea);
		
		add(scrollPane,BorderLayout.CENTER); //add the scrollingPane to GUI
		setSize(400, 400);
		setVisible (true); 
		
		myString ="";
		openFile();
		readRecords();
		displayText();
		closeFile();
		
		System.out.println(myString);
	}
		public void openFile(){
		
			try{
			
			input=new ObjectInputStream(new FileInputStream("employee.ser"));
			
			}
			catch (IOException ioException){
			System.err.println("Error Opening File");
			}
	}
	public void readRecords(){ 
		// local objects to be written to file
		
		
		while(input!=null){
			try{	
				EmployeeRecord record=(EmployeeRecord) input.readObject();
				
				myString=myString.concat(String.format("%d %s %s %s %d %f %d %d", 
						record.getEmpID(),
						record.getFirstName(), 
						record.getLastName(),
						record.getJobTitle(),
						record.getDept(),
						record.getPay(),
						record.getStart(),
						record.getEnd()));
			}

			catch (EOFException eofException){
				System.err.println("");
				return;
			} 
			catch (IOException ioException){
				System.err.println("Error writing to file.");
				return;
			}
			catch (ClassNotFoundException classNotFoundException){
				System.err.println("Invalid Input.  Please try again.");
				return;
			} 
			
		} // end of while
		
	}
		public void displayText(){
			outputArea.setText(myString);
	}
		public void closeFile(){
			try {
				if(input != null){
				input.close();
			}
		}
			catch (IOException ioException){
				System.err.println("Error Closing File.");
		}
	} // end of close file	
} // end of class


------Label------------
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.Icon;
import javax.swing.ImageIcon;

public class Label extends JFrame{

	private JLabel l1;
	private JLabel l2;
	private JLabel l3;
	private JButton b1;
	private JButton b2;
	
	public Label (){
		super("Employee Record");
		setLayout(new FlowLayout());
		
		l1=new JLabel("Label 1 of text");
		l1.setToolTipText("Label 1");
		add(l1);
		
		Icon image=new ImageIcon(getClass().getResource("bug1.gif"));
		l2=new JLabel("Label 2 of Image", image, SwingConstants.LEFT);
		l2.setToolTipText("Label 2 of Image");
		add(l2);
	
		l3=new JLabel();
		l3.setText("Label 3 of text and location");
		l3.setIcon(image);
		l3.setHorizontalTextPosition(SwingConstants.CENTER);
		l3.setVerticalTextPosition(SwingConstants.BOTTOM);
		l3.setToolTipText("Label 3 of position");
		add(l3);
		
		b1=new JButton("Save");
		add(b1);
		
		b2=new JButton("Add");
		add(b2);
		
		ButtonHandler handler=new ButtonHandler();
		b1.addActionListener(handler);
		b2.addActionListener(handler);
	}
	private class ButtonHandler implements ActionListener {
		public void actionPerformed(ActionEvent event){
			JOptionPane.showMessageDialog(Label.this,
					String.format("You pressed: %s", event.getActionCommand()));
			}
		}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Need help adding and saving using JFrame

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Need help adding and saving using JFrame

Posted 28 March 2010 - 08:17 AM

How do you want to add it?
I don't think you need a new class for it.
you can have a List of Employee Objects.
each time the Add button is pressed, use:
employeeList.add(new Employee(/*paramters*/);


as for the save button, simply have a method to write to a file. opening an output stream is quite similar to the input stream you already have, so it shouldn't be a problem.
Was This Post Helpful? 1
  • +
  • -

#3 ryan29121  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 28-March 10

Re: Need help adding and saving using JFrame

Posted 28 March 2010 - 08:49 AM

View Postjapanir, on 28 March 2010 - 07:17 AM, said:

How do you want to add it?
I don't think you need a new class for it.
you can have a List of Employee Objects.
each time the Add button is pressed, use:
employeeList.add(new Employee(/*paramters*/);


as for the save button, simply have a method to write to a file. opening an output stream is quite similar to the input stream you already have, so it shouldn't be a problem.


First off, thank you for the help that you provided so far. I am quite new to Java and there are quite a few things I do not understand. For the code you mentioned employeeList.add(new Employee(/*paramters*/);,could I input all the records into the parameters? In the GUI class I have all the employee records as one string (myString). Could I use
employeeList.add(new Employee(myString);
Or is that incorrect. To explain what I am trying to do further, when the user presses the "add" button they will be prompted with an input for each record (Employee ID, First Name, Last Name, Job Title, Department, Pay, start date, end date). Then after information is entered, user presses "save" button to save information to a file. Can you provide any additional information to help me? Thanks again
Was This Post Helpful? 0
  • +
  • -

#4 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Need help adding and saving using JFrame

Posted 28 March 2010 - 09:40 AM

you can set your data file as a csv file in that format:
id, firstname, lastname job...
(at the same order the parameters are ordered in the constructor)
the datafile will look something like:

123123,leroy,tsruya,...
124123,john,smith,....

now, when reading the file, use the split(String regex) method of the String class (on each line).
the split method returns an array of String Objects tokenized by the regex. (in your case the regex should be the ',').
String[] tokens = line.split("\\,");

then, you can parse each data as needed (the ID should be parsed to int, name and last name should remain String)

then create a new Employee Object as:
int ID = Integer.parseInt(tokens[0]);
String fName = tokens[1];
String lName = tokens[2];
.....

EployeeRecord temp = new EmployeeRecord(id, fName, lName...);

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1