In my Java Swing app, I have multiple JTextFields for dates, there is a JButton when clicked, it will open a calendar to pick a date, and the date string should be inserted into one of the JTextFields, so I want to design the program so that user first clicks in a date JTextField he wants to input a date [ get focus on that field and remember it ], the program saves the JTextField as target component, then that component is passed to the calendar object to input the picked date. So far I can hard code the program so it can input any date string I pick in to a certain JTextField I've hard coded with, but the problem is how to remember the JTextField user clicked on ? So I don't have to hard code it.
I've tried : Date_Field.getDocument().addDocumentListener(A_Listener); It won't get the focus until user starts typing in it.
I've also tried :
Date_Field.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Focused_Date_TextField=(JTextField)evt.getSource();
}
});
Also didn't work, because when user clicked on it, there is no action yet [ therefore no focus ] until user starts typing.
So what's a way to get the JTextField when user JUST clicks on it without typing anything ?
Thanks for the answers and suggestions, but they are not what I was looking to find. Too much trouble and I don't have enough space in my app to add a button to each field, even if I do have enough space, they look too busy for dozens of fields each has it's own button, I want to clean simple look and yet still functions the way I like, so I did some search and found the ideal answer, here is how to achieve it :
Date_TextField[Index].addFocusListener(new FocusListener()
{
public void focusGained(FocusEvent e)
{
Out("Focus gained : "+e.toString());
Focused_Date_TextField=(JTextField)e.getSource();
}
public void focusLost(FocusEvent e)
{
Out("Focus lost : "+e.toString());
}
});
I then pass Focused_Date_TextField to the calendar, so when a date is picked, the date text will be input into the user selected JTextField.