0

I have created a sudoku board with 2D JTextFields of 9x9. User is allowed to enter text in any of the textfields. Here the problem starts. As the user is entering data I want to get the index of the JTextField in which user is entering data and simaltaneously get the data of that text field(for validation which is secondary).

Here is the code i have written for the purpose

The code runs but doesn't solve the problem. Any other suggestion will work too. Thanks for your valuable time in advance.

Ansharja
  • 1,237
  • 1
  • 14
  • 37
Joey
  • 13
  • 5
  • You are creating a new layoutSudkou object every time focusGained method gets called ... You need to check the textfield that you already have – Ansharja Oct 06 '17 at 19:43
  • Don't post an image of code. Include the code of your [mcve] directly in the forum so people can test the code if they wish. – camickr Oct 06 '17 at 20:00
  • As soon as i will get my hands on laptop i will put the code in formatted manner.Until then please make best of the image. – Joey Oct 06 '17 at 20:19
  • One thing you could do is make use of the `clientProperty` property of the `JTextField` and seed it with a key, which can then be used to determine where in the model the field appears – MadProgrammer Oct 06 '17 at 22:53
  • If this is not a duplicate, please [edit] your question to include a [mcve] that shows your revised approach; post code, not a _picture_ of the code. – trashgod Oct 07 '17 at 17:57

1 Answers1

0

Please edit your question including your code in a formatted block, also consider to provide a Minimal, Complete, and Verifiable example.

Since you are creating a new layoutSudkou object (ls) every time focusGained method is invoked, the event source will never be a jtextfield of that particular instance (ls.jf [row][column]).

You can save your 2D array as an instance field of your class (the one where you create and add the textfields) and than check which textfield has gained the focus.

You should also take a look at Java Naming Conventions for your code (class names should not begin with a lowercase letter) and, assuming that jf is public field of layoutSudkou class, Why declare variables private in a class or similar questions.

Ansharja
  • 1,237
  • 1
  • 14
  • 37