Results 1 to 20 of 28
Thread: Get cell position in GridLayout
- 05-21-2012, 01:05 PM #1
Member
- Join Date
- Apr 2012
- Location
- Vijayawada, India
- Posts
- 22
- Rep Power
- 0
- 05-21-2012, 03:16 PM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
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?
Website: Learn Java by Examples
- 05-21-2012, 03:22 PM #3
Re: Get cell position in GridLayout
To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-21-2012, 10:27 PM #4
Member
- Join Date
- Apr 2012
- Location
- Vijayawada, India
- Posts
- 22
- Rep Power
- 0
- 05-21-2012, 10:29 PM #5
Member
- Join Date
- Apr 2012
- Location
- Vijayawada, India
- Posts
- 22
- Rep Power
- 0
- 05-22-2012, 12:09 PM #6
Member
- Join Date
- Apr 2012
- Location
- Vijayawada, India
- Posts
- 22
- Rep Power
- 0
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 } }); }
- 05-22-2012, 01:19 PM #7
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.
- 05-22-2012, 06:35 PM #8
Member
- Join Date
- May 2012
- Posts
- 17
- Rep Power
- 0
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); } } }
-
Re: Get cell position in GridLayout
- 05-22-2012, 06:49 PM #10
Member
- Join Date
- May 2012
- Posts
- 17
- Rep Power
- 0
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.
- 05-22-2012, 06:57 PM #11
Member
- Join Date
- Apr 2012
- Location
- Vijayawada, India
- Posts
- 22
- Rep Power
- 0
Re: Get cell position in GridLayout
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?
- 05-22-2012, 07:14 PM #12
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.
- 05-22-2012, 07:34 PM #13
Member
- Join Date
- Apr 2012
- Location
- Vijayawada, India
- Posts
- 22
- Rep Power
- 0
- 05-22-2012, 07:41 PM #14
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.
- 05-24-2012, 10:05 PM #15
Member
- Join Date
- Apr 2012
- Location
- Vijayawada, India
- Posts
- 22
- Rep Power
- 0
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.
-
Re: Get cell position in GridLayout
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.
- 05-24-2012, 11:54 PM #17
Member
- Join Date
- Apr 2012
- Location
- Vijayawada, India
- Posts
- 22
- Rep Power
- 0
Re: Get cell position in GridLayout
Last edited by daxserver; 05-25-2012 at 12:02 AM.
-
Re: Get cell position in GridLayout
Create a single ActionListener object, and add them to the JButtons in a nested for loop. It's done all the time.
The ActionEvent object is passed into the ActionListeners actionPerformed method as a parameter. Simply call getSource() on this object.How can we get ActionEvent's getSource?
-
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.
- 05-25-2012, 12:44 PM #20
Member
- Join Date
- Apr 2012
- Location
- Vijayawada, India
- Posts
- 22
- Rep Power
- 0
Similar Threads
-
Alignment in GridLayout
By Zamereon in forum AWT / SwingReplies: 2Last Post: 02-27-2011, 11:49 AM -
help! Gridlayout
By eiramae in forum Java AppletsReplies: 3Last Post: 02-19-2011, 05:11 AM -
get position in string from caret position
By helloworld111 in forum AWT / SwingReplies: 5Last Post: 02-19-2009, 01:36 AM -
SWT GridLayout
By Java Tip in forum Java TipReplies: 0Last Post: 01-08-2008, 09:04 AM -
Gridlayout
By Marty in forum AWT / SwingReplies: 2Last Post: 05-31-2007, 11:48 AM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote
.gif)

Bookmarks