I need help working with getFocusOwner (). I have a Sudoku game that I created in Java and I want to add a cursor arrow to the program. I did some research and found that using a focus system would be a better way (if it is not, please give me a better way and I can investigate this).
Okay, so for testing purposes, I'm trying to focus on SetField, a custom class that extends JTextField (Custom because I wanted to disable any inputs that were not numbers). He is focused on focus. I called requestFocusInWindow () on SetField in the middle of the grid, and the focus is set to this component. The problem arises when I try to find a component that has focus.
This is what I do to check the getFocusOwner () method:
sGrid[40].requestFocusInWindow(); try{ System.out.println(this.getFocusOwner().getClass().getSimpleName()); } catch(NullPointerException e){ e.printStackTrace(); }
No matter which component I'm trying to include, I always get a null pointer exception. I tried this with JButtons, JLabels and even with JPanels in my program. However, the focus is on the component. I see that the caret is blinking in the alleged SetField. Is there something I'm doing wrong? Thanks in advance for any help.
source share