To call inherited final method produce "cannot find symbol" error

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!

2 answers

  • answered 2017-11-14 23:16 shmosel

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

  • answered 2017-11-14 23:17 Robert Kock

    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.