In JAVA,Briefly explain what the following listener code will does. Assume you h
ID: 3829529 • Letter: I
Question
In JAVA,Briefly explain what the following listener code will does. Assume you have the following components.
JTextField input;
JLabel output;
The listener class is registered to input. The original text displayed in input is "Enter something here" and the original text displayed in output is "waiting".
public class InputFieldListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String inputText = input.getText();
boolean correct = inputText.equalsIgnoreCase("something") ? true : false;
output.setText(correct ? "you got it" : "not quite");
if(correct) {
input.setEditable(false);
}
}
}
Explanation / Answer
There is a InputField that listens to a listener such that :-
1. String inputText = input.getText(); //Whatever the user eneters comes in as inputText
2.boolean correct = inputText.equalsIgnoreCase("something") ? true : false; //If the user enters something in inputText , then correct will be true otherwise false
3. output.setText(correct ? "you got it" : "not quite"); //The Label will display as "you got it" if correct at statement 2 is true otherwise output will be "not quite"
4. input.setEditable(false); // if correct become true in statemnet 2 then the TextField is no more editable
Thanks, let me know if there is any concern, This is the explanation of what the code does.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.