Edmond Lee
Edmond Lee

Reputation: 17

print info from an array object into individual field

I have code that when I run the project, it brings up a user interface that said add player. When I clicked that, I have a dialog box ask me to input name and points. It works fine, but when I tried to print out the info I entered, it went wrong. Below is the code, any help is much appreciated.

package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import view.GUI;

public class addButtonActionListener implements ActionListener {

    private GUI frame;
    private JTextField name;
    private JTextField points;

    public addButtonActionListener(GUI frame) {
        frame = this.frame;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        // System.exit(0);
        name = new JTextField();
        points = new JTextField();

        Object[] details = {
            "Your Name", name,
            "Points to start", points
        };

        JOptionPane.showConfirmDialog(null, details);
        System.out.println("Add Player button pressed!");
        System.out.println(name);
        System.out.println(points);
        // frame.getDetails().add(hello);
        // System.out.println(frame.getDetails());
    }
}

Please ignore the constructor on top, because I'm calling the button from a different class.

Upvotes: 1

Views: 109

Answers (1)

Kon
Kon

Reputation: 10810

Change

System.out.println(points);

To

System.out.println(points.getText());

When you send an object, like a JTextField to println() method, it automatically tries to call the objects toString() method. For most Java objects, this method returns something you don't really want to print out, like a hash code. The getText() method, on the other hand, returns the String content of the JTextField.

Upvotes: 1

Related Questions