473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Event handling

Hi there,

I did this program, but, im having problems with attaching the events. i want the total bill amount to be calculated when i click the ‘OK’ button, and i need it to be displayed in the window. The problem is that I dont know why I dont get why its not adding the total bill of what i selected. It only displays "Total: 0"


im assuming that:

Sandwich = $2 per unit
Tea – $1 per unit
Coke – $1 per unit

Expand|Select|Wrap|Line Numbers
  1. import java.awt.BorderLayout;
  2. import java.awt.FlowLayout;
  3. import java.awt.GridLayout;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6.  
  7. import javax.swing.JApplet;
  8. import javax.swing.JButton;
  9. import javax.swing.JCheckBox;
  10. import javax.swing.JComboBox;
  11. import javax.swing.JLabel;
  12. import javax.swing.JPanel;
  13. import javax.swing.JRadioButton;
  14. import javax.swing.JTextField;
  15.  
  16.  
  17.  
  18. public class Cafeamr extends JApplet  implements ActionListener{
  19. private JButton b1, b2; 
  20. private JCheckBox c1, c2, c3;
  21. //private JRadioButton c5, c6, c7;
  22. private JComboBox q;
  23. private JLabel label1, label2,label3,label4,label5;
  24. private JPanel p1, p2, p3, p4, p5, p6, p7, p8;
  25. private JTextField textbox1,textbox2,textbox3;
  26.  
  27.     public Cafe()
  28.     {
  29.        // build left north panel
  30.        label1 = new JLabel( "Cafe:  My Cafe" );
  31.        p1 = new JPanel();
  32.        p1.setLayout( new FlowLayout( FlowLayout.LEFT ) );
  33.        p1.add( label1 );
  34.  
  35.        // build right east panel
  36.        b1 = new JButton( "Ok" );
  37.        b1.addActionListener(this);
  38.        b2 = new JButton( "Cancel" );
  39.        b2.addActionListener(this);
  40.        p2 = new JPanel();
  41.        p2.setLayout( new GridLayout( 2, 1, 5, 5 ) );
  42.  
  43.        p2.add( b1 );
  44.        p2.add( b2 );
  45.  
  46.        // build left south panel
  47.        label2 = new JLabel( "Payment Mode: " );
  48.        q = new JComboBox();
  49.        q.addItem( "Cash" );
  50.        q.addItem("Credit Card");
  51.        p3 = new JPanel();
  52.        p3.setLayout( new FlowLayout( FlowLayout.LEFT, 10, 0 ) );
  53.        p3.add( label2 );
  54.        p3.add( q );
  55.  
  56.  
  57.        // build left east panel
  58.        label3=new JLabel("Item");
  59.        c1 = new JCheckBox( "Sandwich" );
  60.        c2 = new JCheckBox( "Tea" );
  61.        c3 = new JCheckBox( "Coke" );
  62.        p4 = new JPanel();
  63.        p4.setLayout( new GridLayout(4,1,3,3 ) );
  64.        p4.add(label3);
  65.        p4.add(c1);
  66.        p4.add(c2);
  67.        p4.add(c3);
  68.  
  69.        // build left west panel
  70.        p5 = new JPanel();
  71.        p5.setLayout( new GridLayout(4,1,5,5) );
  72.        label4=new JLabel("Quantity");
  73.        textbox1=new JTextField(2);
  74.  
  75.        textbox2=new JTextField(2);
  76.  
  77.        textbox3=new JTextField(2);
  78.  
  79.        p5.add(label4);
  80.        p5.add(textbox1);
  81.        p5.add(textbox2);
  82.        p5.add(textbox3);
  83.  
  84.  
  85.        // build left center
  86.        p8 = new JPanel();
  87.        p8.setLayout( new FlowLayout( FlowLayout.CENTER, 30, 0 ) );
  88.       // p8.setBackground( Color.white );
  89.        p8.add( p4 );
  90.        p8.add( p5 );
  91.  
  92.  
  93.        // setup left panel
  94.        p6 = new JPanel();
  95.        p6.setLayout( new BorderLayout() );
  96.        p6.add( p1, BorderLayout.NORTH );
  97.        p6.add( p8, BorderLayout.CENTER );
  98.        p6.add( p3, BorderLayout.SOUTH );
  99.  
  100.        // setup applet layout
  101.        p7 = new JPanel();
  102.        p7.setLayout( new FlowLayout( FlowLayout.CENTER, 10, 5 ) );
  103.        p7.add( p6 );
  104.        p7.add( p2 );
  105.        getContentPane().add( p7 );
  106.  
  107.        setSize( 400, 250 );
  108.        show();
  109.     }
  110.  
  111.     public void actionPerformed (ActionEvent d)
  112.     {
  113.         if(d.getSource() ==b1){
  114.             int total= 0;
  115.             int SP = 2;
  116.             int TP = 1;
  117.             int CP = 1;
  118.             //if (d.getSource())
  119.  
  120.             try
  121.             {
  122.                 if (c1.isSelected()==true){
  123.                    String B1 = textbox1.getText();
  124.                     int b1 = Integer.parseInt(B1);
  125.                   total = total + (b1 * SP);
  126.                 }
  127.  
  128.                 if (c2.isSelected()==true){
  129.                     String B2 = textbox2.getText();
  130.                     int b2 = Integer.parseInt(B2);
  131.                       total = total + (b2 * TP);
  132.                 }
  133.  
  134.                 if (c3.isSelected()==true){
  135.                     String B3 = textbox1.getText();
  136.                     int b3 = Integer.parseInt(B3);
  137.                       total = total + (b3 * CP);
  138.                 }
  139.             }
  140.             catch(Exception e) {
  141.                 e.printStackTrace();
  142.             }
  143.              label5 = new JLabel( "Total: "+ total );
  144.              p7.add(label5);
  145.              validate();
  146.             System.out.println(total);
  147.         } // ok button
  148.         else {
  149.             System.exit(1);
  150.         }
  151.  
  152.  
  153. }
  154.     public void init()
  155.     {
  156.         new Cafe();
  157.     }
  158. }
