I have a class called "Vindu" and a class called "Innlegg". I am adding a object from "Innlegg" into an ArrayList called al. But when i try to get the text out from al i only get text like this: "Innlegg@5de9ac4" Is this the memory address or something? How can i get the text i store from the objects inside the ArrayList?

Here is the class "Vindu":

public class Vindu extends JFrame implements ActionListener {
	
	JTextArea jta = new JTextArea();
	JTextField jtfInnlegg = new JTextField(20);
	JTextField jtfNavn = new JTextField(20);
	JTextField jtfDato = new JTextField(20);
	ArrayList<Innlegg> al = new ArrayList<Innlegg>();
	
	public Vindu(){
		
		
		Container cp = this.getContentPane();
		BorderLayout bl = new BorderLayout();
		
		cp.setLayout(bl);
		cp.add(jta,BorderLayout.CENTER);
		
		
		
		
		
		JPanel jpOuter = new JPanel();
		JLabel lblInnlegg = new JLabel("Innlegg");
		JLabel lblNavn = new JLabel("Navn");
		JLabel lblDato = new JLabel("Dato");
		
		JButton jbLagre = new JButton("Lagre innlegg");
		jbLagre.addActionListener(this);
		jpOuter.add(lblInnlegg);
		jpOuter.add(jtfInnlegg);
		jpOuter.add(lblNavn);
		jpOuter.add(jtfNavn);
		jpOuter.add(lblDato);
		jpOuter.add(jtfDato);
		jpOuter.add(jbLagre);
		
		
		GridLayout gl1 = new GridLayout(7,1);
		jpOuter.setLayout(gl1);
		
		JPanel jpInner = new JPanel();
		jpInner.add(jpOuter);
		cp.add(jpInner,BorderLayout.WEST);
		
		
		
		this.setVisible(true);
		this.setSize(400,400);
		this.setTitle("Blogg program");
		this.setLocation(150, 150);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	
	}

	
	public void actionPerformed(ActionEvent evt) {
		String command = evt.getActionCommand();
		if (command.equals("Lagre innlegg") ){
			Innlegg inn = new Innlegg();
			
			String innlegg = jtfInnlegg.getText();
			String navn = jtfNavn.getText();
			String dato = jtfDato.getText();
			
			inn.setInnlegg(innlegg);
			inn.setNavn(navn);
			inn.setdato(dato);
			
			al.add(inn);
			
			
			jta.setText("Antall Innlegg i Bloggen er: " + al.size() + "\n");	
			
			for(int i = al.size() -1; i >= 0; i--){
                jta.append(al.get(i)+ "\n");
            }
			
			
		}
		
	}

}

And here is the class Innlegg:

public class Innlegg  {
	
	
	String navn;
	String innlegg;
	String dato;
	
	public String getNavn()
	{
		return navn;
	}
	public String getInnlegg()
	{
		return innlegg;
	}
	public String getDato()
	{
		return dato;
	}
	
	public void setNavn(String navn)
	{
		this.navn = navn;
	}
	public void setInnlegg(String innlegg)
	{
		this.innlegg = innlegg;
	}
	public void setdato(String dato)
	{
		this.dato = dato;
	}

}

When you ddisplay an Object in Java, it calls its toString method. Every Object has a toString method because they inherit one from the class Object. That inherited method prints the name of the class and the Object's hash code (probably, but not necessarily its address). To display something more meaningful for your class, override toString. Eg

class Person {
   String name;
   int age;
   ...
   @Override
   public String toString() {
     return "person " + name + ", " + age + " years old;
   }
}
commented: Thank you, this solved my problem! +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.