Results 1 to 12 of 12
  1. #1
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Add String Objects to a JFrame

    Hi There,

    Im new on this forum page, Im just asking on how can I add objects from other classes with inheritance to the JFrame.

    I have an Employee class with instance fields which inherits other instance fields from the super class (Person class).
    And I have another program class which is a GUI frame called ListEmployees. The ListEmployee class should be able to list all Employee objects to the JFrame from an ArrayList of Employee objects which is added from another class called AddEmployee.
    But its just the ListEmployee Im fustrated about. The AddEmployee seems to be fine.

    Thanks anyone.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: Add String Objects to a JFrame

    Can you post the code that shows the problem? Also post the full text of any error messages you are getting.

  3. #3
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Add String Objects to a JFrame

    Yeah no problem.

    Iv'e listed two classes below, The Employee class, and the EmployeeList class (which is the GUI to list possible Employee objects)
    Im not getting any errors as such, I just cant get my head around to to display the objects inside the JFrame?
    There so much more other classes, but I dont think its necessary to show.
    Thanks again.
    Java Code:
    import java.io.*;
    import java.util.*;
    
    //THE EMPLOYEE CLASS EXTENDS PERSON AND IMPLEMENTS SERIALIZABLE
    public class Employee extends Person implements Serializable		//INHERITANCE - Employee IS A Person
    {
    	//private static final long serialVersionUID = 1L;
    	
    	protected Date dateOfBirth;					//COMPOSITION - Employee HAS A dateOfBirth
    	private double salary;
    	protected Date startDate;						//COMPOSITION - Employee HAS A starDate
    	private int number;
    	
    	//A UNIQUE EMPLOYEE NUMBER INITIALIZED TO 1
    	private static int employeeNo=1;
    	
    	//A SCANNER OBJECT TO READ IN USER INPUT
    	Scanner keyboard = new Scanner(System.in);
    	Scanner keyNums = new Scanner(System.in);
    	
    	//DEFUALT CONSTRUCTOR
    	public Employee()
    	{
    		dateOfBirth = new Date();
    		salary = 0.0;
    		startDate = new Date();
    		number = employeeNo++;
    	}
    	
    	//INITIALIZED CONSTRUCTOR
    	public Employee(String t, String fn, String sn, String pn, int d, int m, int y, double s, int sd, int sm, int sy)
    	{
    		super(t,fn,sn,pn);
    		dateOfBirth = new Date(d,m,y);
    		salary = s;
    		startDate = new Date(sd,sm,sy);
    		number = employeeNo++;
    	}
    	
    	//GET METHODS
    	public Date getDateOfBirth()
    	{
    		return dateOfBirth;
    	}
    	
    	public double getSalary()
    	{
    		return salary;
    	}
    	
    	public Date getStartDate()
    	{
    		return startDate;
    	}
    	
    	public int getEmployeeNo()
    	{
    		return employeeNo;
    	}
    	
    	public int getNumber()
    	{
    		return number;
    	}
    	
    	//SET METHODS
    	public void setDateOfBirth(Date dob)
    	{
    		dateOfBirth = dob;
    	}
    	
    	public void setSalary(double s)
    	{
    		salary = s;
    	}
    	
    	public void setStartDate(Date sd)
    	{
    		startDate = sd;
    	}
    	
    	public void setNumber(int num)
    	{
    		number = num;
    	}
    	
    	//A TOSTRING METHOD TO RETURN ITS INSTANCE FIELDS
    	//ALSO CALLING THE SUPER TOSTRAING METHOD FROM PERSON
    	public String toString()
    	{
    		return number +": "+ super.toString() +", "+ dateOfBirth +", "+ salary +", "+ startDate;
    	}
    	
    	//AN EQUALS METHOD TO CHECK FOR EQUALITY
    	public boolean equals(Employee emp)
    	{
    		if(super.equals(emp) && dateOfBirth.equals(emp.dateOfBirth) && salary == emp.salary && startDate.equals(emp.startDate))
    			return true;
    		else
    			return false;
    	}
    }
    
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class ListEmployee extends JFrame
    {
    	JPanel p1 = new JPanel();
    	private JLabel title;
    	private JLabel addAnotherLbl;
    	private JButton back, add;
    	//add objects to it
    	private JTextField objects = new JTextField();
    	
    	public ListEmployee()
    	{
    		add(title = new JLabel("A list of Employee Record(s) below:"), BorderLayout.NORTH);
    		p1.add(back = new JButton("Back"));
    		p1.add(addAnotherLbl = new JLabel("Click ADD to add another employee record: >"));
    		p1.add(add = new JButton("ADD"));
    		
    		add(p1, BorderLayout.SOUTH);
    		
    		ListenerClass listener = new ListenerClass();
    		back.addActionListener(listener);
    		add.addActionListener(listener);
    		
    		//TRYING TO LIST THE EMPLOYEES FROM THE ARRAYLIST TO THE JFRAME
    		EmployeeFileHandler empFileHandler = new EmployeeFileHandler();
    		
    		emp.setSalary(90.00);
    		
    		for(int i=0; i<=5; i++)
    		{
    			empFileHandler.readRecordsFromFile();
    			empFileHandler.list();
    			//System.out.println(emp);
    			Employee emp = new Employee();
    			
    		}
    	}
    	
    	class ListenerClass implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			if(e.getSource() == back)
    			{
    				EmployeeServices EmployeeFrame = new EmployeeServices();
    				EmployeeFrame.setTitle("Hotel Ireland Database");
    				EmployeeFrame.setSize(400,250);
    				EmployeeFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    				EmployeeFrame.setLocationRelativeTo(null);
    				EmployeeFrame.setVisible(true);
    				setVisible(false);
    			}
    			
    			else
    			{
    				AddEmployee addFrame = new AddEmployee();
    				addFrame.setTitle("Add Employee Record");
    				addFrame.setSize(450, 300);
    				addFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    				addFrame.setLocationRelativeTo(null);	
    				addFrame.setVisible(true);
    				setVisible(false);
    			}
    		}
    	}
    	
    	public static void main(String[] args)
    	{
    		ListEmployee listFrame = new ListEmployee();
    		listFrame.setTitle("List of Employee Record(s)");
    		listFrame.setSize(450,200);
    		listFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		listFrame.setLocationRelativeTo(null);
    		listFrame.setVisible(true);
    	}
    }
    Last edited by Norm; 11-26-2011 at 04:05 PM. Reason: added code tags

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: Add String Objects to a JFrame

    to display the objects inside the JFrame?
    Are these objects components that are to be added to the JFrame so that they display in the GUI?
    Or what do you mean by "display the objects"?

    Are you asking how to get references to objects in other classes from the ListEmployee class?
    If you have a reference in the ListEmployee class to the other class, you could call one of that class's methods and have it return a reference to the objects you need access to.

  5. #5
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Add String Objects to a JFrame

    Sorry, I know how to go about adding objects such as buttons, labels etc. Im referring to Employee objects. In general how can you add objects of a class you create (in my case Employee objects) and display them on the JFrame?
    Thanks

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: Add String Objects to a JFrame

    If the Employee class does not extend a Swing component class, I don't know how you can display it on a JFrame.
    You could take data from an Employee class object and display that data in components that are being displayed on the JFrame. This would require methods in the Employee class that return the desired data and it would require components that have been added to the JFrame. For example you could get a String from the Employee class and display it by putting it into a text field that is being displayed on the GUI.
    how can you add objects of a class you create
    You create objects by using the new statement.
    You can add those objects to a container like an array or ArrayList by creating an instance of the object and either assigning it to an element in the array or adding it to the ArrayList with a method.

  7. #7
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Add String Objects to a JFrame

    Ok thanks. Well I wouldn't need to extend the swing class from the Employee class, because Im adding the Employee objects to the EmployeeList. Where the EmployeeList will need to extend the swing class.
    So you can't actually add Employee objects to the JFrame, you need to add them to a container, like JLabels, JTextFields etc, then add that JLabel or JTextField to the JFrame?
    And could you give me an example on how add an Employee object to a container?

    I tried this in the EmployeeLIst but it says its not compatible:
    //Create an object reference of Emlpoyee
    Employee emp = new Employee();
    //Tried to add that object to the JFrame;
    add(emp);

    Thanks again.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: Add String Objects to a JFrame

    how add an Employee object to a container?
    If the Employee class extended JComponent you could do this:
    aContainerRef.add(new Employee());

    If it does NOT extend a component, you can not add it to a container. Read the API doc for the add() method to see what are valid arguments to the add method.

    it says its not compatible:
    As I have said, the Employee class must extend a component if you want to add it to a container

  9. #9
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Add String Objects to a JFrame

    Ok thanks. Il try see how Il go about doing it. By the way, what does the aContainerRef.add(new Employee()); statement do?
    Thanks

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: Add String Objects to a JFrame

    aContainerRef.add(new Employee());
    It creates an instance of the Employee class, and calls the add method of the object referenced by the variable: aContainer

  11. #11
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Add String Objects to a JFrame

    Ok thanks. Your replies were very helpful.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

Similar Threads

  1. [BUG]: jframe objects messed up
    By batia in forum Advanced Java
    Replies: 4
    Last Post: 03-09-2011, 07:27 AM
  2. Counting vowles in a collection of String objects
    By sunde887 in forum New To Java
    Replies: 4
    Last Post: 01-18-2011, 07:22 AM
  3. Replies: 0
    Last Post: 03-10-2009, 09:55 AM
  4. Find java objects in a given string.
    By sarathi in forum New To Java
    Replies: 3
    Last Post: 03-06-2009, 08:42 AM
  5. Creating objects based on a String value
    By lvh in forum New To Java
    Replies: 4
    Last Post: 04-30-2008, 02: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
  •