ImageIcon tenP1 = new ImageIcon("10p.gif");
    	tenP = new JButton("", tenP1);
    	ImageIcon twentyP1 = new ImageIcon("20p.gif");
    	twentyP = new JButton("", twentyP1);
    	ImageIcon fiftyP1 = new ImageIcon("50p.gif");
    	fiftyP = new JButton("", fiftyP1);
    	ImageIcon pound = new ImageIcon("pound.gif");
    	onePound = new JButton("", pound);
    	ImageIcon twopound = new ImageIcon("twopound.gif");
    	twoPound = new JButton("", twopound);

for above set of buttons can someone please show me how to do a single action listener that would add that specific amount to "Amount Insert" and decrease that specific amount to "Amount Due" please :)

so something like this but how do one single action listerer? can i do switch statement. please show me using code?

twentyP.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e) {
						amountIn = amountIn + 0.20;
						amInseted.setText("Amount Instered: \u00A3" + amountIn);
					}});

It is possible to add the same action listener as a listener for more than one button.

Create an inner class that implements the ActionListener interface, create an instance of it and add it as listener to the buttons.

Agree with NormR1!
Also, doing this will get the same result:
Create a class that implements ActionListener interface, we will override the method actionPerformed(evt), inside the actionPerformed method, we use evt.getSource() and compare it to several buttons.

---
Songokute

... but even better:
create class as above. Add a variable for amount, that can be set by a parameter in the constructor. When adding instances of this class to each button pass the appropriate value into the constructor.
Now there's no need for getSource or a switch or any of that stuff; just add/subtract the amount (same code for all buttons, just the amount varies)

twentyP.addActionListener(new myListener(0.20));

class myListener implements ActionListener [
   ...
   public myListener(float amount) {
     this.amount = amount;
     ...
   public void actionPerformed(ActionEvent e) {
     amountIn = amountIn + amount;

...

thanks guys :)

... but even better:
create class as above. Add a variable for amount, that can be set by a parameter in the constructor. When adding instances of this class to each button pass the appropriate value into the constructor.
Now there's no need for getSource or a switch or any of that stuff; just add/subtract the amount (same code for all buttons, just the amount varies)

twentyP.addActionListener(new myListener(0.20));

class myListener implements ActionListener [
   ...
   public myListener(float amount) {
     this.amount = amount;
     ...
   public void actionPerformed(ActionEvent e) {
     amountIn = amountIn + amount;

...

public class myListener implements ActionListener {
    		public myListener (float amount) {
    			this.amount = amount;
    		}
			public void actionPerformed(ActionEvent e) {
				amountInserted = amountInserted + amount;
				balance = balance - amount;
				lInserted.setText("Amount Inserted: " + amountInserted);
				lDue.setText("Amount Due: " + balance);
				checkBalanceisZero(balance);
			}};

giving me one slight error?

PaymentNewGUI.java:100: illegal start of expression
        public class myListener implements ActionListener {
        ^
1 error

Looks like you inserted that code in the wrong place (OR there's an error on the immediately preceding line). That code should go inside the class, but outside any methods.
ps: Don't forget the declare float amount; in the myListener class

Looks like you inserted that code in the wrong place (OR there's an error on the immediately preceding line). That code should go inside the class, but outside any methods.
ps: Don't forget the declare float amount; in the myListener class

james i m confused could you perhaps correct me? i think i may have not done the braces proper or its a method inside a method giving error?

That's a class definition, not a method. It should be inside the class where you have the addActionListener calls, but NOT inside any method. Don't declare it public. You just need to close all the {} properly

class myListener implements ActionListener {
    float amount;
    public myListener (float amount) {
       this.amount = amount;
    }
    public void actionPerformed(ActionEvent e) {
       amountInserted = amountInserted + amount;
       balance = balance - amount;
       lInserted.setText("Amount Inserted: " + amountInserted);
       lDue.setText("Amount Due: " + balance);
       checkBalanceisZero(balance);
    } // closes actionPerformed
} // closes class myListener

That's a class definition, not a method. It should be inside the class where you have the addActionListener calls, but NOT inside any method. Don't declare it public. You just need to close all the {} properly

class myListener implements ActionListener {
    float amount;
    public myListener (float amount) {
       this.amount = amount;
    }
    public void actionPerformed(ActionEvent e) {
       amountInserted = amountInserted + amount;
       balance = balance - amount;
       lInserted.setText("Amount Inserted: " + amountInserted);
       lDue.setText("Amount Due: " + balance);
       checkBalanceisZero(balance);
    } // closes actionPerformed
} // closes class myListener
ImageIcon tenP1 = new ImageIcon("10p.gif");
    	tenP = new JButton("", tenP1);
    	tenP.setEnabled(false);
    	tenP.addActionListener(new myListener(0.10));
			
    	ImageIcon twentyP1 = new ImageIcon("20p.gif");
    	twentyP = new JButton("", twentyP1);
    	twentyP.setEnabled(false);
    	twentyP.addActionListener(new myListener(0.20));
    	
    	ImageIcon fiftyP1 = new ImageIcon("50p.gif");
    	fiftyP = new JButton("", fiftyP1);
    	fiftyP.setEnabled(false);
    	fiftyP.addActionListener(new myListener(0.50));
    	
    	ImageIcon pound = new ImageIcon("pound.gif");
    	onePound = new JButton("", pound);
    	onePound.setEnabled(false);
    	onePound.addActionListener(new myListener(1.00));
    	ImageIcon twopound = new ImageIcon("twopound.gif");
    	twoPound = new JButton("", twopound);
    	twoPound.setEnabled(false);
    	twoPound.addActionListener(new myListener(2.00));
    	
    	public class myListener implements ActionListener {
    		double amount;
    	    public myListener (double amount) {
    	       this.amount = amount;
    	    }
			public void actionPerformed(ActionEvent e) {
				amountInserted = amountInserted + amount;
				balance = balance - amount;
				lInserted.setText("Amount Inserted: " + amountInserted);
				lDue.setText("Amount Due: " + balance);
				checkBalanceisZero(balance);
			}}

Still the same errors and ive done exactly how u did it only i am using double since all my values of amount are double which isnt the problems, the problems is the listener :(

That's INSIDE a method. YOU must put the class definition OUTSIDE any method definitions.

That's INSIDE a method. YOU must put the class definition OUTSIDE any method definitions.

haha finally thank you got you :$

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.