0

I'm developing a swing applet which features a JPanel with a GridLayout.
The row and columns of the widgets placed into this layout are of significance to the widgets. Currently, I do not keep a reference to the widgets (though they're subclasses of Swing widgets, so they're readily modifiable).

At one stage, I need a widget in this grid to process its neighbouring widgets; that is, the widgets to its left, right, up and down in the GridLayout must be accessible by the widget.
Am I able to get references to these widgets from the GridLayout by knowing their row and column number in the layout? Is there a relevant GridLayout method?
(I believe the Qt GUI framework had a method for this)

Otherwise, I suppose I'll have to keep references to the widgets in an array.
Thanks

Anti Earth
  • 4,671
  • 13
  • 52
  • 83

2 Answers2

2

Am I able to get references to these widgets from the GridLayout by knowing their row and column number in the layout? Is there a relevant GridLayout method?

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • This works, though seems very inelegant (I'm making the key a string with the row and column numbers separated by a comma). There's some casting and index bound errors to worry about. – Anti Earth May 23 '13 at 14:17
  • output in Sting value from getClientProperty can be accelator for [EventHandler](http://stackoverflow.com/a/9007348/714968) – mKorbel May 23 '13 at 14:51
  • there is another way [Component comp = myPanel.findComponentAt(x, y);](http://docs.oracle.com/javase/7/docs/api/java/awt/Container.html#findComponentAt%28int,%20int%29) – mKorbel May 23 '13 at 21:19
  • "Locates the visible child component that contains the specified position"; this makes me believe (also since it raises an unexplained error in my code) the paramters to `findComponent` are pixel positions, and not row and column of the layout – Anti Earth May 24 '13 at 04:12
1

Am I able to get references to these widgets from the GridLayout by knowing their row and column number in the layout?

There is no GridLayout method but you can just get the component from the panel itself.

Container.getComponent(...);

Of course you need a little math to convert the row/column to a single index value.

camickr
  • 321,443
  • 19
  • 166
  • 288