0

In my frame, I have a JTable and a JTabbedPane.

The JTabbedPane contains a JTextField for a Date.

When it is filled with a date and Enter or Tab key is pressed, the "Date" column of the JTable has to be filled.

I have a method to check whether the text field contains a valid date or not.

wchargin
  • 15,589
  • 12
  • 71
  • 110
Praful
  • 43
  • 2
  • 6

1 Answers1

2

You could do this with an ActionListener.

JTextField textField; // this is your text field
textField.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent ae) {
        // Place your code to fill the JTable here.
        // Maybe something like:
        tableModel.setValueAt(textField.getText(), row, column);
    }
}

In the above example, make sure to reference textField, row, and column correctly.


EDIT: As MadProgrammer pointed out, a FocusListener would provide an even broader implementation: whenever the text field loses focus, the code could be run. That would be implemented as follows:

textField.addFocusListener(new FocusAdapter() {
    @Override
    public void focusLost(FocusEvent fe) {
        // Place your code to fill the JTable here.
        // Maybe something like:
        tableModel.setValueAt(textField.getText(), row, column);
    }
}
Community
  • 1
  • 1
wchargin
  • 15,589
  • 12
  • 71
  • 110