2

I have this code:

import javafx.scene.control.TextArea;
import javafx.event.EventHandler;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.KeyCode;
import javafx.scene.Node;

public class InitialTextFixedArea extends TextArea{
    public InitialTextFixedArea(String testoIniziale){
        super(testoIniziale);
        EventHandler<KeyEvent> eventHandler = new EventHandler<KeyEvent>(){
            @Override
            public void handle(KeyEvent e){
                if((e.getEventType() == KeyEvent.KEY_PRESSED & this.getCaretPosition() < 5)
                    |(this.getCaretPosition() == 5 & e.getCode() == KeyCode.BACK_SPACE)){
                    this.setEditable(false);
                }else{ 
                    this.setEditable(true);
                }
            }
        };
        this.addEventFilter(KeyEvent.KEY_PRESSED, eventHandler);
        this.addEventFilter(KeyEvent.KEY_RELEASED, eventHandler);
    }
}

and when I try to compile, I obtain more than one error of type "cannot find symbol", where the symbols are the getCaretPosition() and setEditable() methods. Do not I inherit these methods from TextArea that inherit them from TextInputControl?

Thank you for the answers!

Mr-IDE
  • 7,051
  • 1
  • 53
  • 59

2 Answers2

0

this in this context points to the EventHandler, which doesn't have those methods. Change it to InitialTextFixedArea.this.getCaretPosition() or just getCaretPosition().

shmosel
  • 49,289
  • 6
  • 73
  • 138
0

You're calling this.getCaretPosition() from within an EventHandler. In that case this refers the the event handler and not to the TextArea.
Try calling it without this.

Robert Kock
  • 5,795
  • 1
  • 12
  • 20