• 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Make buttons add values to a text field for a vending machine application  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

imgur screenshot

I made a vending machine with windowbuilder on eclipse, but i'm unsure of how to make the buttons with prices add up into the textfield and how to make the purchase buttons deduct the price of the item from the total value in the textfield.
I don't know if i'm making sense but any advice would be helpful, thanks.

//A1 price heading
JLabel lblA1Price = new JLabel("\u00A31.00");
lblA1Price.setBounds(79, 142, 48, 14);
frame.getContentPane().add(lblA1Price);

//A1 Icon Image
JLabel lblA1img = new JLabel("");
lblA1img.setIcon(new ImageIcon(VendingApp.class.getResource("/imgs/sodabottle.png")));
lblA1img.setBounds(35, 167, 128, 150);
frame.getContentPane().add(lblA1img);

//A1 purchase button
JButton btnA1 = new JButton("A1");
btnA1.setBounds(62, 324, 89, 23);
frame.getContentPane().add(btnA1);




//5p button
JButton btn5p = new JButton("\u00A30.05");
btn5p.setBounds(603, 405, 89, 23);
frame.getContentPane().add(btn5p);

//10p button
JButton btn10p = new JButton("\u00A30.10");
btn10p.setBounds(702, 405, 89, 23);
frame.getContentPane().add(btn10p);

//20p button
JButton btn20p = new JButton("\u00A30.20");
btn20p.setBounds(603, 439, 89, 23);
frame.getContentPane().add(btn20p);

//50p button
JButton btn50p = new JButton("\u00A30.50");
btn50p.setBounds(702, 439, 89, 23);
frame.getContentPane().add(btn50p);

//1 pound button
JButton btn1Pd = new JButton("\u00A31.00");
btn1Pd.setBounds(603, 473, 89, 23);
frame.getContentPane().add(btn1Pd);

//2 pound button
JButton btn2Pd = new JButton("\u00A32.00");
btn2Pd.setBounds(702, 473, 89, 23);
frame.getContentPane().add(btn2Pd);

 
Marshal
Posts: 64494
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I'm afraid the image you added to your post doesn't appear correctly. Please avoid images as far as possible.
Avoid setBounds() except possibly for top‑level containers. Use a layout manager. Swing works better with layouts. Only use GUI builders when you have enough experience in writing GUIs to edit the generated code if you need to.
Beware of starting variable names with their type: priceLabel rather than labelPrice. And don't abbreviate variable names.
You don't need to say getContentPane(); you can simply say frame.add(xyz);

You haven't shown how you are using the buttons. Are you adding listeners of any kind?
 
Isla Oughton
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what the whole code looks like at the moment, i will remake it again with a layout and rename the variables without abbreviations.
I would like to add event handlers to the insert amount buttons so they would do additions in the textfield,
and on the buttons of the items like A1, A2, A3 to substract the item value from the textfield.
But i'm not sure how i'm supposed to do it, thanks.






 
Rancher
Posts: 3041
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. You have already asked this question and received an answer: https://stackoverflow.com/questions/56264433/how-do-i-make-buttons-with-different-values-add-to-a-textfield-instead-of-replac

What part of the answer don't you understand? We can't guess what you find confusing.

2. Don't post your application. We don't have time to read 300 lines of code.

Your question is about updating a text field when a button is clicked.

So you need to:

1.  create a JFrame with a JTextField (with a default value of 0) and a JButton.
2. then you add an ActionListener to the button. In the ActionListener code you get the value of the text field and the value of represented by the button, add the two together and then update the text field with the new value.

Get this working for one button first. If you have problems you have a simple SSCCE to post in the forum.
 
Isla Oughton
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry for re-asking the question again, i just didn't understand what to do.
This is what I attempted, i'm not sure if it's the correct way,
but when i click the button it seems to add to the textfield

 
Campbell Ritchie
Marshal
Posts: 64494
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Isla Oughton wrote:I'm sorry for re-asking the question again, i just didn't understand what to do. . . .

You post a question saying you don't understand the answer. Don't post your question on a different thread. If you post a new question on a different website, make sure that people reading it know it is a cross‑post. That usually applies to both places you have posted.

    * * * * * * * *

Don't use the contents of your price label as data. The label is supposed to reflect and display what is in your application, not to be the application itself.
Don't use doubles for money. Use integer arithmetic with the prices denominated in pence, or, much better, use BigDecimals. Don't use this constructor; use this one. BigDecimal has lots of constructors, but I think you only need that one for this application.
Make sure you have a VendingMachine object somewhere, with methods like add(Item), supplyOrder(), receivePayment(BigDecimal), and hasEnoughMoney().
You should be able to run this sort of code from the command line without a GUI.Then you create a similar vending application object as a field of another object, and that other object also has the GUI as a field. To go back to what I said yesterday:-You can expand those action listeners to update your figuresI am sure you will know how to write those methods. Have a look at these two posts for more information about BigDecimal: 1 2. Since you aren't dividing anything, you can ignore some of the problems I spoke about.

They told you about document listeners on SO. have a look in the Java™ Tutorials. But I am not convinced that is what you want. You don't stand by a vending machine altering the price on its little screen until you get the correct amount. You put money in the slot until you get the right amount; you will have to model that with buttons.

Careful about naming variables. It isn' immdeiately obvious that buttonTWO_PD means two pounds. Only use UPPER_CASE and underscores when writing the names of constant.
 
Campbell Ritchie
Marshal
Posts: 64494
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only let the supplyItem() method complete normally if you actually have paid for the goods. If you try to run that method without sufficient cash, be sure to make it throw an Exception.
 
Good heavens! What have you done! Here, try to fix it with this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!