Get value from 2D array

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 512 Views - Last Post: 09 March 2013 - 08:37 PM Rate Topic: -----

#1 Matthieu105  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 07-March 13

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"?

Image: http://www.imagebam....87e2b2242069231
Is This A Good Question/Topic? 0
  • +

Replies To: Get value from 2D array

#2 Matthieu105  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 07-March 13

Re: Get value from 2D array

Posted 08 March 2013 - 11:02 AM

View PostMatthieu105, 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"?

Image: http://www.imagebam....87e2b2242069231


Np got it already
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5774
  • View blog
  • Posts: 12,585
  • Joined: 16-October 07

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.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Get value from 2D array

Posted 08 March 2013 - 07:30 PM

View Postbaavgai, on 08 March 2013 - 04:36 PM, said:

Alternately, you could implement a separate actionlistener instance for each label with the data you want to pass already bound.


Hope farrell2k will read that one :) :) :)
Was This Post Helpful? 0
  • +
  • -

#5 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 822
  • View blog
  • Posts: 2,529
  • Joined: 29-July 11

Re: Get value from 2D array

Posted 08 March 2013 - 07:46 PM

:lol: 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.

for(int i = 0, i < array.length; i++) {
    for(int j = 0; j < array[0].length; j++) {
       if(array[i][j].contains(e.getX(), e.getY())) {
           return new String(i + "," + j);
       }
    }
}


Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Get value from 2D array

Posted 08 March 2013 - 07:51 PM

Cost nothing... or you missed the whole concept

A class with 2 int as instance variables cost about 20 bytes per instance....
probably less than the code you want to put in it to figure out on which JLabel the mouse X,Y was clicked

Just remembering a post where you wrote I was overdoing it by creating a listener object for each label :)/> But may be baavgai, our senior guru, recommendations are better than mine :)/>

This post has been edited by pbl: 08 March 2013 - 07:52 PM

Was This Post Helpful? 0
  • +
  • -

#7 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 822
  • View blog
  • Posts: 2,529
  • Joined: 29-July 11

Re: Get value from 2D array

Posted 08 March 2013 - 08:00 PM

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.

I also remember that post, you mentioned. :)

This post has been edited by farrell2k: 08 March 2013 - 08:01 PM

Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5774
  • View blog
  • Posts: 12,585
  • Joined: 16-October 07

Re: Get value from 2D array

Posted 08 March 2013 - 08:23 PM

Extending JButton would be my first choice.

However, I see nothing fundamentally wrong with this approach:
public class GridClick extends JFrame{
	private final int ROWS = 4, COLS = 5;

	public GridClick() {
		setLayout(new GridLayout(ROWS,COLS,2,2));
		for(int row=0; row<ROWS; row++) {
			for(int col=0; col<COLS; col++) {
				final int r = row, c = col;
				JButton b = new JButton("" + ((row*COLS+col)+1));
				b.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent ae) {
						cellClick(r,c);
					}
				});
				this.add(B)/>;
			}
		}
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(500,500);
	}
	
	private void cellClick(int row, int col) {
		System.out.println("cellClick(" + row + ", " + col + ")" );
	}
}



The least desirable approach would be to search through a list of objects for data that you could have simply handed to the listener.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Get value from 2D array

Posted 08 March 2013 - 08:28 PM

View Postfarrell2k, on 08 March 2013 - 10:00 PM, said:

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
Was This Post Helpful? 0
  • +
  • -

#10 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 822
  • View blog
  • Posts: 2,529
  • Joined: 29-July 11

Re: Get value from 2D array

Posted 08 March 2013 - 08:50 PM

View Postpbl, on 09 March 2013 - 03:28 AM, said:

View Postfarrell2k, on 08 March 2013 - 10:00 PM, said:

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.

But to make you happy, I will say that your method is better.
Was This Post Helpful? 0
  • +
  • -

#11 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5774
  • View blog
  • Posts: 12,585
  • Joined: 16-October 07

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.
Was This Post Helpful? 0
  • +
  • -

#12 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 822
  • View blog
  • Posts: 2,529
  • Joined: 29-July 11

Re: Get value from 2D array

Posted 09 March 2013 - 06:35 AM

Oh, who even cares? Sometimes pbl and I just enjoy going back and forth, then he usually points out something stupid I say, then I get all indignant about it. :)

This post has been edited by farrell2k: 09 March 2013 - 06:43 AM

Was This Post Helpful? 0
  • +
  • -

#13 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 415
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Get value from 2D array

Posted 09 March 2013 - 06:47 AM

And it seems that your original poster disappeared a long time ago, - interesting read nonetheless :)
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Get value from 2D array

Posted 09 March 2013 - 06:46 PM

View Postbaavgai, 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
Was This Post Helpful? 0
  • +
  • -

#15 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5774
  • View blog
  • Posts: 12,585
  • Joined: 16-October 07

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.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2