1

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

1
  • Ooooh you right and it gave me an idea that seems to be working, instead of a void method am changing it to return Button method Commented Feb 12, 2020 at 1:05

1 Answer 1

2

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")
Sign up to request clarification or add additional context in comments.

1 Comment

yeap, seem to be working. Changing Void to a Return method instead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.