I want to retrieve the text from a JTextField
instance in another class after the user presses Enter. I've tried using helper methods to pass along a boolean to only set the text when it is true/when the user presses Enter. I've tried using a public JTextField
field, but that only leads to an error or initializing the text field again, which returns nothing. I'm very new to using these, so I am definitely overlooking one or two aspects.
// imports...
public class test1 extends canvas() {
public JTextField box;
public boolean hasTextEntered = false;
public void jFields() {
Action action = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("enteredText: " + box.getText());
hasTextEntered = true;
enteredText = box.getText();
}
};
test2.handleIfTextEntered(hasTextEntered);
}
public String handleJFieldID() {
return enteredText;
}
// imports...
public class test2() {
public boolean handleIfTextEntered(boolean ifTextEntered) {
return ifTextEntered;
}
public String jFieldID() {
while(handleIfTextEntered(false) {
if(handleIfTextEntered(true){
String testID = test.handleJFieldID();
break;
}
}
The above is a small snippet of what I'm trying to do.
I've used helper methods to pass along variables and public fields.
Right now, I am just trying to have the other class get the text from the JTextField
, but later on I will expand on this to suit the rest of my code. It'll ultimately be used to input a link, and the rest of my code will do what it was supposed to. This will replace scanners more or less in my code since I want to turn this into a small app eventually.
Your code has serious problems (copy and paste problems?), let's start with the class declaration:
public class test1 extends canvas() {
public class test2() {
In that declaration the (), they are of more, these are used in the declaration of methods, not in those of classes neither in their extends.
On the other hand we have the method that creates the Action, it is very nice, although you do not use it, because you do not apply it to anything, in the same method, but outside the code invoked by the Action, appears a test2, that I guess must be an object test2 that is not declared in the class (difficult to know because the name of the class you declare it in small letters, if it was the name of the class, the invoked method should be static), that is to say that the method will be called when the method jFields() is called and not when the user writes the JTextField.
public class Test1 extends Canvas {
Test2 test2;
String enteredText;
public JTextField box;
// not extending from any component (I don't know what is “canvas”),
// I don't know where the *JTextField* comes from, I have passed it
// in the constructor of the class, but the correct implementation
// depends on the rest of the code.
public Test1( JTextField b ) {
box = b;
// instanciate "Test2" object
test2 = new Test2();
// add a "KeyListener" no an "ActionListener" to "box
box.addKeyListener( new KeyAdapter() {
@Override
public void keyTyped( KeyEvent e ) {
// if the key pressed is “Enter”.
if( e.getKeyCode() == KeyEvent.VK_ENTER ) {
System.out.println( "enteredText: " + box.getText() );
enteredText = box.getText();
// we dispense with the boolean, making the check at source
if( enteredText.isEmpty() ) {
test2.handleIfTextEntered( enteredText );
}
}
}
} );
}
// change to descriptive name
public String getFieldText() {
return enteredText;
}
}
public class Test2 {
String text;
public void handleIfTextEntered( String enteredText ) {
text = enteredText;
}
}
PS: in your code, the enteredText statement is also missing.