0

Was marked as duplicate, but still: my question does not refer to override annotations in general, but specifically in fxml controllers.

I am pretty new to java and trying to build a fxml application. I want to add an @Override annotation to a java fxml controller, but I am getting the error message "error: method does not override or implement a method from a supertype", and I cannot figure the origin of the mistake.

The same annotation can be used without any problem to the main java document, so I don't why it is not possible in the controller. The function only is effective once used with the @Override annotation, so commenting it out makes the function useless.

Is is impossible to use additional @Overrides in the controller, or is it a matter of handling them correctly?

public class Main_FXMLController implements Initializable {


private Button button;

@FXML
private void handleButtonAction(ActionEvent event) throws IOException {      
    System.out.println("You clicked me!");        
    Platform.exit();

}


 // I am trying to add to @Override annotation here, but it returns " method does not override or implement a method from a supertype"
 @Override
 private static void test () {
     System.out.println("Test");

 }


@Override
public void initialize(URL url, ResourceBundle rb)  {

}  
rainer
  • 3,295
  • 5
  • 34
  • 50
  • 5
    Why would you want to do this? What do you think `@Override` means? Do you normally add random bits to your code? – Jim Garrison Jan 02 '17 at 18:08
  • 1
    BTW: annotating a `static` method with `@Override` NEVER works... (`static` methods cannot be overridden) – fabian Jan 02 '17 at 18:12
  • fabian, removing static does not change the result. – rainer Jan 02 '17 at 18:27
  • 1
    What is the parent-type method you are trying to override? Your type `implements Initializable`, so any methods you wish to override must be declared either in `Initializable` or `Object`, and be accessible. Did you read the answers to the question duplicated by yours? – Lew Bloch Jan 02 '17 at 20:49
  • 1
    Also, it's pretty safe to say that in general adding any code "at random" to your program is not going to be useful. – Lew Bloch Jan 02 '17 at 20:54
  • of course not totally at random ... – rainer Jan 02 '17 at 21:00
  • Lew, read my way through the post, but couldn't find what I need ... you say "any methods you wish to override must be declared .. in Initializable" but is mine not?? Bear with me, please ... – rainer Jan 02 '17 at 21:08
  • You need to answer the first comment to get any chance of a meaningful answer to this. What do you think adding the `@Override` annotation here is going to do? As for: "my question does not refer to override annotations in general, but specifically in fxml controllers" what on earth do you think the difference is? `@Override` means exactly the same thing in a FXML controller as it means in any other class. – James_D Jan 04 '17 at 02:32
  • Good point ... checking this, it really has nothing to do with the controller, and more with the type of class I am trying to use or to which I am trying to connect. From Jim's posted link I understood that is has something to do with " misspelling a method name or not correctly matching the parameters ... Literally, I would understand that one is trying to override basic configurations that come with preconfigured files, like an fxml file. Searching more, it also has something to to with inheritance and calling "extends application" .... still all a little cloudy .... – rainer Jan 04 '17 at 18:05
  • The annotation is for overriding methods from the parent class (and/or in the inheritance hierarchy) - with it you are denoting the method is to be overriden with your implementation (not the values/configurations, etc) - what are you trying to accomplish? - the error you are getting means the parent (or the hierarchy) does not have a method with the same signature of `test()` – blurfus Jan 10 '17 at 00:49

0 Answers0