mr Enthusiast
mr Enthusiast

Reputation: 45

Getting String from textfield to another class null value[Java]

Hello I'm sorry if this is answered before, I'm really tired and bored :/ What I'm trying to do is getText() from textField in Class1 to String in Class2:

In Class1:

JTextField textField = new JTextField();
frame.getContentPane().add(textField);
. . .
    public String getme() {
    return textField.getText();
}

And to get String in Class2:

String user = new Class1().getme();

This doesn't work.. it gets null value.

If i put raw value, like:

public String getme() {
    return "hm";
}

It does work.

Can you help, ty in advance!

Upvotes: 0

Views: 169

Answers (1)

Matthieu Saleta
Matthieu Saleta

Reputation: 1508

You're creating a new Class1 with a new JTextField inside. It's certainly the default behaviour of the getText() method on new objects.

Upvotes: 2

Related Questions