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

    Default Printing object location instead of actual object.

    Hey guys, I'm trying to setup a constructor that will pass objects from an ArrayList, converted to an Array, and then printed out of a JComboBox, I have it all set up and working (Moderator edit: obscenity removed finally!) except for one minor problem; when the program is run, the JComboBox loads, but it loads the objects location, instead of the actual objects I require. I've done a lot of fiddling around with this constructor, and don't want to do anything to ruin it, so I'm just wondering if I'd be able to use a print method from a different class (within the file), without my program getting compile errors? My code is as below.
    Java Code:
    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(getEmployeesCB());
    
    		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 Object[] getEmployeesCB() {
    	String fname, fName, current;
    	int counter = 0;
    	  ArrayList<Employee> fullName = new ArrayList<Employee>();
    		try {
    	Scanner scan = new Scanner(new File("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++;
    				fullName.add(new Employee(fname));
    				}
    		scan.close();
    		} catch(InputMismatchException exc) {
    			System.out.println("There was a problem: " + exc);
    		} catch(IOException ex) {
    			System.out.println("There was a problem: " + ex);
    		} catch(Exception e) {
    			System.out.println("There was a problem: " + e);
    		}
    	return fullName.toArray();
    	}
    	
    
    	public class Employee {
    	   int id, hireYear;
    	   double salary;
    	   String name, title, fname;
    
    		public Employee() {
    			id = 1;
    			name = "Nathan Kreider";
    			title = "Owner";
    			hireYear = 2011;
    			salary = 100000.00;
    		}
    
    		public Employee(String fname) {
    			fname = fname;
    		}
    		
    		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, fullName;
    	    int counter = 0;
    	      ArrayList<String> nameList = new ArrayList<String>();
    		public void actionPerformed(ActionEvent e) {
    			idTF.setEditable(false);
    			nameTF.setEditable(true);
    			hireyearTF.setEditable(true);
    			salaryTF.setEditable(true);
    		try {
    	Scanner scan = new Scanner(new File("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);
    			  fullName = fname;
    				nameList.add(fullName);
    			        arrayList.add(employee);
    				}
    			/*for(int i = 0; i < arrayList.size(); i++) {
    			    if(employeesCB.getSelectedItem() == scan.match) {
    				nameTF.setText();                         s2w2
    			    }
    			}*/
    		scan.close();
    		} catch(InputMismatchException exc) {
    			System.out.println("There was a problem: " + exc);
    		} catch(IOException ex) {
    			System.out.println("There was a problem: " + ex);
    		}
    		}
    	}
    
    	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();
    	}
    }
    Last edited by DarrylBurke; 02-04-2012 at 09:21 AM. Reason: Removed obscenity

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: Printing object location instead of actual object.

    Write your own Combobox-Renderer or override the toString method of your Employee-class!

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Printing object location instead of actual object.

    Vampiricx3, we don't allow such language on these forums. Any repeat may lead to a ban.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    christopherx is offline Member
    Join Date
    Oct 2011
    Posts
    92
    Rep Power
    0

    Default Re: Printing object location instead of actual object.

    I think to understand the problem here, you need to understand the nature of Objects in Java.

    There are two (main) data types. Primitive data types, and Objects or "Pointers". Primitive data types directly represent the values they store. int, char, boolean are data types, to name a few. Objects, however, are pointers. And pointers do not directly represent the values they store. Pointer data types store a *pointer* too an instance of that particular class. so for example, your JComboBox is getting passed, for example: emp1, which is of type Employee. The JComboBox represents strings, so it's converting the pointer into a more manageable type. It converts the pointer to string, and not the instance, and all you get is a pointer to the memory address of that particular instance.

    What you need to do is figure out how to store the values in the JComboBox. this might involve a loop, and as you cycle through each Employee in the ArrayList, you concatenate their details and add them to a JComboBox. Or as eRaaaa suggested, override the toString() method! It is entirely up to you. I just thought you should understand why it isn't working x)

  5. #5
    Vampiricx3 is offline Member
    Join Date
    Feb 2012
    Posts
    31
    Rep Power
    0

    Default Re: Printing object location instead of actual object.

    Quote Originally Posted by DarrylBurke View Post
    Vampiricx3, we don't allow such language on these forums. Any repeat may lead to a ban.

    db
    Sorry, it's been a **** of a day, I'll try not to repeat this.

    Quote Originally Posted by christopherx View Post
    I think to understand the problem here, you need to understand the nature of Objects in Java.

    There are two (main) data types. Primitive data types, and Objects or "Pointers". Primitive data types directly represent the values they store. int, char, boolean are data types, to name a few. Objects, however, are pointers. And pointers do not directly represent the values they store. Pointer data types store a *pointer* too an instance of that particular class. so for example, your JComboBox is getting passed, for example: emp1, which is of type Employee. The JComboBox represents strings, so it's converting the pointer into a more manageable type. It converts the pointer to string, and not the instance, and all you get is a pointer to the memory address of that particular instance.

    What you need to do is figure out how to store the values in the JComboBox. this might involve a loop, and as you cycle through each Employee in the ArrayList, you concatenate their details and add them to a JComboBox. Or as eRaaaa suggested, override the toString() method! It is entirely up to you. I just thought you should understand why it isn't working x)
    Still kind of hazy on how I am supposed to be doing, as whatever data type I try, I get an error from my JComboBox, saying I need an Object[] for my program to compile, so then I add the .toArray() to my code, and now I get the pointers, not the actual objects.
    Last edited by Fubarable; 02-04-2012 at 02:14 PM. Reason: Come on now!

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

    Default Re: Printing object location instead of actual object.

    Quote Originally Posted by christopherx View Post
    There are two (main) data types. Primitive data types, and Objects or "Pointers".
    Much better to call them "reference types". To call them pointers is somewhat misleading especially to those familiar with pointer use in C or C++.


    What you need to do is figure out how to store the values in the JComboBox. this might involve a loop, and as you cycle through each Employee in the ArrayList, you concatenate their details and add them to a JComboBox.
    No -- to the original poster-- please don't follow this suggestion! You want to be able to get the selected object in its entirety from the JComboBox, and by following this recommendation you lose it. To the original poster, please just ignore christopherx's well-meaning but misguided recommendation here.


    Or as eRaaaa suggested, override the toString() method!
    This is better, or better still, use a custom renderer. The tutorials can show you how.
    Last edited by Fubarable; 02-04-2012 at 02:14 PM.

  7. #7
    christopherx is offline Member
    Join Date
    Oct 2011
    Posts
    92
    Rep Power
    0

    Default Re: Printing object location instead of actual object.

    Apologies. I just wanted to offer a range of solutions, but I understand the advantage of being able to use the entire object rather than my solution.

Similar Threads

  1. Printing out value of an object
    By jasonwucinski in forum New To Java
    Replies: 21
    Last Post: 02-15-2011, 09:10 AM
  2. Printing Object Information
    By dom12 in forum New To Java
    Replies: 10
    Last Post: 11-04-2010, 06:39 PM
  3. Printing values from object in Array?
    By thesinter in forum New To Java
    Replies: 3
    Last Post: 01-20-2010, 05:19 AM
  4. [SOLVED] printing address of current instance of object?
    By emceenugget in forum New To Java
    Replies: 1
    Last Post: 02-09-2009, 09:36 PM
  5. [SOLVED] Location of Object Declaration
    By JT4NK3D in forum New To Java
    Replies: 4
    Last Post: 05-30-2008, 03:45 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
  •