15 Replies - 230 Views - Last Post: 09 March 2013 - 08:37 PM
#1
Get value from 2D array
Posted 08 March 2013 - 10:47 AM
Image: http://www.imagebam....87e2b2242069231
Replies To: Get value from 2D array
#2
Re: Get value from 2D array
Posted 08 March 2013 - 11:02 AM
Matthieu105, on 08 March 2013 - 10:47 AM, said:
Image: http://www.imagebam....87e2b2242069231
Np got it already
#3
Re: Get value from 2D array
Posted 08 March 2013 - 02:36 PM
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.
#5
Re: Get value from 2D array
Posted 08 March 2013 - 07:46 PM
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);
}
}
}
#6
Re: Get value from 2D array
Posted 08 March 2013 - 07:51 PM
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
This post has been edited by pbl: 08 March 2013 - 07:52 PM
#7
Re: Get value from 2D array
Posted 08 March 2013 - 08:00 PM
I also remember that post, you mentioned.
This post has been edited by farrell2k: 08 March 2013 - 08:01 PM
#8
Re: Get value from 2D array
Posted 08 March 2013 - 08:23 PM
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.
#9
Re: Get value from 2D array
Posted 08 March 2013 - 08:28 PM
farrell2k, on 08 March 2013 - 10:00 PM, said:
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
#10
Re: Get value from 2D array
Posted 08 March 2013 - 08:50 PM
pbl, on 09 March 2013 - 03:28 AM, said:
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.
#11
Re: Get value from 2D array
Posted 09 March 2013 - 04:12 AM
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.
#12
Re: Get value from 2D array
Posted 09 March 2013 - 06:35 AM
This post has been edited by farrell2k: 09 March 2013 - 06:43 AM
#13
Re: Get value from 2D array
Posted 09 March 2013 - 06:47 AM
#14
Re: Get value from 2D array
Posted 09 March 2013 - 06:46 PM
baavgai, on 09 March 2013 - 06:12 AM, said:
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
#15
Re: Get value from 2D array
Posted 09 March 2013 - 08:03 PM
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.
|
|

New Topic/Question
Reply



MultiQuote






|