Thanks in advance and i hope you guys can help me out,

cheers

outofmymind
Dec 4 '06 #1
4 1585
horace1
1,510 Expert 1GB
The only problem I noticed was the class was called Cafeamr
Expand|Select|Wrap|Line Numbers
  1. public class Cafeamr extends JApplet  implements ActionListener{
  2.  
and the constructor was called Cafe
Expand|Select|Wrap|Line Numbers
  1.         public Cafe()
  2.  
once I changed the class name to Cafe and ran it with AppletViewer the program worked OK - I entered the quantity in the JTextField, clicked on the item box and then OK and the Total was correct.
Dec 4 '06 #2
r035198x
13,262 8TB
The only problem I noticed was the class was called Cafeamr
Expand|Select|Wrap|Line Numbers
  1. public class Cafeamr extends JApplet implements ActionListener{
  2.  
and the constructor was called Cafe
Expand|Select|Wrap|Line Numbers
  1.         public Cafe()
  2.  
once I changed the class name to Cafe and ran it with AppletViewer the program worked OK - I entered the quantity in the JTextField, clicked on the item box and then OK and the Total was correct.
The code should also not have compiled.
Dec 5 '06 #3
The code should also not have compiled.
Thanks for your help r035198x ! :) :)...it worked

outofmymind
Dec 6 '06 #4
r035198x
13,262 8TB
Thanks for your help r035198x ! :) :)...it worked

outofmymind
You should thank horace1 there. He is the one who gave you the solution. I merely pointed out that your version could not possibly compile.
Dec 6 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Pavils Jurjans | last post by:
Hallo, I have been programming for restricted environments where Internet Explorer is a standard, so I haven't stumbled upon this problem until now, when I need to write a DOM-compatible code. ...
1
by: Covad | last post by:
Hi all, For some reason my change() function is only called when the page loads. I'd much rather it gets called when the select changes. Here's the code: window.onload = init; function...
4
by: Eric | last post by:
How can I dynamically assign an event to an element? I have tried : (myelement is a text input) document.getElementById('myelement').onKeyUp = "myfnc(param1,param2,param3)"; ...
18
by: Christopher W. Douglas | last post by:
I am writing a VB.NET application in Visual Studio 2003. I have written a method that handles several events, such as closing a form and changing the visible status of a form. I have some code...
8
by: Mark | last post by:
Hi, I'm looking for some ideas on how to build a very simple Event processing framework in my C++ app. Here is a quick background ... I'm building a multithreaded app in C++ (on Linux) that...
4
by: hillcountry74 | last post by:
Hi, I'm a newbie and trying to understand event handling in c#. I have understood handling events using delelgate objects. But not this method- "Event handling by overriding the virtual...
3
by: Ashok Kumar K | last post by:
Hi all, Where can I get some insight on using the __hook, __unhook, event_source and event_receiver for specifically COM events. The documentation given in MSDN is very minimal. I have the...
2
by: Paul E. Orman | last post by:
I have a piece of VB code (.NET 1.1 - VB 2003) that loads data from a database through a timer. So the timer is setup and from it I call the procedure that loads the latest records from the...
4
by: mflll | last post by:
I am looking into the different techniques of handling arrays of edit boxes in Java Script. The first program below works fine. However, are there better ways of doing this, where the person...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.