0

Hi guys I need help getting an event from a button in one class, so that i can act upon it in my other class. Because the class that invokes the class that has the buttons in it is the outer class, it has no access to the buttons on the inside. How can i do this?
I believe this is somewhat the answer, but i don't really understand it. If it is can someone enlighten me?
http://www.daniweb.com/forums/thread223262.html
Here is my code.
OrderTake:

import java.util.Scanner;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class OrderTake {
	
	public static void main (String args[]) {

		JFrame frame = new JFrame("Order Taker");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setPreferredSize(new Dimension(400,400));
		
		JTabbedPane tabpane = new JTabbedPane();
		OrderItem oI1 = new OrderItem(new ImageIcon("pizza.jpg"),10.00,20.00,30.00);
		OrderItem oI2 = new OrderItem(new ImageIcon("beverages.jpg"),2.33,2.75,3.00);
		OrderItem oI3 = new OrderItem(new ImageIcon("images.jpg"),3.00,5.00,7.00);
		OrderItem oI4 = new OrderItem(new ImageIcon("desserts.jpg"),2.20,3.30,4.00);
		tabpane.addTab("Pizza",oI1);
		tabpane.addTab("Beverages",oI2);
		tabpane.addTab("Sandwhiches",oI3);
		tabpane.addTab("Desserts",oI4);
		
		frame.getContentPane().add(tabpane);
		frame.pack();
		frame.setResizable(false);
		frame.setVisible(true);
	}
}

OrderItem:

import java.util.Scanner;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

	public class OrderItem extends JPanel{
		private JPanel panel, panel2,panel3;
		private JButton b1,b2,b3,b4;
		private JLabel label, label2, label3, label4, label5, label6;
		private int qCount=0, sCount=1;
		private double priceS, priceM, priceL, currentPrice,orderTotal;
		
		public OrderItem(ImageIcon icon, double priceS, double priceM, double priceL)
		{
			this.priceS=priceS;
			this.priceM=priceM;
			this.priceL=priceL;
			currentPrice=priceS;
			
			setLayout(new BorderLayout());
			setBackground(Color.white);
			
		   label = new JLabel(icon);
			add(label,BorderLayout.NORTH);
	
			panel = new JPanel();
			panel.setLayout(new GridLayout(3,2));
			
			////////////////////////////////////////////////////////
			///Top of grid layout for panel
			////////////////////////////////////////////////////////
			label5 = new JLabel("Quantity",SwingConstants.CENTER);
			label5.setBorder(BorderFactory.createLineBorder(Color.black,1));
			label6 = new JLabel("Size",SwingConstants.CENTER);
			label6.setBorder(BorderFactory.createLineBorder(Color.black,1));
			panel.add(label5);
			panel.add(label6);
		   ////////////////////////////////////////////////////////
			///Middle of grid layout for panel
			////////////////////////////////////////////////////////
			panel2 = new JPanel();
			panel2.setLayout(new GridLayout(1,2));
			b1 = new JButton("Up");
			b1.addActionListener( new ButtonListener());
			b2 = new JButton("Down");
			b2.addActionListener( new ButtonListener());
			panel2.add(b1);
			panel2.add(b2);
			panel.add(panel2);
			panel3 = new JPanel();
			panel3.setLayout(new GridLayout(1,2));
			b3 = new JButton("Up");
			b3.addActionListener( new ButtonListener());
			b4 = new JButton("Down");
			b4.addActionListener( new ButtonListener());
			panel3.add(b3);
			panel3.add(b4);
			panel.add(panel3);
		  	////////////////////////////////////////////////////////
			///Bottom of grid layout for panel
			////////////////////////////////////////////////////////
			label3 = new JLabel("" + qCount,SwingConstants.CENTER);
			panel.add(label3);
			label4 = new JLabel("small",SwingConstants.CENTER);
			panel.add(label4);
		
			add(panel,BorderLayout.CENTER);
			label2= new JLabel("Price: " + orderTotal,SwingConstants.CENTER);
			add(label2,BorderLayout.SOUTH);
	}
		
		private class ButtonListener implements ActionListener
		{

				public void actionPerformed(ActionEvent event){
					
					if(event.getSource() == b1){
						qCount++;
						orderTotal=qCount*currentPrice;
						label2.setText("Price: "+ orderTotal);
						label3.setText(""+qCount);
					}else if(event.getSource() ==b2){
						qCount--;
						if(qCount<=0)
							qCount=0;
						orderTotal=qCount*currentPrice;
						label2.setText("Price: "+ orderTotal);
						label3.setText(""+qCount);
					}else if(event.getSource() ==b3){
						sCount++;
						if(sCount>3)
							sCount=3;
						switch(sCount){
							case 1:
							label4.setText("small");
							currentPrice=priceS;
							break;
							case 2:
							label4.setText("medium");
							currentPrice=priceM;
							break;
							case 3:
							label4.setText("large");
							currentPrice=priceL;
							break;
						}
						orderTotal=qCount*currentPrice;					//Makes Price update when switching to 
						label2.setText("Price: "+ orderTotal);       //different sizes 
					}else if(event.getSource() ==b4){
						sCount--;
						if(sCount<1)
							sCount=1;
						switch(sCount){
							case 1:
							label4.setText("small");
							currentPrice=priceS;
							break;
							case 2:
							label4.setText("medium");
							currentPrice=priceM;
							break;
							case 3:
							label4.setText("large");
							currentPrice=priceL;
							break;
						}
						orderTotal=qCount*currentPrice;					//Makes Price update when switching to 
						label2.setText("Price: "+ orderTotal);       //different sizes 
					
					}
			}
	}
}

To give you a full idea of what i'm trying to do, let me explain the project. I'm trying to get the total of all orderItems depending on the quantity and size of there plate. This works perfect. The problem im stuck at is getting all of those individual totals to combine. I need the overall total(All order items totals put together) to be updated with each press of an orderItem's buttons(b1,b2,b3,b4). Hope that helps you help me and in turn help someone else haha. Thanks in advance!

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by BestJewSinceJC
0

You have several tests in your actionPerformed method; this implies that you should probably have a separate ActionListener for each button. For example, since b1 is 'Up' and b2 is 'Down' you should probably have separate listeners for those. Otherwise, it is pointless to use an inner class and you might as well just have OrderItem implements ActionListener.

As for the question you asked, you can access the variable of the outer class by using [from the inner class] OuterClass.this.varName

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.