Results 1 to 19 of 19
  1. #1
    h3nch is offline Member
    Join Date
    Jan 2010
    Posts
    9
    Rep Power
    0

    Default how do i see the JButton that called actionPerformed?

    so I want to get the specific button which called an actionPerformed method in order to make changes to the button once it has been pressed. The reason for this is I have a grid of buttons and I want to change the appearance of the last one pressed. I have used the getSource() method which kind of works but it gives me all of the previous buttons that have been pressed which is not what I want. please help me find a solution

  2. #2
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    Quote Originally Posted by h3nch View Post
    The reason for this is I have a grid of buttons and I want to change the appearance of the last one pressed. I have used the getSource() method which kind of works but it gives me all of the previous buttons that have been pressed which is not what I want.
    What do you mean? Can you provide sscce
    "There is no foolproof thing; fools are too smart."
    "Why can't you solve my Problem ?"

  3. #3
    h3nch is offline Member
    Join Date
    Jan 2010
    Posts
    9
    Rep Power
    0

    Default

    its a little difficult to give a good sscce, but heres some of the vital sections of my code:

    SmallSquare is a class that extends JButton, when it is pressed the color of it changes. butns is a 2d array which holds all the instances of SmallSquare
    Java Code:
                    for(int i=y-1; i>=0; i--)
            	{
               		for(int j=0; j<x; j++)
               		{
               			SmallSquare square = new SmallSquare(j,i);
                   			butns[i][j] = square;
                   			square.addActionListener(new SquareListen());
    				panel.add(square);
               		}
            	}
    this is where i am retrieving the source of the button press (contained within the SquareListen listener)
    Java Code:
    final SmallSquare pressedSq = (SmallSquare) event.getSource();
    Last edited by h3nch; 01-22-2010 at 12:59 AM.

  4. #4
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    getSource() method which kind of works but it gives me all of the previous buttons that have been pressed
    This cannot be what is happening. getSource() returns one object. Only one.

    Do you set the consumed value in the event?

    Java Code:
    square.addActionListener(new SquareListen());
    As written, each square is getting its own event handler.
    So that handler will never see events from any other square.
    So calling getSource() is pointless. It is usually sufficient
    to have only one handler. In it getSource() will be useful.

  5. #5
    h3nch is offline Member
    Join Date
    Jan 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by zweibieren View Post
    Do you set the consumed value in the event?
    What do you mean by this?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm guessing he's asking if you happen to change the source in the action event object, but I'm not sure. Regardless, you need to show us how your code is not working at present as most of us see no reason why it shouldn't.

  7. #7
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    i don't know why i'm doing this...

    you cannot show effort to provide a simple sscce, why should we then try ...
    but, today's my birthday and you are a lucky winner

    Java Code:
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class JButtons {
    
    	public static ActionListener listener = new ActionListener() {
    
    		@Override
    		public void actionPerformed(ActionEvent e) {
    			JButton source = (JButton) e.getSource();
    			System.out.println(source.getText());
    		}
    	};
    
    	public static void main(String[] args) {
    
    		JFrame frame = new JFrame("HTML Example");
    
    		JPanel panel = new JPanel(new GridLayout(10, 10));
    
    		JButton[][] buttonArray = new JButton[10][10];
    
    		for (int i = 0; i < buttonArray.length; i++) {
    			for (int j = 0; j < buttonArray[i].length; j++) {
    				JButton button = new JButton(i + "/" + j);
    				button.addActionListener(listener);
    				buttonArray[i][j] = button;
    				panel.add(button);
    			}
    		}
    
    		frame.add(panel);
    		frame.setSize(600, 400);
    
    		frame.setVisible(true);
    	}
    
    }
    i didnt comment either...
    so please, next time, give it a try and create a conceptual idea and code of what you are tring to achieve
    "There is no foolproof thing; fools are too smart."
    "Why can't you solve my Problem ?"

  8. #8
    h3nch is offline Member
    Join Date
    Jan 2010
    Posts
    9
    Rep Power
    0

    Default

    Well I know this probably won't help at all and is a pretty long and tedious thing to read but here is the source code:

    Java Code:
    <Pointless wall of code that I shouldn't have put up>
    Last edited by h3nch; 01-22-2010 at 07:45 PM.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Please read the link on creating an SSCCE. It will help you 1) create a bit of code that is useful for us to look at and 2) force you to solve your problem in a small simple program (which is much easier to do) then a big complex one. The latter is something you should be doing regardless of whether you post questions to the forum or not.

    Much luck.

  10. #10
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    you can try
    Java Code:
    square.addActionListener(this);
    WALL OF CODE ! YAY !
    "There is no foolproof thing; fools are too smart."
    "Why can't you solve my Problem ?"

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,508
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by AndreB View Post
    but, today's my birthday
    Congratulations; have a great day.

    kind regards,

    Jos

  12. #12
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    thanks :-)
    "There is no foolproof thing; fools are too smart."
    "Why can't you solve my Problem ?"

  13. #13
    h3nch is offline Member
    Join Date
    Jan 2010
    Posts
    9
    Rep Power
    0

    Default

    Ok after alot of thinking and frustration I have been able to pinpoint my problem (but not solve it). What I want to do is select a square from the grid by clicking on a button and then when I press a separate button, it should then allow me to take values from the square that was pressed but for some reason values from all previously clicked on squares are given.

    This time I created a sscce :D
    Java Code:
    
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    class ClickBtnListener implements ActionListener
    {
    	private JButton source;
    	
    	public ClickBtnListener(JButton src)
    	{
    		source = src;
    	}
    
    	public void actionPerformed(ActionEvent e)
    	{
    		System.out.println("button clicked: " + source.getText());
    	}
    }
    
    class SquareListener implements ActionListener
    {
    	private ShortExample sExample;
    
    	public SquareListener(ShortExample shortE)
    	{
    		sExample = shortE;
    	}
    
    	public void actionPerformed(ActionEvent e)
    	{
    			JButton source = (JButton) e.getSource();
    			System.out.println("grid clicked: "+ source.getText());
    			
    			JButton btn = sExample.addBtn;
    			btn.addActionListener(new ClickBtnListener(source));
    	}
    }
    
    public class ShortExample {
    
    	protected JButton addBtn;
    
    	public ShortExample()
    	{
    		JFrame frame = new JFrame("Short Example");
    		JPanel panel = new JPanel(new GridLayout(10, 10));
    		JButton[][] buttonArray = new JButton[10][10];
    		
    		addBtn = new JButton("Click me");
    		
    		for (int i=0; i<buttonArray.length; i++)
    		{
    			for (int j=0; j<buttonArray[i].length; j++)
    			{
    				JButton button = new JButton(i +"/"+ j);
    				button.addActionListener(new SquareListener(this));
    				buttonArray[i][j] = button;
    				panel.add(button);
    			}
    		}
    
    		frame.add(panel, BorderLayout.CENTER);
    		frame.add(addBtn, BorderLayout.SOUTH);
    		frame.setSize(600, 400);
    
    		frame.setVisible(true);
    	}
    	
    	public static void main(String[] args)
    	{
    		ShortExample s = new ShortExample();
    	}
    }
    In this example try press a button in the grid and then the click me button afterwards and do this more than once to see the problem show up in the terminal/command prompt

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Every time a square button is pressed it adds a new ClickButtonListener to the addBtn. So after 10 presses of these square buttons, that addbtn is bloated with 10 separate listeners. Then when you press the addbtn, all of those listeners will fire.

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    So don't add a new listener each time the square button has been pressed. Instead change the state of the program and then have the addBtn query the program's state. For a very basic example...
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    public class ShortExample2 {
    
      private JButton addBtn;
      private int lastI = -1;
      private int lastJ = -1;
    
      public ShortExample2() {
        JFrame frame = new JFrame("Short Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel(new GridLayout(10, 10));
        JButton[][] buttonArray = new JButton[10][10];
    
        addBtn = new JButton("Click me");
        addBtn.addActionListener(new ClickBtnListener2(this));
        
        SquareListener2 sqListener2 = new SquareListener2(this);
    
        for (int i = 0; i < buttonArray.length; i++) {
          for (int j = 0; j < buttonArray[i].length; j++) {
            JButton button = new JButton(i + "/" + j);
            button.addActionListener(sqListener2);
            buttonArray[i][j] = button;
            panel.add(button);
          }
        }
    
        frame.add(panel, BorderLayout.CENTER);
        frame.add(addBtn, BorderLayout.SOUTH);
        frame.setSize(600, 400);
    
        frame.setVisible(true);
      }
      
      public void setLastI(int i) {
        this.lastI = i;
      }
      
      public void setLastJ(int j) {
        this.lastJ = j;
      }
      
      public int getLastI() {
        return lastI;
      }
      
      public int getLastJ() {
        return lastJ;
      }
    
      public static void main(String[] args) {
        ShortExample2 s = new ShortExample2();
      }
    }
    
    class ClickBtnListener2 implements ActionListener {
      private ShortExample2 sExample;
    
      public ClickBtnListener2(ShortExample2 shEx2) {
        this.sExample = shEx2;
      }
    
      public void actionPerformed(ActionEvent e) {
        System.out.println("button clicked: [" + sExample.getLastI() + ", " + sExample.getLastJ() + "]"); 
      }
    }
    
    class SquareListener2 implements ActionListener {
      private ShortExample2 sExample;
    
      public SquareListener2(ShortExample2 shortE) {
        sExample = shortE;
      }
    
      public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        System.out.println("grid clicked: " + command);
    
        String[] tokens = command.split("/");
        sExample.setLastI(Integer.parseInt(tokens[0]));
        sExample.setLastJ(Integer.parseInt(tokens[1]));
      }
    }

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,508
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    Every time a square button is pressed it adds a new ClickButtonListener to the addBtn. So after 10 presses of these square buttons, that addbtn is bloated with 10 separate listeners. Then when you press the addbtn, all of those listeners will fire.
    Erm, normally Listeners don't fire anything; they are the target of the firing ;-) But true: adding more and more of them is obviously a bug.

    kind regards,

    Jos

  17. #17
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    wow :-) now thats what i'm talking about! A simple example showing the problem.

    like jos and fubarable said, you have a misunderstanding for Observer Pattern

    But, nevertheless why this doesnt help?
    "There is no foolproof thing; fools are too smart."
    "Why can't you solve my Problem ?"

  18. #18
    h3nch is offline Member
    Join Date
    Jan 2010
    Posts
    9
    Rep Power
    0

    Default

    Thanks guys for all the help, I've realised what the problem is and messed around with ShortExample.java while looking at Fubarable's working version. Next time I have a problem I'll try to create a shorter version of it first before posting anything but for now im off to fix the big project.

  19. #19
    12_incher is offline Member
    Join Date
    Jan 2010
    Posts
    1
    Rep Power
    0

    Default

    Quote Originally Posted by h3nch View Post
    Thanks guys for all the help, I've realised what the problem is and messed around with ShortExample.java while looking at Fubarable's working version. Next time I have a problem I'll try to create a shorter version of it first before posting anything but for now im off to fix the big project.
    Just don't put a listener class within a listener class. That way you can have a 12 inch you know what just like me.

Similar Threads

  1. Help with actionPerformed
    By mayhewj7 in forum New To Java
    Replies: 8
    Last Post: 02-10-2009, 06:45 PM
  2. trouble with actionPerformed
    By diggitydoggz in forum New To Java
    Replies: 2
    Last Post: 12-26-2008, 02:18 AM
  3. Help with actionPerformed Statements
    By wco5002 in forum New To Java
    Replies: 8
    Last Post: 03-26-2008, 04:02 AM
  4. actionPerformed problem
    By tomitzel in forum New To Java
    Replies: 1
    Last Post: 01-08-2008, 06:10 PM
  5. Problems with jButton ActionPerformed
    By susan in forum AWT / Swing
    Replies: 3
    Last Post: 08-07-2007, 04:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •