Results 1 to 9 of 9
Thread: MouseListener from Array
- 07-09-2012, 06:05 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 31
- Rep Power
- 0
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];
Java Code:HandleClicks handleClicks = new HandleClicks();
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); }}
So, the HandleClicks class is this.
Java Code:private class HandleClicks extends MouseAdapter{ public void mousePressed(MouseEvent e) { if (e.isMetaDown()) // TODO ; } }
Java Code:bomb[row][column].setIcon(bombIcon);
- 07-09-2012, 06:44 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
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 **
- 07-09-2012, 07:22 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 31
- Rep Power
- 0
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.
-
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:
- How to Use Buttons, Check Boxes, and Radio Buttons
- How to Write an Action Listener
- How to Use CardLayout
Also, a while back, I created code for this and have posted it here. I'm "hovercraft" on that forum.
- 07-09-2012, 11:03 PM #5
Member
- Join Date
- Mar 2012
- Posts
- 31
- Rep Power
- 0
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.
-
- 07-10-2012, 12:42 AM #7
Member
- Join Date
- Mar 2012
- Posts
- 31
- Rep Power
- 0
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.
- 07-10-2012, 07:28 AM #8
Member
- Join Date
- Mar 2012
- Posts
- 31
- Rep Power
- 0
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.
- 07-12-2012, 07:55 PM #9
Member
- Join Date
- Mar 2012
- Posts
- 31
- Rep Power
- 0
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
-
Help with mouselistener
By kastraki2000 in forum Java AppletsReplies: 2Last Post: 04-30-2011, 04:56 PM -
can't get x and y from mouselistener
By j2me64 in forum Java 2DReplies: 3Last Post: 04-24-2010, 05:57 PM -
i need help for MouseListener
By sfaxianovic in forum New To JavaReplies: 2Last Post: 08-21-2008, 04:30 AM -
MouseListener
By Aswq in forum New To JavaReplies: 12Last Post: 07-18-2008, 09:10 AM -
Null array when passed to MouseListener
By stevemcc in forum New To JavaReplies: 2Last Post: 04-02-2008, 11:42 PM
Bookmarks