This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Component number inside GridLayout. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Component number inside GridLayout." Watch "Component number inside GridLayout." New topic

Component number inside GridLayout.

Heinrich Umdelin

Joined: Apr 07, 2009
Posts: 1

I have a 6x6 GridLayout with JButtons inside a JFrame. Now there is a method getComponent(int i) wich will give you the i'th Component counting from top left corner down. I seem to remember (maybe wrongly) that there is a method which will return
this component number for Component. But searching the docs for eg AbstractButton comes up with nothing. Pointers ?
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46424
Welcome to JavaRanch

I went to the API index -> G -> ctrl-F -> "getComponent" but I couldn't find a method called "getComponentNumber". Nor could I find such a method in Container or Component.

It seems non-object-oriented that a child Component know which order it was added to a parent Component/Container. It would be the parent Component/Container which ought to know that.
There are methods like getComponents() which return all the child Components (presumably as Component[]); they might help.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10836

In addition to what Campbell said, you can also use getComponentCount() and getComponentAt(int index).

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
I agree. Here's the link:
subject: Component number inside GridLayout.
It's not a secret anymore!