Finding the coordinates of an object which is in a GridBagLayout

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1822 Views - Last Post: 29 November 2012 - 01:39 AM Rate Topic: -----

#1 Allophyl  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 09-August 10

Finding the coordinates of an object which is in a GridBagLayout

Posted 27 November 2012 - 11:45 AM

I currently have a panel in which there are several JTextFields and JTextAreas in a GridBagLayout, and I need to overlay images over the top left corner of each, so I've put the panel in a JLayeredPane and created a second Panel which will contain the images to put above it.

I would like to be able to get the positions of the textfields and textareas so that I can position each image relative to them instead of having to hard-code the coordinates of each (which means they all have to be redone any time I change something, and they end up being off if the program is run in a different OS). The problem is that when I try to get the coordinates of any of the components, all I ever get is 0,0. I've tried getLocation, I've tried getBounds and I've tried getX and getY. I even tried to do getLayoutDimensions on the layout itself thinking I might potentially be able to use that, but it just returned an array of two empty arrays.

So is there any way I can actually get the coordinates of the components in that gridbaglayout? Is there a better way of going about this?

Is This A Good Question/Topic? 0
  • +

Replies To: Finding the coordinates of an object which is in a GridBagLayout

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Finding the coordinates of an object which is in a GridBagLayout

Posted 27 November 2012 - 12:25 PM

I am afraid not if the JComponent position is known by the GridLayout not the JComponent itself as you seem to confirm it.
Just another good reason not to use GridBagLayout and leave it to GUI Builder :) How many components do you have per layer ?
Was This Post Helpful? 0
  • +
  • -

#3 Allophyl  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 09-August 10

Re: Finding the coordinates of an object which is in a GridBagLayout

Posted 27 November 2012 - 01:45 PM

Okay, that's annoying. Other than using a GUI editor (which I'd rather not use, and wouldn't really solve the problem of things moving around when I change stuff), is there any other viable solution?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Finding the coordinates of an object which is in a GridBagLayout

Posted 27 November 2012 - 01:53 PM

In 15 years of Java, I never used a GridBagLayout and I have written quite complicated GUI
I always manage to do what I want with a combination of:
- BorderLayout
- GridLayout
- FlowLayout
- Box.createHorizontalBox() and Box.createVerticalBox()
Was This Post Helpful? 1
  • +
  • -

#5 Allophyl  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 09-August 10

Re: Finding the coordinates of an object which is in a GridBagLayout

Posted 27 November 2012 - 04:16 PM

Well, I've tried putting them in a BoxLayout as well as putting them in a Box, and in both cases I still only ever get 0, 0 for the coordinates of the components inside. What else can I do?
Was This Post Helpful? 0
  • +
  • -

#6 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2068
  • View blog
  • Posts: 4,301
  • Joined: 11-December 07

Re: Finding the coordinates of an object which is in a GridBagLayout

Posted 27 November 2012 - 05:17 PM

The components DO know their locations, otherwise they would be drawn in the wrong place. However, the positions aren't set until the LayoutManager sets it up. I bet if you set your top level window to visible, set its size and validate() it, the coordinates will become set. If that doesn't work, there will be some doLayout() method or something else that will work.

A better solution is to create a subclass of JPanel that draws an image in its top left. You can then put each of your components into its own specialised JPanel and the images will automatically be in the right place.
Was This Post Helpful? 1
  • +
  • -

#7 Ghlavac  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 519
  • Joined: 14-January 09

Re: Finding the coordinates of an object which is in a GridBagLayout

Posted 27 November 2012 - 05:33 PM

Consider using MiGLayout.


Its an amazing alternative to all the crap in the JRE, and has some of the parts of gridbag but also a ton of other features.


Makes it really easy to use and avoids most of the annoying BS that makes up most other layout managers.
Was This Post Helpful? 0
  • +
  • -

#8 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,620
  • Joined: 29-July 11

Re: Finding the coordinates of an object which is in a GridBagLayout

Posted 27 November 2012 - 05:37 PM

