Hi,

I have to make a program containing 52 Buttons having icons on them and have to attach action listener so that each button could function independently. Have used array in this program and have also been able to attach action listener with the array of buttons. Action Listener works fine as long as i try to print the value of the button and predefined text messages , but when i try to change the icon on the button to another icon by clicking on the button it simply do not work, for assistance i am also sending the coding of my program.

Regards

Asad

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


public class cardgame extends JFrame implements ActionListener{

	JMenuBar mb;
	JMenu file,help;
	JMenuItem newgame,exit,about;
	JButton b1[];
	JPanel p1;
	int z,i;
	String text;
	ImageIcon as[],icon[];
	
	public cardgame(){
		
		mb=new JMenuBar();
		setJMenuBar(mb);
		file=new JMenu("File");
		newgame=new JMenuItem("New Game");
		exit=new JMenuItem("Exit");
		mb.add(file);
		file.add(newgame);
		file.add(exit);
		exit.addActionListener(this);
		help=new JMenu("Help");
		about=new JMenuItem("About");
		mb.add(help);
		help.add(about);
		
		p1=new JPanel();
		ImageIcon as[]=new ImageIcon[52];
		JButton b1[]=new JButton[52];
		p1.setLayout(new GridLayout(4,2,5,5));
		for(int i=0;i<b1.length;i++){
		as[i]=new ImageIcon("D:/WorkSpace/images.gif");
		b1[i]=new JButton(Integer.toString(i+1),as[i]);
		b1[i].addActionListener(this);
		
		p1.add(b1[i]);
		}
		
		Container c1=getContentPane();
		c1.add(p1);
			
	}
	
	/**
	 * This method initializes this
	 * 
	 */
	
	public void actionPerformed(ActionEvent e){
		
		if(e.getSource().equals(exit)){
			System.exit(0);
		}
		
		String text=((JButton)e.getSource()).getText();
		
		 System.out.println("Value of text " + text);
		 
	   	    try{
	     z=Integer.parseInt(text);
	    if(z==1)
	    	
	    	System.out.println("Its Working");
	    	    
	    try{
	    ImageIcon icon[]=new ImageIcon[52];
	    JButton b1[]=new JButton[52];
	    //for(int i=0;i<b1.length;i++){
	    icon[i]=new ImageIcon("D:/WorkSpace/cards/as.gif");
	    //b1[i]= new JButton(Integer.toString(i+1),icon[i]);
	    b1[i]=new JButton(icon[i]); 
	    System.out.println("working");
	     	     
	    }catch(NullPointerException ex){
	    	System.out.println("Invalid use of null refrence");
	    }
	    
	    
	     if(z==2)System.out.println("Thats Good");
	}catch(NumberFormatException ex){
		System .out.println("Invalid Format");
	 }
	}
		
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		cardgame c=new cardgame();
		
		c.setTitle("Card Game");
		c.setSize(800, 700);
		c.setVisible(true);
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"

Lets take a look at your actionPerformed() method:-

public void actionPerformed(ActionEvent e){
		
		if(e.getSource().equals(exit)){
			System.exit(0);
		}
		
		String text=((JButton)e.getSource()).getText();
		
		 System.out.println("Value of text " + text);
		 
	   	    try{
	     z=Integer.parseInt(text);
	    if(z==1)
	    	
	    	System.out.println("Its Working");
	    	    
	    try{

            // YIKES, why are you creating your buttons all over again !!!
	    ImageIcon icon[]=new ImageIcon[52];
	    JButton b1[]=new JButton[52];
	    //for(int i=0;i<b1.length;i++){
	    icon[i]=new ImageIcon("D:/WorkSpace/cards/as.gif");
	    //b1[i]= new JButton(Integer.toString(i+1),icon[i]);
	    b1[i]=new JButton(icon[i]); 
	    System.out.println("working");
	     	     
	    }catch(NullPointerException ex){
	    	System.out.println("Invalid use of null refrence");
	    }
	    
	    
	     if(z==2)System.out.println("Thats Good");
	}catch(NumberFormatException ex){
		System .out.println("Invalid Format");
	 }
	}

In your try block why are you creating a new button but you just want to change the icon of an existing button, also that new button, should not be visible as you have not added it to your content pane "pl", you have just created it but done nothing with it.
Instead If you want the icon of the button to change on a click, use the setIcon() method of the AbstractButton class which JButton inherits.
The steps are pretty simple, inside your actionPerformed():-
- Use e.getSource() to get a reference to the clicked button (Which you already seem to know)
- Cast it to a JButton and call the setIcon() method on it.

Edited 6 Years Ago by stephen84s: n/a

This article has been dead for over six months. Start a new discussion instead.