Results 1 to 8 of 8
  1. #1
    mad72584 is offline Member
    Join Date
    Aug 2011
    Location
    Bogotá
    Posts
    28
    Rep Power
    0

    Default How can I to add a mouse event to a button that it'll edit itself and other button

    Hi, I have this code:

    Java Code:
    for(init=0; init<visitante; init++)
                {
                for(inito=0; inito<12; inito++)
                    {
                    entradasVisitante[init][inito].addMouseListener(new MouseAdapter() 
                        {
                        int sumador = 0;    
                        public void mouseClicked(MouseEvent a) 
                            {
                            if (SwingUtilities.isLeftMouseButton(a))
                                {
                                JButton j = (JButton) a.getSource();
                                sumador++;
                                j.setText(sumador + "");
                                }
                            if (SwingUtilities.isRightMouseButton(a))
                                {
                                JButton j = (JButton) a.getSource();
                                if (sumador == 0)
                                    j.setText("0");
                                else 
                                    {
                                    sumador--;
                                    j.setText(sumador + "");
                                    }
                                }
                            }
                        }); 
                    }
                }
    But I want to add other event to this button, what is that does the same but now on other button...and a label.

    I don't gain to know how I can do it this.

    Thank you very much.

    PS: Excuse me my english

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

    Default

    You generally don't use MouseListeners with JButtons, but rather ActionListeners. Also, can you try to explain in greater detail what it is you are trying to do? Perhaps it's me, but I don't understand your current explanation.

    Que tenga suerte.

  3. #3
    mad72584 is offline Member
    Join Date
    Aug 2011
    Location
    Bogotá
    Posts
    28
    Rep Power
    0

    Default

    I am trying to do is this: I have an matrix of buttons, each button has a MouseListener that does is that if it has been clicked by right mouse button, its text increases by one, if it has been clicked by left button decreases one. That I have it. But now I wanna some buttons does the same and does it with its neighbor next button in the same row too, the problem I have is that the parameters of cycle "for", to location of the button, the mouseClicked does not get it. I used the getSource() to edit the same button, but to edit other one I do not know what I can do.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    but to edit other one I do not know what I can do
    Is this your problem: you have an array of button objects and when one of them is clicked, you want to find other buttons in the array that are near the button that was clicked.
    Couple of solutions:
    Search the array for the location of the button that was clicked.
    Use the JComponent's ClientProperties to save and get the button's position in the array.

  5. #5
    mad72584 is offline Member
    Join Date
    Aug 2011
    Location
    Bogotá
    Posts
    28
    Rep Power
    0

    Default

    Thanks, Can you help me showing an example? I'm using "getSource" to adjudge the event to the same button, but to find the location? I don't now it and the ClientProperties neither.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    Use getSource to get a reference to the button that was pressed. Then do a search in a list where there are references to all the buttons. When you find the index where the value from getSource is located, then you can use the index's value to get to other references.
    Say the button's reference is at location 10 in the list then location 9 and location 11 will be its neighbors in the list.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    The ClientProperty works like a Map. You get and put objects using the JComponent class's methods.
    You could have a key of "Location" and have a Point() as its value.

  8. #8
    mad72584 is offline Member
    Join Date
    Aug 2011
    Location
    Bogotá
    Posts
    28
    Rep Power
    0

    Lightbulb [solved]

    Thank you very much. You are a great help to me. Perfect!. Thanks Norm.
    Last edited by mad72584; 08-06-2011 at 07:36 PM.

Similar Threads

  1. Layout and button event problem! please help
    By counterfox in forum AWT / Swing
    Replies: 0
    Last Post: 01-28-2011, 12:48 AM
  2. button that runs away from the mouse cursor
    By MartinBenes71 in forum New To Java
    Replies: 1
    Last Post: 01-28-2010, 08:00 AM
  3. Show image on glasspane while mouse button is down
    By zweibieren in forum AWT / Swing
    Replies: 10
    Last Post: 09-12-2009, 06:29 AM
  4. button is not getting disabled on onload event
    By Harshada in forum Advanced Java
    Replies: 2
    Last Post: 03-08-2009, 02:03 PM
  5. Hovering mouse over a button and painting
    By gtraylo in forum AWT / Swing
    Replies: 2
    Last Post: 04-24-2008, 09:33 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
  •