Results 1 to 9 of 9
  1. #1
    Sutured is offline Member
    Join Date
    Mar 2012
    Posts
    30
    Rep Power
    0

    Default MouseListener from Array

    Hello.

    I am making MineSweeper and I have a problem concerning the MouseListener event handling.

    I have created a two-dimensional Array that holds all the JButtons.

    Java Code:
    private BombButton[][] bomb = new BombButton[17][31];
    I initialized the MouseListener.

    Java Code:
    HandleClicks handleClicks = new HandleClicks();
    And I created the Array table.

    Java Code:
    for (int row = 1 ; row <= 16 ; row++){
    			for (int column = 1 ; column <= 30 ; column++){
    				bomb[row][column] = new BombButton();
    				add(bomb[row][column]);
    				bomb[row][column].addMouseListener(handleClicks);
    			}}
    I added the MouseListener inside the for loop cause I wanted to add each button to the Listener while it is being created.

    So, the HandleClicks class is this.

    Java Code:
    private class HandleClicks extends MouseAdapter{
    		public void mousePressed(MouseEvent e) {
    			if (e.isMetaDown()) 
                   // TODO
    				;
    		}
    	}
    I want to insert in the //TODO field something like:

    Java Code:
    bomb[row][column].setIcon(bombIcon);
    How can I get which is the row and the column of the button that was pressed to set them in the setIcon?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default Re: MouseListener from Array

    Why MouseListener?
    They're buttons so wouldn't ActionListener make more sense?
    The ActionEVent from that contains the object the event occurred on, in your case the JButton, on which (after casting) you can setIcon().
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Sutured is offline Member
    Join Date
    Mar 2012
    Posts
    30
    Rep Power
    0

    Default Re: MouseListener from Array

    Even though I read the answer almost the moment you posted it, I still can't figure it out.

    When I find it, I'll post the results.

    Thank you Tolls.

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

    Default Re: MouseListener from Array

    I agree with Tolls: If this is a Swing application, use JButtons and add ActionListeners not MouseListeners to the JButtons. Also consider creating a grid of JPanels for you cells that each use CardLayout so that you can have the cell initially show a JButton and then swap out to a JLabel after the JButton has been pressed. To learn more about CardLayout, JButtons and ActionListeners, please check out the tutorials listed below:



    Also, a while back, I created code for this and have posted it here. I'm "hovercraft" on that forum.

  5. #5
    Sutured is offline Member
    Join Date
    Mar 2012
    Posts
    30
    Rep Power
    0

    Default Re: MouseListener from Array

    Yes, got it, finally.

    I read about casting and inheritance amongst others at API. So I fully understood the situation and then it was easy to understand what I had to do exactly, using your help of course.

    Thank you Tolls and Fubarable.

    The reason I use Mouse listener is that I want to have access to right click button event as well. I don't know if this exists with ActionListener but I couldn't find it.

    In my problem again, it seems that I cannot get the row and the column that the JButton exists into the Array so this doesn't solve other issues. Like to check if it has bomb. I haven't found how it can be done yet.

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

  7. #7
    Sutured is offline Member
    Join Date
    Mar 2012
    Posts
    30
    Rep Power
    0

    Default Re: MouseListener from Array

    This is the only thing I didn't see Fubarable from your links.

    I didn't see it on purpose because I didn't want to see Minesweeper in full implementation. I wanted to try it myself, like I successfully did with my previous Calculator. And I had in mind that whenever I finish it, I'd study it to check how better you programmed it. Hope you understand what I am saying because my English is not helping me to express it.

    But it seems that I will see it sooner, at least to get over this specific part that I am struck.

  8. #8
    Sutured is offline Member
    Join Date
    Mar 2012
    Posts
    30
    Rep Power
    0

    Default Re: MouseListener from Array

    I found a way. I used ClientProperty while creating each button, one for row and the other for column.

    I saw yours Fubarable and of course you handle it better than me. I am refactoring at the moment.

    Thank you again.

  9. #9
    Sutured is offline Member
    Join Date
    Mar 2012
    Posts
    30
    Rep Power
    0

    Default Re: MouseListener from Array

    Hello all again,

    I have just finished Minesweeper. I don't think it has bugs and it feels complete. I felt like posting it here cause you people help a lot.

    Download mirrors for Mine_Sweeper.jar - Mirrorcreator - Upload files to multiple hosts

    When you go to the page, download it from the bottom cells named 'Hosting site' and 'Download link', not the other random download buttons.

    I have included the code as well of course.

    If you care checking it, feel free to post any comments about anything. Possible mistakes I made, things I could do better or easier, structure archetype, whatever you think it needs(and me also) to be improved.

    My main priority was the MainPanelGUI class to handle everything concerning GUI and the model package to have all the calculations needed to being made.

    I started Java approx 4 months ago but the last 2 weeks and a half I am working on swing.

    Thank you.

Similar Threads

  1. Help with mouselistener
    By kastraki2000 in forum Java Applets
    Replies: 2
    Last Post: 04-30-2011, 03:56 PM
  2. can't get x and y from mouselistener
    By j2me64 in forum Java 2D
    Replies: 3
    Last Post: 04-24-2010, 04:57 PM
  3. i need help for MouseListener
    By sfaxianovic in forum New To Java
    Replies: 2
    Last Post: 08-21-2008, 03:30 AM
  4. MouseListener
    By Aswq in forum New To Java
    Replies: 12
    Last Post: 07-18-2008, 08:10 AM
  5. Null array when passed to MouseListener
    By stevemcc in forum New To Java
    Replies: 2
    Last Post: 04-02-2008, 10:42 PM

Posting Permissions

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