I can't believe that you cannot get the x,y or any component with getBounds().
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Finding the coordinates of an object which is in a GridBagLayout

Posted 27 November 2012 - 06:32 PM

They need to be drawn first, which may be complicated with different layers in a LayeredPane
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Finding the coordinates of an object which is in a GridBagLayout

Posted 27 November 2012 - 07:41 PM

View PostAllophyl, on 27 November 2012 - 07:16 PM, said:

Well, I've tried putting them in a BoxLayout as well as putting them in a Box, and in both cases I still only ever get 0, 0 for the coordinates of the components inside. What else can I do?

I am not sure at all you are using the good approach: putting the components in a certain layout and then finding the coordinates of these components to draw over them
Simply use the same layout in all layers
Was This Post Helpful? 0
  • +
  • -

#11 Ghlavac  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 519
  • Joined: 14-January 09

Re: Finding the coordinates of an object which is in a GridBagLayout

Posted 27 November 2012 - 09:25 PM

The point of not being able to obtain this stuff beforehand is the very reason that you utilize a layout manager though, you should never explicitly need to know the bounds as long as you're using the correct layout managers.
Was This Post Helpful? 0
  • +
  • -

#12 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2068
  • View blog
  • Posts: 4,301
  • Joined: 11-December 07

Re: Finding the coordinates of an object which is in a GridBagLayout

Posted 28 November 2012 - 12:39 AM

PBL, are you sure they need to be drawn first? I think they just have to be layed out first which you can probably find a way of doing before the object is drawn.

I still think sub classing a panel to overlay an image in its top left is the way to go. A dozen lines for a simple solution.
Was This Post Helpful? 0
  • +
  • -

#13 Allophyl  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 09-August 10

Re: Finding the coordinates of an object which is in a GridBagLayout

Posted 28 November 2012 - 12:46 AM

View Postpbl, on 27 November 2012 - 10:41 PM, said:

View PostAllophyl, on 27 November 2012 - 07:16 PM, said:

Well, I've tried putting them in a BoxLayout as well as putting them in a Box, and in both cases I still only ever get 0, 0 for the coordinates of the components inside. What else can I do?

I am not sure at all you are using the good approach: putting the components in a certain layout and then finding the coordinates of these components to draw over them
Simply use the same layout in all layers

Using the same layout in all layers wouldn't help at all. With both GridBags and BoxLayout, each "cell" is sized according to the size of the component inside of it (unless you hard-code the size of the cells ahead of time, which is rarely a good idea for something like this). If I used the exact same layout in the layer for the images, the cells would just be sized according to the sizes of the images, which have no relation at all to the size of the components in the layer below.

View Postcfoley, on 27 November 2012 - 08:17 PM, said:

The components DO know their locations, otherwise they would be drawn in the wrong place. However, the positions aren't set until the LayoutManager sets it up. I bet if you set your top level window to visible, set its size and validate() it, the coordinates will become set. If that doesn't work, there will be some doLayout() method or something else that will work.

A better solution is to create a subclass of JPanel that draws an image in its top left. You can then put each of your components into its own specialised JPanel and the images will automatically be in the right place.


Thank you thank you thank you thank you!
I tried calling doLayout() on the box that the components are in and I'm able to get the coordinates after doing so. Works great now!
Was This Post Helpful? 0
  • +
  • -

#14 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2068
  • View blog
  • Posts: 4,301
  • Joined: 11-December 07

Re: Finding the coordinates of an object which is in a GridBagLayout

Posted 28 November 2012 - 02:16 AM

Glad you got it working!
Was This Post Helpful? 0
  • +
  • -

#15 Allophyl  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 09-August 10

Re: Finding the coordinates of an object which is in a GridBagLayout

Posted 28 November 2012 - 12:30 PM

By the way, why do people hate GridBagLayout so much? I use it all the time and I love it, this is the first time since actually learning how to use it that I've had a real problem with it that I couldn't figure out on my own. It does tend to clutter up your code a bit with all the GridBagConstraints objects you have to create and set the values of, but it's incredibly flexible and powerful.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2