• Post Reply Bookmark Topic Watch Topic
  • New Topic

Beginner stuck at Event Handling  RSS feed

 
Eddie Dylan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone! I just started learning Java in my spare time this semester and over this weekend, I am stuck at understanding how event handling works!

I am trying to do a menu for a simple pizza menu.

This menu lets you choose from 4 sizes, small, medium, large, extra large.
Then, you choose how it is made, thin crust, thick crust, pan baked.
Finally, you add toppings to the pizza, sausages, pepperoni, cheese, green pepper, onion.

The challenge for me is calculating and displaying the cost as the user is still choosing .







Any tip or guidance is much appreciated!
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

For the time being, cut down your app to handle one thing only. Maybe size only without thickness or toppings. That will make it easier for you to see what is happening.
Also be careful about names. What does an array called name mean? What does the number x mean? Those aren't obvious from reading their names, as they should be.
Maybe there is something useful for you in the Java™ Tutorials.
Is item listener the right interface to use? Wouldn't you want a change listener?
Where do those case numbers in the Act class (another bad name, I am afraid) come from?

I think you will have to start again with a simplified app. Two radio buttons in a group. Add a change listener to them. If possible add fields to them, then you can iterate the array, find which one is selected, and get the results of the field. Even if that field is the text associate with the button. Once you work out how to do that, go back to the pizzas.
 
David Simkulette
Ranch Hand
Posts: 67
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eddie Dylan wrote:Hi everyone! I just started learning Java in my spare time this semester and over this weekend, I am stuck at understanding how event handling works!

I am trying to do a menu for a simple pizza menu.

This menu lets you choose from 4 sizes, small, medium, large, extra large.
Then, you choose how it is made, thin crust, thick crust, pan baked.
Finally, you add toppings to the pizza, sausages, pepperoni, cheese, green pepper, onion.

The challenge for me is calculating and displaying the cost as the user is still choosing .







Any tip or guidance is much appreciated!


OK at least you're confused as to when the price should be calculated. The way you wrote it, the price is calculated when Act is constructed. The only action Act takes in response to a button push is t show the price which was already calculated. This can't be what you meant to do. With gigantic methods like you have, and no organizationm to your app, it's hard for you to see what's really happening and hard for us to see what you're even trying to do. If you hold to this style, you're going to be in a continual state of chaos as things get more complicated.

So. Starting at the begininning and ending in just four or so posts, let's write a PizzaCost program together.  At the end of it you will understand Event Handling and more importantly you won't be burying your programming errors under eye-glazing amounts of code, which is your current state.

Deal? 

I'll tell you to write something. You try to do that. Then I'll post the thing I wanted you to write. If there's any dsifference, you can compare the two and see what and why I did what I did. You'll understand everything by the fourth or so post and you'll also your whole game will be elevated.


So if you're up for it, here's the first thing thing to do.

Write a JavaFX application whose start method consists merely of calling just three methods in sucession, then exits without even launching a Stage. 

The methods themselves you'll write, but their bodies (the logic in them) will be empty for now.

Method one has signature :

private void createComponents();

Method two has signature :

private void establishContainmentHierarchy(Scene scene);

Method three has signature :

private void setLAF(Scene scene);

It should both compile and run without errors.

That's it for post one.
 
Rob Camick
Ranch Hand
Posts: 2828
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's it for post one.


1. No need to quote the entire question. If everybody did that it would take forever to scroll through the question.

2. This is a Swing question, not a JavaJX question.
 
David Simkulette
Ranch Hand
Posts: 67
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:
That's it for post one.


1. No need to quote the entire question. If everybody did that it would take forever to scroll through the question.

2. This is a Swing question, not a JavaJX question.


Thanks !!!

Change that to Swing app then we can do either LOL...
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!