Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: ActionListener changing ImageIcons:

  1. #1
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default ActionListener changing ImageIcons:

    If you look at line 74 you will see my actionPerformed method where I want to switch the icon displayed on the JButton. I know I still have to add the images to the button however I am running into a few issues here.

    Therefore my main question is, am I even approaching this problem correctly? In my next class it will be vital that I can decipher what each icon is display on the JButton. I am working on programing A* but I have to get everything talking first.

    Please take a look at line 74, any advice would be greatly appreciated.

    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.*;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
     
    public class Screen implements ActionListener
    {
    	JButton start;
    	JButton reset;
    	JButton box[][] = new JButton[20][20];
    	JLabel lbl;
    	//Icons to be displayed in box[][]
    	ImageIcon wall;
    	ImageIcon begin;
    	ImageIcon end;
     
    	//counter how many time box[][] was pressed to enable
    	//the Image Icons
    	private int counter=0;
     
    	Screen()
    	{
    		//main frame
    		JFrame j = new JFrame("A* Algorithm");
     
    		//setting main frame
    		j.setSize(1200,500);
    		j.setLayout(new BorderLayout());
    		j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		//create Panel for multi array buttons
    		JPanel p = new JPanel();
    		p.setLayout(new GridLayout(20,20));
     
    		//create command buttons "start" and "restart"
    		JPanel CmdBtns = new JPanel();
     
    		//create buttons matrix of buttons
    		for(int row=0;row<20;row++)
    		{
    			for(int col=0;col<20;col++)
    			{   //create new matrix buttons
    				box[row][col] = new JButton();
    				//add buttons to panel
    				p.add(box[row][col]);
    			}
    		}
     
    		//create start button
    		start = new JButton("Start");
    		CmdBtns.add(start);
     
    		//create reset button
    		reset = new JButton("Reset");
    		CmdBtns.add(reset);
     
    		//Centering matrix buttons onto main JFrame
    		j.add(p,BorderLayout.CENTER);
     
    		//adding start and restart buttons
    		j.add(CmdBtns,BorderLayout.SOUTH);
     
    		j.setVisible(true);
    	}
     
    	//handling matrix buttons
    	public void actionPerformed(ActionEvent ae)
    	{
    		//counting how many times button the the matrix was pressed
    		counter++;
    		//reseting counter to zero if the buttons was pressed three times
    		counter%=3;
     
    		if(ae.getActionCommand().equals(box))
    		{
    			switch(counter)
    			{
    			case 0:
    				//display wall icon
    				break;
    			case 1:
    				//display start icon
    				break;
    			case 2:
    				//display end icon
    				break;
     
    				default:
    					break;
     
    			}
    		}
    	}
     
    	public static void main(String[] args)
    	{
    		new Screen();
    	}
     
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: ActionListener changing ImageIcons:

    am I even approaching this problem correctly?
    Other than running the Swing app on the EDT, your approach seems okay, but it's not clear what the problem is, so a definite answer is not possible. What are the 400 buttons to do?

    Some fine points at this "work in progress" stage:

    1. The actionListener has not been added to any of the buttons.

    2. The if statement condition:

    (ae.getActionCommand().equals(box)

    will not return true for individual button presses.

    3. It's not clear if the counter is intended to count the number of presses on each button or on any button. Currently, it counts a press on any button, or, more accurately, on any entry into the actionPerformed() method.

    Thanks for commenting your code.

  3. #3
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: ActionListener changing ImageIcons:

    The end statement of this program is to program A Star. When the buttons in the matrix is pressed different options will display to what it is . If it is pressed once the program will read it as a wall and know it cannot go this way. The other options will let the user pick the start and end points.

    The counter is suppose to cycle through these options and if the button was not pressed it is a approved path to be checked on the open list.

    I have also attached a new program. Which way to you think is better?

    Class Screen
    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.*;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
     
    public class Screen
    {
    	JButton start;
    	JButton reset;
    	MatrixButtons[][] matrixButtons = new MatrixButtons[20][20];
     
    	Screen()
    	{
    		//main frame
    		JFrame j = new JFrame("A* Algorithm");
     
    		//setting main frame
    		j.setSize(1200,500);
    		j.setLayout(new BorderLayout());
    		j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		//create Panel for multi array buttons
    		JPanel p = new JPanel();
    		p.setLayout(new GridLayout(20,20));
     
    		//create command buttons "start" and "restart"
    		JPanel CmdBtns = new JPanel();
     
    		//create buttons matrix of buttons
    		for(int row=0;row<20;row++)
    		{
    			for(int col=0;col<20;col++)
    			{   //create new matrix buttons
    				matrixButtons[row][col] = new MatrixButtons();
    				//add buttons to panel
    				p.add(matrixButtons[row][col]); //error on line
    			}
    		}
     
    		//create start button
    		start = new JButton("Start");
    		CmdBtns.add(start);
     
    		//create reset button
    		reset = new JButton("Reset");
    		CmdBtns.add(reset);
     
    		//Centering matrix buttons onto main JFrame
    		j.add(p,BorderLayout.CENTER);
     
    		//adding start and restart buttons
    		j.add(CmdBtns,BorderLayout.SOUTH);
     
    		j.setVisible(true);
    	}
     
    	public static void main(String[] args)
    	{
    		new Screen();
    	}
     
    }

    Class MatrixButtons

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.ImageIcon;
     
    public class MatrixButtons extends Screen implements ActionListener 
    {
    	//Icons to be displayed in box[][]
    		ImageIcon wall;
    		ImageIcon begin;
    		ImageIcon end;
     
    		byte value =0;
    		/*
    		 0:nothing
    		 1:wall
    		 2:begin
    		 3:end
    		 */
     
    		MatrixButtons()
    		{
    			wall = new ImageIcon(this.getClass().getResource("wall.png"));
    			begin = new ImageIcon(this.getClass().getResource("begin.png"));
    			end = new ImageIcon(this.getClass().getResource("end.png"));
    			matrixButtons.addActionListener(new ActionListener());  //error on line
    		}
     
    		public void actionPerformed(ActionEvent ae) 
    		{
    			value++;
    			value%=3;
     
    			switch(value)
    			{
    			case 0:
    				setIcon(wall);   //error on line
    				break;
    			case 1:
    				setIcon(begin); //error on line
    				break;
    			case 2:
    				setIcon(end);   //error on line
    				break;
     
    				default:
    					break;
    			}
     
    		}
     
    }

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: ActionListener changing ImageIcons:

    I'm not quite getting it, but I think what you've described will require keeping track of how many times each button is pressed. If that's the case, you may want each matrix button to extend JButton so that you can add the press counter to the matrix button class.
    I have also attached a new program. Which way to you think is better?
    I won't compare each line of 2 different posts to determine the differences and then give an opinion as to which is better. The real test for which is better is to determine which one most accurately accomplishes what you're trying to do while providing the ability to grow easily into what you need it to do next.

    Since this is a work in progress with unclear requirements, I can't make those judgments.

  5. #5
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: ActionListener changing ImageIcons:

    Yes I want to keep track of how many times each button is pressed and reset each button every third time it is pressed.

    To start off it is set to null
    press once it is set to wall
    pressed twice set to begin
    pressed three times, set to end
    pressed fourth time it is back to null and the process repeats for each button.

    The above buttons will be used to find a path, if it is null it will be check to see if it is one of the nodes on the shortest path, walls will not be checked and the program will start from the “start” icon and end when it find the “end” icon.

    I posted this question in another forum and seen someone mentioned something about map<JButton,Integer>. However I was thinking this might be a little over kill, opinion? I am more familiar with C++ and trying to learn swing and get better at java. Thought this would be a fun challenging project and I appreciate the advice.

    A* Algorithm with Python-Pygame - YouTube
    This above link is something like what I am trying to do. I will eventually add a few more path find algorithms to the options but one step at a time.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: ActionListener changing ImageIcons:

    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: ActionListener changing ImageIcons:

    Yes norm I posted at a different forum too. I do not understand? Do you think I am trying to play or mislead posters on here? Is there some ethic I am unaware of against asking the question in two different places? I even posted on that forum that I posted on here? I really appreciate the advice, didn't mean to rub you the wrong way.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: ActionListener changing ImageIcons:

    It's ok to post on other forums. However, we appreciate it if you post a link to them.
    See: http://www.javaprogrammingforums.com...s-posting.html
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Re: ActionListener help
    By JoeB in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2013, 01:20 AM
  2. ActionListener inside another ActionListener
    By kpat in forum AWT / Java Swing
    Replies: 6
    Last Post: March 28th, 2012, 03:43 PM
  3. Help with ActionListener please
    By knightmetal in forum AWT / Java Swing
    Replies: 3
    Last Post: August 23rd, 2011, 05:41 PM
  4. ActionListener help
    By hello_world in forum What's Wrong With My Code?
    Replies: 10
    Last Post: July 27th, 2011, 11:45 PM
  5. [SOLVED] ActionListener help
    By kbwalker87 in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 14th, 2010, 06:57 PM