Results 1 to 11 of 11
- 03-13-2011, 09:50 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 22
- Rep Power
- 0
how to get the row and column of a component
I am using gridlayout to display a 9x9 grid of jbuttons and jlabels, representing a futoshiki puzzle (thanks Fubarable). When you click a button it changes values from 0 to 5. I have another class (Futoshiki) that holds all the data of the puzzle and the GUI class is responsible only for displaying that data. Upon clicking a button at a specific row and column i want to set the value of the same cell in the Futoshiki class to the whatever text is in the button. I know i can do this by writing a separate listener class for each button and set the row and the column manually(as in the code below) but i was wondering if there is a more efficient way of doing this. Is it possible to get the row and the column of a specific component(button) and return it as an int value that can be passed on to another method?
Here is my code:
the button
The listener classJava Code:panelGrid.setLayout(new GridLayout(0, 9)); JButton b00 = new JButton(" "); ActionListener ac = new Counter(b00); b00.addActionListener(new Counter(b00)); panelGrid.add(b00);
The setSquare methodJava Code:public class Counter implements ActionListener { private int count; private JButton button; private int row; private int col; public Counter(JButton b) { button = b; // here i am doing this manually row = 0; col = 0; } public void actionPerformed(ActionEvent e) { count++; if (count > 5) { count = 0; } button.setText(""+count); Futoshiki f = new Futoshiki(); Futoshiki.setSquare(row, col, count); System.out.println("CLICKED "+count+" TIMES"); } }
Thank you in advance for your time.Java Code:public static void setSquare(int row, int column, int val) { squares[row][column] = new FilledSquare(val); }
-
One possible way to do this (and I'm not sure how kosher this is), is to set the actionCommand String of the button with the two numbers, perhaps separated by a space. Then this can simply be extracted in the actionPerformed method by calling getActionCommand on the ActionEvent object that is passed into the method parameter. Another way is to get the JButton that has been pushed via the getSource method of ActionEvent, and loop through all of the buttons until the pressed button has been reached.
- 03-13-2011, 11:31 PM #3
Member
- Join Date
- Dec 2010
- Posts
- 22
- Rep Power
- 0
so getting the row and column as separate ints is not possible?
Also do you mind explaining the second way a bit more because I'm not sure i understand how it's supposed to help? If it's obvious, please don't judge me, it's a real mess in my head right now
-
I didn't say that, and yes they can be obtained as separate ints.
Also do you mind explaining the second way a bit more because I'm not sure i understand how it's supposed to help? If it's obvious, please don't judge me, it's a real mess in my head right now
You loop through all your buttons in nested for loops. When you find a button that equals the current button, the indices of the for loop will be your button's row and column.
- 03-14-2011, 12:46 AM #5
Member
- Join Date
- Dec 2010
- Posts
- 22
- Rep Power
- 0
Well if i could do that all i need to do i pass these ints as the setSquare's parameters for row and column and that would solve my problem...right :confused: The thing is i don't know how to obtain them and i couldn't find a way in the api library (might have missed something, although i checked several times).I didn't say that, and yes they can be obtained as separate ints.
-
- 03-14-2011, 01:02 AM #7
Member
- Join Date
- Dec 2010
- Posts
- 22
- Rep Power
- 0
oh i see now. i was thinking of a direct method like getRow/getColumn or something like that. Thank you for the time, Fubarable :)
- 03-14-2011, 02:28 AM #8
Member
- Join Date
- Dec 2010
- Posts
- 22
- Rep Power
- 0
ok i am ashamed to admit but i don't know how to construct my loops :S this is what i came up with and needless to say it doesn't work
please help me ;/Java Code:public class Counter implements ActionListener { private int count; private JButton button; private int row; private int col; public Counter(JButton b) { button = b; } public void actionPerformed(ActionEvent e) { count++; if (count > 5) { count = 0; } button.setText(""+count); button = (JButton) e.getSource(); for (int row = 0; row < 10; row++) { for (int col = 0; col<10; col++){ JButton b = (JButton) e.getSource(); if (button.equals(b)) { Futoshiki f = new Futoshiki(); Futoshiki.setSquare(row, col, count); } } } System.out.println("CLICKED "+count+" TIMES"); } }
-
If you wanted to do this by the loop method, and your ActionListener is in a separate and distinct class from your GUI, then one way to do this is to have this method in the GUI section of the code, not the ActionListener class.
So your GUI could have a method like so:
And your Listener would get a reference to the GUI via a parameter to its constructor (assuming that you want one listener object to be used on all the buttons).Java Code:/** * @param buttonPressed * @return int[] where [0] holds row and [1] holds column number * or returns {-1, -1} if error */ public int[] getRowCol(JButton buttonPressed) { int[] result = {-1, -1}; // assuming that the buttons are held in a 2-dimensional array called buttons for (int row = 0; row < buttons.length; row++) { for (int col = 0; col < buttons[row].length; col++) { if (buttons[row][col] == buttonPressed) { result[0] = row; result[1] = col; return result; } } } return result; }
Then in the Listener, again you'd get a reference to the button pressed via the ActionEvent's getSource method, and you'd call the gui's method above passing in this reference.
- 03-14-2011, 09:30 PM #10
Member
- Join Date
- Dec 2010
- Posts
- 22
- Rep Power
- 0
thank you Fubarable, you helped me again :)
- 03-16-2011, 06:51 PM #11
Cross posted verbatim at
how to get the row and column of a component gridlayout
They must be scratching their heads there trying to figure out who Fubarable might be.
db
Similar Threads
-
insert row and column and delete row and column
By daredavil82 in forum New To JavaReplies: 13Last Post: 09-22-2011, 06:10 PM -
How to write column by column to text file
By trkece in forum JDBCReplies: 9Last Post: 02-15-2011, 01:13 AM -
Get Row/Column In JEditorPane
By 67726e in forum AWT / SwingReplies: 3Last Post: 11-08-2010, 01:10 AM -
selecting column names dynamically from table column header
By neha_sinha in forum AWT / SwingReplies: 1Last Post: 07-06-2010, 04:50 PM -
hide column
By anilkumar_vist in forum Advanced JavaReplies: 2Last Post: 12-13-2009, 05:46 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks