w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
How do I "getActionCommand" from a JTextField? I SEE it when debugging but

If you want to know which component generated the event you should be using the event to get the source:


The action command is only for the ActionListener (in which case you would use the getActionCommand() method of the ActionEvent) and not a KeyListener.


Create a Map to hold the information you need:

Map<Component, Point> textFields = new
Hashmap<Component, Point>();

Now in the addCell method you add the data to the map:

textFields.put(cell, new Point(row, col));

Then in the KeyListener you access the Point:

Point whatever = textFields.get(event.getComponent());

Now you know the row/column of the text field where the text was entered.

Or, as MadProgrammer has already suggested you can add your Point as a client property of each JTextfield. That approach is probably a little easier than this suggeston. However, that approach will create a Map for each JTextField and this approach only creates a single Map for all text fields so it is more efficient.

© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.