void addClick(JTextField field,String s) {
field.addMouseListener(new MouseInputAdapter() {
public void mouseClicked(MouseEvent e) {
//when user clicks on amount text field,
//makes text field editable to enable user to enter amount in it
if (field.getText().equals(s)) {
field.setText(""); // sets the deafult text as empty string and display it
field.setEditable(true);
}
}
});
}
JTextField is passed as parameter to addClick method. Initially it is not editable, i.e. setEditable(false). But when user clicks on it should be made editable and user can enter the data through keyboard. MouseListener is working fine, i.e. it is making text field editable (sets setEditable(true)), but when I enter text in text field, it is not displaying the data that I am entering. By the way, I have implemented all the other methods of mouse listener interface.
Can I know how I can make text field to display data user is entering through keyboard as text field is not reading any input from the keyboard.
With this code, the program will make the JTextField editable if its content equals the String s. If the predefined/current content is not equal to s, then the JTextField won't become editable. Since you wrote only this one void method, I don't know the content, but if it is not equal to s, I assume the JTextField won't be editable.
If you want the JTextField to be editable no matter whether the current content equals s or not, then you must write
field.setEditable(true);outside the if condition.