Use a method from another JavaFX class Java

Adriando :

So if you are familiar with JavaFX, Buttons objects can be modified with the following Node methods

myButton.setTranslateX(10);
myButton.setTranslateY(-10);

Those methods works inside

public void start(Stage primaryStage) throws Exception {}

For which I understand, start is a method in Application to run JavaFX purpose. Since all myButton objects will have the same structure, i tried to make the following method in Main.java file

public void createMyButton(double X, double Y, String label, String image_path) throws Exception {
    this.setTranslateX(X);
    this.setTranslateY(Y);
    this.setText(label);
    //TO DO this.setButtonImage(src=image_path);
 }

However I understand that the methods inside createMyButton are from another class (from Node I think). And (of course) i get the error

Cannot resolve method 'setTranslateX' in 'Main' s

since the compiler is looking those methods in my program, not in JavaFX SDK. How can I call other class methods in my own methods? I tried with

public void createMyButton(bla bla) throws Exception extends Node
public void createMyButton(bla bla) throws Exception extends Application

but i think Iam completely outside the diamond. I also trying to make my own class which inherits methods from other class but it's a little bit outside of my current knowledge and i was wondering if there is a easier/straighter way to do it

Crummy :

I'm not a JavaFX person but I think the issue is that you're calling this.setTranslateX(X); in a method where this is not a button (I think it's a Main object perhaps, need to see more code to be sure).

Try this:

public Button createMyButton(double X, double Y, String label, String image_path) throws Exception {
    Button button = new Button(...) // not sure how you're initialising your buttons normally
    button.setTranslateX(X);
    button.setTranslateY(Y);
    button.setText(label);
    button.setButtonImage(src=image_path);
    return button
}

Then, elsewhere when you want to create a button, you'd call the method instead:

Button button = createMyButton(10, 20, "My Button", "images/button.png")

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=421533&siteId=1