• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Desperate for some help with event handling with JavaFX!

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi,

I'm trying to create a small program that displays shapes on screen. The user types in both the shape they want and the colour, and then the program will display the shape in the centre of the screen.

I understand that it's related to action events and possibly action listener, but I'm getting really bogged down with how to actually implement it and can't see the wood for the trees, so to speak. I was running through it in my head and thought maybe an if statement related to the event handler might be the way to go? So if the user types in say, Rectangle and Blue, display the given shape on the screen.

Here is the code I have so far: Pastebin  I've followed some tutorials online so far as getting the class set up for event handling, but I don't even really understand what I did. The more I read the more I realise I don't know!

Here's what my GUI currently looks like when it runs: imgur

Can anyone give me any pointers on where to start?

Thanks!
 
Marshal
Posts: 72913
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please post your code here; people are often reluctant to go to distant websites.

Linden Garcia wrote:. . . related to action events and possibly action listener . . .

No, it isn't. Action events and action listeners are used in AWT/Swing, not FX. You have obviously had the misfortune to find the wrong sort of tutorial. I hardly know any FX, but you usually pass a λ to a setOnXXX() method. Let's see your code please.
 
Linden Garcia
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch

Please post your code here; people are often reluctant to go to distant websites.

Linden Garcia wrote:. . . related to action events and possibly action listener . . .

No, it isn't. Action events and action listeners are used in AWT/Swing, not FX. You have obviously had the misfortune to find the wrong sort of tutorial. I hardly know any FX, but you usually pass a λ to a setOnXXX() method. Let's see your code please.





This is the GUI so far:

 
Marshal
Posts: 26598
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I moved the thread to the JavaFX forum in the hope that people who knew something about JavaFX would see it.

I'm not one of those people but I'm sure that having your Application implement EventHandler is the wrong thing to do. It's been the wrong thing to do in Swing for over 15 years now and JavaFX isn't that different. So I tracked down a tutorial about how to use an EventHandler for a JavaFX Button:

Using JavaFX UI Controls

There's "back" and "forward" buttons there which I expect go to pages which deal with other kinds of control.
 
Linden Garcia
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:I moved the thread to the JavaFX forum in the hope that people who knew something about JavaFX would see it.

I'm not one of those people but I'm sure that having your Application implement EventHandler is the wrong thing to do. It's been the wrong thing to do in Swing for over 15 years now and JavaFX isn't that different. So I tracked down a tutorial about how to use an EventHandler for a JavaFX Button:

Using JavaFX UI Controls

There's "back" and "forward" buttons there which I expect go to pages which deal with other kinds of control.



Ah, I didn't notice the JavaFX board or I would've posted it in there - thanks!
And thanks for the link, I'll read through; hopefully it will add some more context.
 
Saloon Keeper
Posts: 4497
166
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To see any shape, you have to give it something to show it in. A Label might be a good start.
For instance: add this in your setup:

Now, label, and the shapes A, R and C should be fields in your class, so that they are in scope in other methods as well.
Next, add an ActionListener to your button

and add the method

Admitted, this is a very rudimentary example, but it gives an idea how you can get it done. At first I wanted to use a Canvas in stead of a Label, but a Canvas does not have a way do draw a Shape, as far as I know. Happy experimenting!

Edit: oops! Since I refer to the TextField T1 in the handle method, T1 should also be an instance member,!
 
Campbell Ritchie
Marshal
Posts: 72913
330
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:. . . Since I refer to the TextField T1 in the handle method, T1 should also be an instance member,!

As an alternative, you can pass a reference to T1 as an argument to that method, in which case the method might well be static.
 
Piet Souris
Saloon Keeper
Posts: 4497
166
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Never thought you liked static methods!  

But there is also the color textfield, and creating a two-parameter static method for the two textfield is not very elegant, imho
 
Campbell Ritchie
Marshal
Posts: 72913
330
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't. But such a method wouldn't use any fields of the object, so it might be static.
 
Piet Souris
Saloon Keeper
Posts: 4497
166
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It uses the label and the shapes. But OP has some other issues to solve. Wonder how he/she copes.
 
Linden Garcia
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the replies everybody! I’ve been working through it the last few days and finally cracked it, your comments were really helpful; I think I’m getting to grips with it now.
 
Piet Souris
Saloon Keeper
Posts: 4497
166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Glad to be of help!

The problem that I mentioned above is the problem of what the user is supposed to type into the text boxes,  and since that is free text, how you can derive the shape and color. Therefore I had the idea of using Comboboxes, giving the user some fixed options. But that's just a minor detail.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic