0

how can i use KeyEvent to replace JTextField.getSource in this part so that when an input is typed, there's no need for pushing "Enter"

public void actionPerformed(ActionEvent e) {

        int rowSelected = -1;
        int colSelected = -1;



        JTextField source = (JTextField) e.getSource();
        boolean found = false;
        for (int row = 0; row < GRID_SIZE && !found; ++row) {
            for (int col = 0; col < GRID_SIZE && !found; ++col) {
                if (tfCells[row][col] == source) {
                    rowSelected = row;
                    colSelected = col;
                    found = true;
                }
            }
        }

        int answer = Integer.parseInt(tfCells[rowSelected][colSelected].getText());
        if (answer == puzzle[rowSelected][colSelected]) {
            tfCells[rowSelected][colSelected].setBackground(getColor(puzzle[rowSelected][colSelected]));
            count--;
        } else {
            tfCells[rowSelected][colSelected].setBackground(Color.RED);
        }
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    Are you trying to get user input as he types? Use a [DocumentListener](https://docs.oracle.com/javase/7/docs/api/javax/swing/event/DocumentListener.html) instead... – Frakcool Apr 17 '18 at 16:25
  • 1
    You don't. You use a DocumentListener (as @Frakcool states) or a DocumentFilter. – Hovercraft Full Of Eels Apr 17 '18 at 16:26
  • Actually I have learned that there's a `DocumentFilter` thanks to your comment @HovercraftFullOfEels :) – Frakcool Apr 17 '18 at 16:27
  • @Frakcool: they're used for 2 different purposes. If you just want to listen for change, use the doc listener. If you want to *filter* the input so as to prevent certain types of input from being accepted, for instance, preventing text from being entered into a JTextField, and only allowing numeric input, then that's where you'd use the doc filter. – Hovercraft Full Of Eels Apr 17 '18 at 16:28
  • That's the one new thing to learn daily for me :) Thanks @HovercraftFullOfEels perfectly explained – Frakcool Apr 17 '18 at 16:30
  • hi thx for helping. can you maybe give an example cuz im quite a greenhand to that – Erik Han Apr 17 '18 at 16:35
  • @ErikHan: please look at the duplicates used to close this section. And please search more deeply *before* asking questions that are likely duplicates. The links above were found by simply searching on java JTextField KeyListener terms. – Hovercraft Full Of Eels Apr 17 '18 at 16:36
  • `there's no need for pushing "Enter"` how do you know when the user is finished typing in the text field if they don't press Enter? `can you maybe give an example` - Start with the [Swing tutorial](https://docs.oracle.com/javase/tutorial/uiswing/TOC.html). The sections on `How to Write a DocumentListener` or `Text Component Features` have working examples. And of course now that you know what classes to use you can search the forum/web for examples that use those classes. – camickr Apr 17 '18 at 16:47
  • like in Sudoku game, user only needs to enter a single number so that there is no need for pushing enter – Erik Han Apr 18 '18 at 04:53

0 Answers0