Reputation: 1212
I'm trying to pass a JTEXTFIELD value I created in class 2 to class 3. My terminal when compiling shows me this:
error: incompatible types
username = class2.username;
My code structure is this.
class 1
- has main
-class2 c2 = new class2
class 2 (extends JFrame)
- JTextField username = new JTextField("", 15);
-method gui here
-method actionlistener here
if e.getsource == submit
class3 c3 = new class3
c3.connection();
class 3
-method connection
-string username declared here
- username = class2.username
How can i get the value from class 2 into class 3?
Upvotes: 0
Views: 39
Reputation: 3489
You were getting the error because you are assigning JTextField
to a String
which would result in incompatible types
error.
To be able to get the value of JTextField
you have to use the getText()
method like so:
username = class2.username.getText();
getText()
returns a String
which you can then assign to any String
you like.
Here is the documentation for getText()
: http://docs.oracle.com/javase/7/docs/api/javax/swing/text/JTextComponent.html#getText()
Upvotes: 1