• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Calling methods from other classes in JavaFX

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a javafx app which generates mazes using a binary tree algorithm . However, I want to move alot of the methods outside of the main class and then just call them from the main class to make the code more readable . I have tried using interfaces to try and simulate multiple inheritance to access the methods but this didnt work because interfaces have no body. I then tried making instances of the other classes then calling the method but this just hung the ui because the new instance made a new object . Below is one of the methods I want to move into another class called 'delNode' it works by taking in start and end coordinates of the line then removing it from the pane. Anyway basically to keep it simple is there a way of calling non static methods from another class('s) without having to make an instance of the class? **making a new instance also causes nullpointer exception


 
Sheriff
Posts: 6171
163
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think I'm understanding.  Why don't you make this a method in the Main class?  Or, if you want separate objects, why would instantiating an object be a problem?  I think I need to see a chunk on your Main class where you're trying to call the method of a new object.

Remember, all the UI is called from one thread, so if you need to do something that won't hang the UI, it can't be done on the UI thread.
 
Knute Snortum
Sheriff
Posts: 6171
163
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code for running a task on a thread other than the UI thread would look like this:
 
Will Isom
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:I don't think I'm understanding.  Why don't you make this a method in the Main class?  Or, if you want separate objects, why would instantiating an object be a problem?  I think I need to see a chunk on your Main class where you're trying to call the method of a new object.

Remember, all the UI is called from one thread, so if you need to do something that won't hang the UI, it can't be done on the UI thread.



I added the relevant snippets . Yeah idk why it doesnt work when I make a new object of the class then call the  method
 
Knute Snortum
Sheriff
Posts: 6171
163
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please note: when you add something new, make a new post.  Don't edit the original post.  It makes things very confusing.

This isn't going to work.  In Main you create a DeleteWall object, but in the constructor you create a Main object.  That tries to create a DeleteWall object, which tries to create a Main object, ad infinitum.

Why don't you start with dividing your Main class into methods, then see if adding other classes makes sense.  Classes are usually named as nouns and methods as verbs.  So do you need a thing or an action?  This will help determine if you need a class or a method.
 
It is difficult to free fools from the chains they revere - Voltaire. tiny ad:
professionally read, modify and write PDF files from Java
https://products.aspose.com/pdf/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!