Results 1 to 6 of 6
  1. #1
    couline is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default how to pass value from textfield to object or file(serializable file)

    ive got the layout codes ready(i have 3 classes). after i inputed the first name, last name, address, age and salary then i clicked the button "save", it should save on the Employee.ser file and it should not overwrite every time i save the input information.

    import javax.swing.*;
    import java.awt.*;
    import java.io.*;
    import java.util.*;
    public class EmployeeApp extends JFrame
    {
    private ArrayList <Employee> list;

    public EmployeeApp()
    {
    list = new ArrayList<Employee>();
    }

    JPanel panel;
    JLabel label;
    JTextField field;
    JFrame frame;
    JButton save;


    public void initialize()
    {
    panel = new JPanel();
    frame = new JFrame("Mark");
    save = new JButton("save");
    frame.add(BorderLayout.SOUTH, save);
    getContentPane().setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setSize(350,350);
    frame.setVisible(true);
    panel.setLayout(new GridLayout(12,0));

    JLabel label = new JLabel("Enter First Name ");
    field = new JTextField(20);
    frame.add(BorderLayout.CENTER,panel);
    panel.add(label);
    panel.add(field);


    label = new JLabel("Enter Last Name ");
    field = new JTextField(20);
    panel.add(label);
    panel.add(field);

    label = new JLabel("Enter Adress ");
    field = new JTextField(20);
    panel.add(label);
    panel.add(field);

    label = new JLabel("Enter Age ");
    field = new JTextField(20);
    panel.add(label);
    panel.add(field);

    label = new JLabel("Enter Salary ");
    field = new JTextField(20);
    panel.add(label);
    panel.add(field);


    }


    public void start()
    {
    initialize();
    }

    private void load()
    {
    File empFile = new File("Employee.ser");
    if(empFile.exists())
    {
    try
    {
    FileInputStream fis = new FileInputStream(empFile);
    ObjectInputStream ois = new ObjectInputStream(fis);
    Employee emp = null;
    while((emp = (Employee) ois.readObject()) != null)
    {
    list.add(emp);
    }
    }
    catch(Exception e){}
    }
    }
    }
    ________________________________________
    import java.io.*;

    public class Employee implements Serializable
    {

    private String firstName;
    private String lastName;
    private String address;
    private int age;
    private double salary;




    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 setAddress(String ad) {
    address = ad;
    }
    public String getAddress() {
    return address;
    }

    public void setAge(int ag){
    age = ag;
    }
    public int getAge(){
    return age;
    }

    public void setSalary(double sal){
    salary = sal;
    }
    public double getSalary(){
    return salary;
    }
    }
    ______________________________________
    public class EmployeeLauncher
    {
    public static void main(String[] args) throws Exception
    {
    EmployeeApp em = new EmployeeApp();
    em.start();
    }
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default Re: how to pass value from textfield to object or file(serializable file)

    For starters you need to attach an ActionListener to your 'save' button.
    Inside that you can read the fields.
    Now that means you have a problem with the fields you have.
    You only keep a reference to the last JTextField.
    So have a JTextField attribute for all 4 fields, otherwise you won't be able to read the values (easily).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    couline is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default Re: how to pass value from textfield to object or file(serializable file)

    Quote Originally Posted by Tolls View Post
    For starters you need to attach an ActionListener to your 'save' button.
    Inside that you can read the fields.
    Now that means you have a problem with the fields you have.
    You only keep a reference to the last JTextField.
    So have a JTextField attribute for all 4 fields, otherwise you won't be able to read the values (easily).
    check my code please, and tell me what i did wrong cuz it still doesnt save on the Employee.ser =/
    _________________

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.*;
    public class EmployeeApp extends JFrame implements ActionListener
    {
    	private ArrayList <Employee> list;
    
    	public EmployeeApp()
    	{
    		list = new ArrayList<Employee>();
    	}
    	Employee obj = new Employee();
    
    	JPanel panel;
    	JLabel label;
    	JTextField field;
    	JFrame frame;
    	JButton save;
    	private JTextField nameField;
    
    	public void initialize()
    	{
    		nameField = new JTextField();
    		panel = new JPanel();
    		frame = new JFrame("Mark");
    		save = new JButton("save");
    		frame.add(BorderLayout.SOUTH, save);
    		getContentPane().setLayout(new FlowLayout());
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(350,350);
    		frame.setVisible(true);
    		panel.setLayout(new GridLayout(12,0));
    
    		JLabel label = new JLabel("Enter First Name ");
    		field = new JTextField(20);
    		frame.add(BorderLayout.CENTER,panel);
    		panel.add(label);
    		panel.add(field);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    
    		label = new JLabel("Enter Last Name ");
    		field = new JTextField(20);
    		panel.add(label);
    		panel.add(field);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    		label = new JLabel("Enter Adress ");
    		field = new JTextField(20);
    		panel.add(label);
    		panel.add(field);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    		label = new JLabel("Enter Age ");
    		field = new JTextField(20);
    		panel.add(label);
    		panel.add(field);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    		label = new JLabel("Enter Salary ");
    		field = new JTextField(20);
    		panel.add(label);
    		panel.add(field);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		save.addActionListener(this);
    		load();
    
    
    
    
    	}
    
    	public void actionPerformed(ActionEvent e)
    	{
    		load();
    		obj.setFirstName(nameField.getText());
    		obj.setLastName(nameField.getText());
    		obj.setAge(nameField.getText());
    		obj.setAddress(nameField.getText());
    		obj.setSalary(nameField.getText());
    
    	}
    
    
    	public void start()
    	{
    		initialize();
    
    	}
    
    	private void load()
    	{
    		File empFile = new File("Employee.ser");
    		if(empFile.exists())
    		{
    			try
    			{
    				FileInputStream fis = new FileInputStream(empFile);
    				ObjectInputStream ois = new ObjectInputStream(fis);
    
    				Employee emp = null;
    				while((emp = (Employee) ois.readObject()) != null)
    				{
    					list.add(emp);
    				}
    			}
    			catch(Exception e){}
    		}
    	}
    	  private void saveObject()
    	{
    		try
    		{
    			File empFile = new File("Employee.ser");
    			FileOutputStream fos = new FileOutputStream(empFile);
    			ObjectOutputStream oos = new ObjectOutputStream(fos);
    			for(Employee emp : list)
    			{
    				oos.writeObject(emp);
    			}
    			oos.close();
    		}
    		catch(Exception e){}
    	}
    
    }
    Last edited by couline; 03-09-2012 at 04:00 PM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default Re: how to pass value from textfield to object or file(serializable file)

    First, please use [code] tags [/code] when posting code.
    That's pretty unreadable as it stands.
    Next stick some System.out.println()'s in there so you can see what's going on, as in if the action listener is being called.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    couline is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default Re: how to pass value from textfield to object or file(serializable file)

    hmmm... x-x can u tell me a bit more problem and what to do with it. x-x sorry, i really dont know what to do with my code. i havent seen an example on how to transfer textfield from a serializable file. =/

  6. #6
    couline is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default Re: how to pass value from textfield to object or file(serializable file)

    Quote Originally Posted by Tolls View Post
    First, please use [code] tags [/code] when posting code.
    That's pretty unreadable as it stands.
    Next stick some System.out.println()'s in there so you can see what's going on, as in if the action listener is being called.
    it saves on the employee.ser now but it only save the salary one. hmm.. which part i did wrong? =/

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.*;
    public class EmployeeApp extends JFrame implements ActionListener
    {
    	private ArrayList <Employee> list;
    
    	public EmployeeApp()
    	{
    		list = new ArrayList<Employee>();
    	}
    	Employee obj = new Employee();
    
    
    
    
    	JPanel panel;
    	JLabel label;
    	JTextField field;
    	JFrame frame;
    	JButton save;
    
    	public void initialize()
    	{
    
    
    		panel = new JPanel();
    		frame = new JFrame("Mark");
    		save = new JButton("save");
    		frame.add(BorderLayout.SOUTH, save);
    		getContentPane().setLayout(new FlowLayout());
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.add(BorderLayout.CENTER,panel);
    		frame.setSize(350,350);
    		frame.setVisible(true);
    		panel.setLayout(new GridLayout(12,0));
    
    		JLabel label = new JLabel("Enter First Name ");
    		field = new JTextField(20);
    
    		panel.add(label);
    		panel.add(field);
    
    
    
    		label = new JLabel("Enter Last Name ");
    		field = new JTextField(20);
    		panel.add(label);
    		panel.add(field);
    
    
    		label = new JLabel("Enter Adress ");
    		field = new JTextField(20);
    		panel.add(label);
    		panel.add(field);
    
    
    		label = new JLabel("Enter Age ");
    		field = new JTextField(20);
    		panel.add(label);
    		panel.add(field);
    
    
    		label = new JLabel("Enter Salary ");
    		field = new JTextField(20);
    		panel.add(label);
    		panel.add(field);
    		save.addActionListener(this);
    
    
    
    
    
    	}
    
    		public void actionPerformed(ActionEvent e)
    		{
    
    
    			obj.setFirstName(field.getText());
    			obj.setLastName(field.getText());
    			obj.setAddress(field.getText());
    
    			int age = Integer.parseInt(field.getText());
    			obj.setAge(age);
    
    			double salary = Double.parseDouble(field.getText());
    			obj.setSalary(salary);
    
    			list.add(obj);
    			saveObject();
    		}
    
    
    
    
    	public void start()
    	{
    		initialize();
    		load();
    	}
    
    	private void load()
    	{
    		File empFile = new File("Employee.ser");
    		if(empFile.exists())
    		{
    			try
    			{
    				FileInputStream fis = new FileInputStream(empFile);
    				ObjectInputStream ois = new ObjectInputStream(fis);
    
    				Employee emp = null;
    				while((emp = (Employee) ois.readObject()) != null)
    				{
    					list.add(emp);
    				}
    			}
    			catch(Exception e){}
    		}
    	}
    	  private void saveObject()
    	{
    		try
    		{
    			File empFile = new File("Employee.ser");
    			FileOutputStream fos = new FileOutputStream(empFile);
    			ObjectOutputStream oos = new ObjectOutputStream(fos);
    			for(Employee emp : list)
    			{
    				oos.writeObject(emp);
    			}
    			oos.close();
    		}
    		catch(Exception e){}
    	}
    
    }

Similar Threads

  1. Replies: 1
    Last Post: 07-10-2011, 04:33 PM
  2. Looping through file and filling textfield
    By cgiguere in forum AWT / Swing
    Replies: 5
    Last Post: 05-29-2011, 07:30 PM
  3. Replies: 7
    Last Post: 02-11-2011, 06:38 AM
  4. Replies: 0
    Last Post: 11-26-2010, 10:10 PM
  5. Replies: 7
    Last Post: 08-17-2010, 06:00 PM

Posting Permissions

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