Get value from 2D array

Posted 08 March 2013 - 10:47 AM

Hello everyone, I have made a 2D grid of JLabels. Every jlabel, the lightblue square on the 2D grid have the [0][0] position, is it possible to get a string back for example if i click the lightblue button i get a string back "0 0"?

Replies To: Get value from 2D array

Re: Get value from 2D array

Posted 08 March 2013 - 11:02 AM

Matthieu105, on 08 March 2013 - 10:47 AM, said:

Hello everyone, I have made a 2D grid of JLabels. Every jlabel, the lightblue square on the 2D grid have the [0][0] position, is it possible to get a string back for example if i click the lightblue button i get a string back "0 0"?

Re: Get value from 2D array

Posted 08 March 2013 - 02:36 PM

Wanna share?

The answer I would offer, if you had some code, would be to extend JLabel to include row and column information. Alternately, you could implement a separate actionlistener instance for each label with the data you want to pass already bound.

Re: Get value from 2D array

Posted 08 March 2013 - 07:46 PM

pbl. So you have a 2d array. Instead of creating god knows how many listeners, inside your mousePressed listener you simply use a nested for loop that cycles through the 2d array calling the JLabel's contains(mousex, mouseY). If contains() returns true, return the position in the array.

Re: Get value from 2D array

Maybe. I am a terrible programmer, but I would definitely not create 100 different action listeners for a 10x10 jlabel array. It just seems strange to me.

Why ?
You live in a State where you pay taxes based on the number of Listener created ?

Would be strange to be to create a single listener on a whole panel and try to identify in it, based on the X,Y clicked coordinates within the panel in which internal JComponent the click actually happened

Re: Get value from 2D array

Maybe. I am a terrible programmer, but I would definitely not create 100 different action listeners for a 10x10 jlabel array. It just seems strange to me.

Why ?

It just seems clunky to me to have the toolkit thread constantly polling 100 different action listeners x timer per second, then when something happens the toolkit thread passes it to the event dispatch thread, then the edt calls the listener which then figures out which button then returns the data, when I could only do a search for the proper button when I need to upon mouse click.

Re: Get value from 2D array

Posted 09 March 2013 - 04:12 AM

Not sure what you two are on about. Not entirely sure you're on the same page.

We're talking about JButtons here. The nice thing about a JButton, as opposed to a basic JComponent, is that it's already wired up to tell you when it's clicked. No MouseListener required. You only really need a mouse listener if you're treating your control as a canvas rather than a widget container.

Re: Get value from 2D array

Posted 09 March 2013 - 06:46 PM

baavgai, on 09 March 2013 - 06:12 AM, said:

Not sure what you two are on about. Not entirely sure you're on the same page.

We're talking about JButtons here. The nice thing about a JButton, as opposed to a basic JComponent, is that it's already wired up to tell you when it's clicked. No MouseListener required. You only really need a mouse listener if you're treating your control as a canvas rather than a widget container.

No they aren't JButton they are JLabel used as Buttons

I say put a MouseListener on each JLabel pass the JLabel or X,Y to the constructor of the Listener so will know which JLabel was clicqued

farrell2k claims that will create too many Listener. Just create one on the whole panel and when the mouse is clicked, determine by calculations based on JPanel size() ovr which JPanel the mouse was when cliked

Actually if the user used JButton he would have to put an ActionListener over each JButton

Re: Get value from 2D array

Posted 09 March 2013 - 08:03 PM

Ah, JLables... why the hell would you use JLables if you wanted to click on the bloody thing? The code I offered assumes buttons. Yes, with a JLabel you essentially reinvent the JButton and use MouseListener instead.

Doing one giant panel looses the benefit of having components at all. This makes sense in a game, where elements are graphics, but not so much in an application where standard controls will do the job for you.