0

I made a method mames "si()" in my CaretListener "as" but I cant call it, the error says "cannot find symbol" somebody can tell me why I cannot find this method

public class FoxTable {


      private boolean TECLADO=false;

      public FoxTable(){
        Accion();
      }


      private void Accion() {
            CaretListener as=new CaretListener() {


                @Override public void caretUpdate(CaretEvent e) {
                    TECLADO=true;
                    System.out.println(e.getDot()+" Dot");
                    System.out.println(e.getMark()+" Mark");
                }

                public boolean si(){
                    if(TECLADO){TECLADO=false;return true;}
                    return true;
                }
            };

            KeyAdapter focus=new KeyAdapter() {

                @Override public void keyPressed(KeyEvent e) {

                    switch(e.getKeyCode()){        

                        case 10:
                          e.getComponent().transferFocus();
                          as.si();
                          break;
                    }                
                }
            };
     }
}

thanks for yours answers and advices!

WearFox
  • 293
  • 2
  • 19

1 Answers1

3

Because your anonymous KeyAdapter inner class doesn't have the 'as' instance in its scope.

You could consider passing your 'as' instance as an attribute or your anonymous KeyAdapter inner class.

EDIT

So, my thought of passing in the 'as' instance variable to an anonymous inner class will not work, please take a peek at the following stack overflow question: Accessing constructor of an anonymous class

So, instead, what I recommend is moving the si() method from inside the CaretListener anonymous inner class to the FoxTable class. Then both anonymous inner classes can call the method as needed.

public class FoxTable {

private boolean teclado = false;

public FoxTable (){
    Accion();
}

public boolean si(){
    if (teclado) {
        teclado = false;
        return true;
    }
    return true;
}

private void Accion() {

    CaretListener as = new CaretListener() {
        public void caretUpdate(CaretEvent e) {
            teclado = true;
            System.out.println(e.getDot()+" Dot");
            System.out.println(e.getMark()+" Mark");
        }
    };

    KeyAdapter focus = new KeyAdapter() {
        public void keyPressed(KeyEvent e) {
            switch(e.getKeyCode()){
                case 10:
                    e.getComponent().transferFocus();
                    si();
                    break;
            }
        }
    };
}

EDIT X 2

SO, if you have to have the si() method in your inner class, this is the only way that I know of doing it. You will not be able to use anonymous inner classes, you're going to need to use declared inner classes and then extend the interfaces of the two different listeners you're using.

public class FoxTable {

    private boolean TECLADO=false;

    public FoxTable(){
        Accion();
    }

    private void Accion() {
        ExtendedCaretListener as = new ExtendedCaretListener();
        KeyAdapter focus = new ExtendedKeyAdapter(as);
    }

    public class ExtendedCaretListener implements CaretListener {
        public void caretUpdate(CaretEvent e) {
            TECLADO=true;
            System.out.println(e.getDot()+" Dot");
            System.out.println(e.getMark()+" Mark");
        }

        public boolean si(){
            if(TECLADO){
                TECLADO=false;
                return true;
            }
            return true;
        }
    }

    public class ExtendedKeyAdapter implements KeyAdapter {

        ExtendedCaretListener as = null;

        public ExtendedKeyAdapter(ExtendedCaretListener as) {
            this.as = as;
        }

        public void keyPressed(KeyEvent e) {
            switch(e.getKeyCode()){
                case 10:
                    e.getComponent().transferFocus();
                    as.si();
                    break;
            }
        }
    }
}

Perhaps someone else will be able to chime in on a different way of doing this.

Community
  • 1
  • 1
hooknc
  • 4,854
  • 5
  • 31
  • 60
  • it doesn't work a put it inside of my KeyAdapter inner class but its the same problem – WearFox Oct 01 '14 at 22:03
  • So, we might need to see a bit more code. You're only showing two anonymous inner classes that are trying to access a parents attribute called TECLADO. Could you show us how TECLADO is being used in the parent class? We don't need the entire class, but a bit more code that still compiles would be helpful. – hooknc Oct 01 '14 at 22:11
  • If you have time, could you update the code in your question above? I don't think the comment sections accept code fragments very well. – hooknc Oct 01 '14 at 22:16
  • Yes its works like that but I what want is use the methods insede the inner class, I thank you for you answer – WearFox Oct 02 '14 at 01:00
  • I don't think you're going to be able to do what you are trying to do with anonymous inner classes. An anonymous inner class cannot add to its public interface. Meaning, that if you look at the CaretListener interface, those are the only methods that classes outside of that anonymous CaretListener can call. The anonymous KeyAdapter will not be able to see that si() method. You will have to actually create a declared inter class that extends CaretListener so you can add the si() method. – hooknc Oct 02 '14 at 15:39
  • Then to get the KeyAdapter to call the si() method, you will have to add a setter method to the anonymous KeyAdapter that will take your new inner class CaretListener. – hooknc Oct 02 '14 at 15:41
  • oooh thats true, thats a perfect idea... Great! thanks – WearFox Oct 02 '14 at 19:21