Results 1 to 11 of 11
  1. #1
    truant420 is offline Member
    Join Date
    Dec 2010
    Posts
    22
    Rep Power
    0

    Default 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
    Java 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 listener class
    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;
               // 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");
                
            }
        }
    The setSquare method
    Java Code:
     public static void setSquare(int row, int column, int val)
        {
            squares[row][column] = new FilledSquare(val);      
        }
    Thank you in advance for your time.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    truant420 is offline Member
    Join Date
    Dec 2010
    Posts
    22
    Rep Power
    0

    Default

    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

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by truant420 View Post
    so getting the row and column as separate ints is not possible?
    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.

  5. #5
    truant420 is offline Member
    Join Date
    Dec 2010
    Posts
    22
    Rep Power
    0

    Default

    I didn't say that, and yes they can be obtained as separate ints.
    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).

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by truant420 View Post
    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 understand, that's why I gave you two suggestions that you could use to get them.

  7. #7
    truant420 is offline Member
    Join Date
    Dec 2010
    Posts
    22
    Rep Power
    0

    Default

    oh i see now. i was thinking of a direct method like getRow/getColumn or something like that. Thank you for the time, Fubarable :)

  8. #8
    truant420 is offline Member
    Join Date
    Dec 2010
    Posts
    22
    Rep Power
    0

    Default

    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
    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");
            
            }
        }
    please help me ;/

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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:

    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;
       }
    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).

    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.

  10. #10
    truant420 is offline Member
    Join Date
    Dec 2010
    Posts
    22
    Rep Power
    0

    Default

    thank you Fubarable, you helped me again :)

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default

    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

  1. insert row and column and delete row and column
    By daredavil82 in forum New To Java
    Replies: 13
    Last Post: 09-22-2011, 06:10 PM
  2. Replies: 9
    Last Post: 02-15-2011, 01:13 AM
  3. Get Row/Column In JEditorPane
    By 67726e in forum AWT / Swing
    Replies: 3
    Last Post: 11-08-2010, 01:10 AM
  4. Replies: 1
    Last Post: 07-06-2010, 04:50 PM
  5. hide column
    By anilkumar_vist in forum Advanced Java
    Replies: 2
    Last Post: 12-13-2009, 05:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •