Page 1 of 2 12 LastLast
Results 1 to 20 of 28
Like Tree3Likes

Thread: Get cell position in GridLayout

  1. #1
    daxserver is offline Member
    Join Date
    Apr 2012
    Location
    Vijayawada, India
    Posts
    22
    Rep Power
    0

    Default Get cell position in GridLayout

    I have placed a GridLayout(5,5) and filled all the cells with JButton[5][5] array. When I click on a JButton, can I know the position of the JButton in the Layout?
    Last edited by Norm; 05-28-2012 at 04:32 PM.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default Re: Get cell position in GridLayout

    What do you mean by JButton position? Do you mean the x, y coordinate of the JButton or do you want to know which JButton was clicked?

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

    Default Re: Get cell position in GridLayout

    To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    daxserver is offline Member
    Join Date
    Apr 2012
    Location
    Vijayawada, India
    Posts
    22
    Rep Power
    0

    Default Re: Get cell position in GridLayout

    Quote Originally Posted by wsaryada View Post
    What do you mean by JButton position? Do you mean the x, y coordinate of the JButton or do you want to know which JButton was clicked?
    I mean I want to know which JButton is clicked.

  5. #5
    daxserver is offline Member
    Join Date
    Apr 2012
    Location
    Vijayawada, India
    Posts
    22
    Rep Power
    0

    Default Re: Get cell position in GridLayout

    Quote Originally Posted by DarrylBurke View Post
    To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem.

    db
    I will extract the code from my program and post it 22nd afternoon local time.

  6. #6
    daxserver is offline Member
    Join Date
    Apr 2012
    Location
    Vijayawada, India
    Posts
    22
    Rep Power
    0

    Default Re: Get cell position in GridLayout

    Here is the code. Assume there is some text set to these JButtons (I have removed those lines)
    Java Code:
    JButton[][] button=new JButton[5][5];
    Container content=getContentPane();
    content.setLayout(new GridLayout(5,5));
    for( int x=0;x<button.length;x++)
            for( int y=0;y<button.length;y++)
            {
            	button[x][y]=new JButton();
            	content.add(button[x][y]);
            	button[x][y].setPreferredSize(new Dimension(100,100));
            	button[x][y].setFocusable(false);
            	button[x][y].addMouseListener(new MouseAdapter()
            	{
            		public void mouseClicked(MouseEvent e4)
            		{
            			JButton source=(JButton)e4.getSource();
            			//Display text of JButton
            		}
            	});
            }

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Get cell position in GridLayout

    The code gets a reference (line 16) to the JButton that was clicked. If you could save your info in the JButton you could use that reference to get the info. See the ClientProperites methods for one way to do that.
    Another way would be to extend the JButton class and add your own variables and methods for saving and returning anythin you want.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    StaticFlow is offline Member
    Join Date
    May 2012
    Posts
    17
    Rep Power
    0

    Default Re: Get cell position in GridLayout

    If your wanting to find what the position of that button is in the Grid layout you could loop through the button array with a double for loop
    checking the text of the source button to the text of the button your at in the array until you find a match and then do with it what you want.
    Java Code:
    		                    JButton source=(JButton)e4.getSource();
    		                    String text = source.getText();
    		                    for(int i =0; i <5;i++)
    		                    {
    		                    	for(int j =0; j<5; j++)
    		                    	{
    
    		                    		if(button[i][j].getText().equalsIgnoreCase(text))
    		                    		{
    		                    			System.out.println("the button you clicked is at location "+ i  + ", " + j);
    		                    		}
    		                    	}
    		                    }
    daxserver likes this.

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

    Default Re: Get cell position in GridLayout

    Quote Originally Posted by StaticFlow View Post
    If your wanting to find what the position of that button is in the Grid layout you could loop through the button array with a double for loop
    checking the text of the source button to the text of the button your at in the array until you find a match and then do with it what you want.
    Java Code:
    JButton source=(JButton)e4.getSource();
    String text = source.getText();
    for(int i =0; i <5;i++)
    {
       for(int j =0; j<5; j++)
       {
    
          if(button[i][j].getText().equalsIgnoreCase(text))
          {
             System.out.println("the button you clicked is at location "+ i  + ", " + j);
          }
       }
    }
    Not a great general solution -- what if several JButtons share the same text, or have no text at all. Better to compare one JButton object is the same as another if you're going to use this algorithm.

  10. #10
    StaticFlow is offline Member
    Join Date
    May 2012
    Posts
    17
    Rep Power
    0

    Default Re: Get cell position in GridLayout

    well yea I agree, I was just guessing from his example when he said that there was text in each I assumed they wouldn't be empty nor the same text.

  11. #11
    daxserver is offline Member
    Join Date
    Apr 2012
    Location
    Vijayawada, India
    Posts
    22
    Rep Power
    0

    Default Re: Get cell position in GridLayout

    Quote Originally Posted by StaticFlow View Post
    well yea I agree, I was just guessing from his example when he said that there was text in each I assumed they wouldn't be empty nor the same text.
    I guessed the same but it was not the case there can be empty/same text in random. I am making some changes so that there are no empty ones and no two have the same text.

    I have searched about Client Properties. It is amazing that we can define our own keys. But I don't get how to use them in my case - I am using Eclipse and downloaded JFormBuilder and integrated it into Eclipse and defined two keys to test and I stopped at a point - I don't how to use them lol.
    How to use them?

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Get cell position in GridLayout

    The client properties is a hashtable. Define a class with the data you want to save, create an instance of that class and put it in the clientproperty of the JButton. When you want to get the object with the data, use the same key you used to save it to get it.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    daxserver is offline Member
    Join Date
    Apr 2012
    Location
    Vijayawada, India
    Posts
    22
    Rep Power
    0

    Default Re: Get cell position in GridLayout

    Quote Originally Posted by Norm View Post
    The client properties is a hashtable. Define a class with the data you want to save, create an instance of that class and put it in the clientproperty of the JButton. When you want to get the object with the data, use the same key you used to save it to get it.
    Can you post a (link for) simple example so that I can understand thoroughly.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Get cell position in GridLayout

    Sorry, I have no link other than the API doc:
    Java Platform SE 6

    Use the client properties like you use a hashtable. There is a get and a put method. Use a String for the key: "MyData" for example.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    daxserver is offline Member
    Join Date
    Apr 2012
    Location
    Vijayawada, India
    Posts
    22
    Rep Power
    0

    Default Re: Get cell position in GridLayout

    I want to remove MouseListener for a specific button at some other place in the program. I tried to add removeMouseListener, but did not know what parameter is to be given.

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

    Default Re: Get cell position in GridLayout

    Quote Originally Posted by daxserver View Post
    I want to remove MouseListener for a specific button at some other place in the program. I tried to add removeMouseListener, but did not know what parameter is to be given.
    No, you shouldn't even be using MouseListener with JButtons, that's not what they're built for or how the best work. Either use JLabels with MouseListeners or JButtons with ActionListeners. For instance if you disable a JButton via setEnabled(false), an ActionListener would recognize this and would not function for the disabled JButton. A MouselListener on the other hand would not behave correctly and would continue to react to mouse presses on the disabled JButton.

    If I wanted to identify a button pushed in a grid, I'd either iterate through the array of JButtons or obtain the button object from the ActionEvent's getSource.
    daxserver likes this.

  17. #17
    daxserver is offline Member
    Join Date
    Apr 2012
    Location
    Vijayawada, India
    Posts
    22
    Rep Power
    0

    Default Re: Get cell position in GridLayout

    Quote Originally Posted by Fubarable View Post
    No, you shouldn't even be using MouseListener with JButtons, that's not what they're built for or how the best work. Either use JLabels with MouseListeners or JButtons with ActionListeners. For instance if you disable a JButton via setEnabled(false), an ActionListener would recognize this and would not function for the disabled JButton. A MouselListener on the other hand would not behave correctly and would continue to react to mouse presses on the disabled JButton.
    Yes, the MouseListener continued reacting even when i disabled it. But there are 25 buttons in an array[][]. How can I implement ActionListener for each of them?

    Quote Originally Posted by Fubarable View Post
    If I wanted to identify a button pushed in a grid, I'd either iterate through the array of JButtons or obtain the button object from the ActionEvent's getSource.
    How can we get ActionEvent's getSource?
    Last edited by daxserver; 05-25-2012 at 12:02 AM.

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

    Default Re: Get cell position in GridLayout

    Quote Originally Posted by daxserver View Post
    Yes, the MouseListener continued reacting even when i disabled it. But there are 25 buttons in an array[][]. How can I implement ActionListener for each of them?
    Create a single ActionListener object, and add them to the JButtons in a nested for loop. It's done all the time.

    How can we get ActionEvent's getSource?
    The ActionEvent object is passed into the ActionListeners actionPerformed method as a parameter. Simply call getSource() on this object.

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

    Default Re: Get cell position in GridLayout

    Perhaps we'll all be better served if you explain in greater detail just what behavior you're trying to achieve rather than what code solution you're trying to use to achieve it. In other words, what is the goal of this part of the program? A Sudoku grid? A memory game? Jeopardy game? Something else? The optimal solution may be something entirely different and will likely depend on these as yet unstated details.

  20. #20
    daxserver is offline Member
    Join Date
    Apr 2012
    Location
    Vijayawada, India
    Posts
    22
    Rep Power
    0

    Default Re: Get cell position in GridLayout

    Quote Originally Posted by Fubarable View Post
    Create a single ActionListener object, and add them to the JButtons in a nested for loop. It's done all the time.
    I replaced MouseListener with ActionListener and good results when I disabled the button

    And I am writing a memory-like game.
    Last edited by daxserver; 05-25-2012 at 12:45 PM. Reason: Spelling Mistake.

Page 1 of 2 12 LastLast

Similar Threads

  1. Alignment in GridLayout
    By Zamereon in forum AWT / Swing
    Replies: 2
    Last Post: 02-27-2011, 11:49 AM
  2. help! Gridlayout
    By eiramae in forum Java Applets
    Replies: 3
    Last Post: 02-19-2011, 05:11 AM
  3. get position in string from caret position
    By helloworld111 in forum AWT / Swing
    Replies: 5
    Last Post: 02-19-2009, 01:36 AM
  4. SWT GridLayout
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-08-2008, 09:04 AM
  5. Gridlayout
    By Marty in forum AWT / Swing
    Replies: 2
    Last Post: 05-31-2007, 11:48 AM

Posting Permissions

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