Reputation: 732
Errors: When compiling there are no errors, when executing there are no errors and the program starts just as I wanted. BUT!
Problem: What I want to achieve is simple. the text I fill in the JTextField should be put in the JTextArea.
Example: I type the word "Cat" in the JTextField, the arraylist remembers the word "Cat" and then the word Cat appears in the JTextArea. Then I repeat the process to create a big list of names. ( ofc I don't actually type the "" marks)
Current output: I type the word "Cat" and (I think) the arraylist remembers it and gives me the output: Paneel$naam@174aa60
What I think this is: I am verry new to Java (I'm even new to programming) so I might be totaly wrong. Paneel$naam@174aa60: Paneel$naam tells me where it comes from and then i get the @ symbol (no clue what this symbol means in java) and then (I think) this is the hashcode of the word "Cat" 174 = C, aa = a and 60 = t. I read on multiple sites and I think this has something to do with class Object and something with toString.
Question What do I have to change or add to my code to make it work properly? (did research and tried but with my limited knoledge i failed to succed)
The whole code:
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
// Main method to make the frame
public class Loterij3 extends JFrame {
public static void main( String args[] ) {
JFrame frame = new Loterij3();
frame.setExtendedState( frame.MAXIMIZED_BOTH );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setTitle( "Klanten Register" );
frame.setContentPane( new Paneel() );
frame.setVisible( true );
}
}
class Paneel extends JPanel {
private boven boven;
JTextArea textvak1;
JTextField textvak2;
OnthoudNaam onthoudNaam = new OnthoudNaam();
JTextField invoervak1;
public Paneel() {
setLayout( new BorderLayout() ); // using border Layout.
setBackground( Color.LIGHT_GRAY );
boven = new boven();
textvak1 = new JTextArea();
add( new JScrollPane( textvak1 ) );
textvak1.setBackground( Color.WHITE );
textvak2 = new JTextField();
textvak2.setHorizontalAlignment(JTextField.CENTER);
textvak2.setEditable( false );
add( boven, BorderLayout.NORTH );
add( textvak1, BorderLayout.CENTER );
add( textvak2, BorderLayout.SOUTH );
}
public class boven extends JPanel {
JButton kiesWin, resetL;
JLabel label1;
public boven() {
setBackground( Color.LIGHT_GRAY );
setLayout( new GridLayout( 1, 4, 100, 5 ) ); // using GridLayout.
Border border =
BorderFactory.createEmptyBorder( 10, 10, 10, 10 );
setBorder( border );
kiesWin = new JButton("Kies een Winnaar!");
kiesWin.addActionListener( new kies() );
resetL = new JButton("Reset alles");
resetL.addActionListener( new reset() );
label1 = new JLabel("Voer Persoon in en druk op enter: ", JLabel.RIGHT);
invoervak1 = new JTextField( 20 );
invoervak1.addActionListener( new InvoerVakHandler() );
add( label1 );
add( invoervak1 );
add( kiesWin );
add( resetL );
}
}
// de naam
class naam {
private String ingevoerdNaam;
public naam( String ingevoerdNaam) {
this.ingevoerdNaam = ingevoerdNaam;
}
public String getIngevoerdNaam() {
return ingevoerdNaam;
}
}
// Arraylist
class OnthoudNaam extends JPanel {
private ArrayList<naam> lijst;
public OnthoudNaam() {
lijst = new ArrayList<naam>();
}
public void voegNaamToe(naam x ) {
lijst.add(x);
}
public String toString() {
StringBuffer buffer = new StringBuffer();
for(naam x : lijst ) {
buffer.append( x );
buffer.append( "\n" );
}
return buffer.toString();
}
}
// invoer handler
public class InvoerVakHandler implements ActionListener {
public void actionPerformed( ActionEvent e ) {
String invoer = invoervak1.getText();
naam naam = new naam( invoer );
onthoudNaam.voegNaamToe( naam );
textvak1.setText( onthoudNaam.toString() );
}
}
// kies
class kies implements ActionListener {
public void actionPerformed( ActionEvent e ) {
}
}
// reset
class reset implements ActionListener {
public void actionPerformed( ActionEvent e ) {
}
}
}
Upvotes: 1
Views: 291
Reputation: 13066
Within method OnthoudNaam
buffer.append( x );
should be changed to:
buffer.append( x.getIngevoerdNaam() );
The reason for this bizarre output is that when you appending the object of naam
(x
) in StringBuffer buffer
it is calling toString()
method of Object
. Which returns the String
in this way:
getClass().getName() + '@' + Integer.toHexString(hashCode())
That's why you are getting such type of output like Paneel$naam@174aa60
. You want to append the ingevoerdNaam
value in JTextArea
so , you should call getIngevoerdNaam
method with the Object x
of naam
.
Upvotes: 0
Reputation: 13123
Your output comes from calling onthoudnaam.toString -- the output with the "@" symbol in it is typical of a toString method on an object that has no other toString written for it. You can override toString in your Onthoudnaam class to return the text you want.
Upvotes: 0
Reputation: 11440
Please see my answer from your other question:
However your output will look like this
Paneel$naam@3dee7a6c
This happens because your class "naam" did not overwritte the toString method and so Object.toString is called which produces a String based on the class name and hashCode of a Object.
So extend your naam class to this:
class naam {
private String ingevoerdNaam;
public naam( String ingevoerdNaam) {
this.ingevoerdNaam = ingevoerdNaam;
}
public String getIngevoerdNaam() {
return ingevoerdNaam;
}
public String toString() {
return ingevoerdNaam;
}
}
Upvotes: 0
Reputation: 1500525
Not quite, it's just Object.toString()
being called on an instance of naam
:
The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())
So it's not the hash code of Cat
- it's the hash code of your object.
To fix this, just override toString
:
@Override
public String toString() {
return ingevoerdNaam;
}
(As per your earlier question, I'd still recommend avoiding all these inner classes though, and I'd still recommend that you start following Java naming conventions.)
Upvotes: 2