How do I get the GidBagLayout location from my buttons?

How could I get the location of my JButtons (or JLabels) in my GridBoxLayout?

I've tried all that I could think of..

here some code sample

JButton button;

GridBagLayout layout =new GridBagLayout();

[...]

GridBagConstraints c =new GridBagConstraints();

c.weightx = 0.5;

c.weighty = 0.5;

c.insets =new Insets(20,20,20,20);

button =new JButton("Button 1");

c.gridx = 0;

c.gridy = 0;

button.addActionListener(this);

centerPanel.add(button, c);

button =new JButton("Button 2");

c.gridx = 1;

c.gridy = 0;

button.addActionListener(this);

centerPanel.add(button, c);

[...]

System.out.println(((JButton)e.getSource()).getText());

System.out.println(layout.getLayoutOrigin());

//System.out.println(((JButton)e.getSource()).);

//System.out.println(((JButton)e.getSource()).location());

System.out.println(e.getSource().toString());

System.out.println(layout.getLayoutDimensions());

//System.out.println(layout.lookupConstraints(((JButton)e.getSource())));

System.out.println(layout.getLayoutOrigin());

I think that's all you experts would need..

what I'm trying to do is when I click on one of them buttons then fetch where that buttons is (in the GridBagLayout, not absolute) and be able to add a new button to the side or under the clicked button..

the System.out.println(layout.getLayoutOrigin()); does seem to give me x=0 and y=0 but that's the same with all buttons so it isn't good enough

[1960 byte] By [Nizzlea] at [2007-11-27 11:49:25]
# 1

if you are using absolute constraints, you could:

- call getConstraints on the layout.

- add the constraints as a client property of the button for later retrieval.

getLayoutOrigin has to do with the layout's upper-left point in the parent container, not the contents of the layout. So it will typically be 0,0, except if borders/insets otherwise affect it (probably).

bsampieria at 2007-7-29 18:25:06 > top of Java-index,Desktop,Core GUI APIs...
# 2

> I think that's all you experts would need..

You would think wrong. We need a SSCCE to see exactly what you are doing.

see http://homepage1.nifty.com/algafield/sscce.html,

In general component don't have a size or location until the GUI is visible. That is the layout manager isn't invoked until the component is displayed.

If you want to add a component after the component you click on then you would probably need to use the add(...) method that takes a position variable as a parameter.

camickra at 2007-7-29 18:25:06 > top of Java-index,Desktop,Core GUI APIs...
# 3

I'm sorry I don't understand..

I tried this

System.out.println(((JButton)e.getSource()).getConstraints());

but it gives me this error

The method getConstraints() is undefined for the type JButton

Nizzlea at 2007-7-29 18:25:06 > top of Java-index,Desktop,Core GUI APIs...
# 4

layout.getConstraints(button).

Read the API docs.

bsampieria at 2007-7-29 18:25:06 > top of Java-index,Desktop,Core GUI APIs...
# 5

> > I think that's all you experts would need..

>

> You would think wrong. We need a SSCCE to see exactly

> what you are doing.

>

> see http://homepage1.nifty.com/algafield/sscce.html,

>

> In general component don't have a size or location

> until the GUI is visible. That is the layout manager

> isn't invoked until the component is displayed.

>

> If you want to add a component after the component

> you click on then you would probably need to use the

> add(...) method that takes a position variable as a

> parameter.

yes I think I need to use the add(..) and a position variable

the problem is that I need to get the position variable from the other component so I could put the new component at the right position

as for the SSCCE I do think that I do that..

but for some extra information

I'm writing an applet.. and I use a borderlayout for some component.. in the center panel I've placed a GridBagLayout and some buttons.. I've given the buttons an ActionListener and now I want to get the location in the GridBag of the Button I've pressed

so when I click one of them buttons I want to know it's location.. as in X=1, Y=2 or something

so I could place the new button on X=1, Y=3

Nizzlea at 2007-7-29 18:25:06 > top of Java-index,Desktop,Core GUI APIs...
# 6

> layout.getConstraints(button).

>

> Read the API docs.

to be honest I did read and I thought that too

System.out.println(layout.getConstraints((JButton)e.getSource()));

gives me

java.awt.GridBagConstraints@12498b5

and I have no clue what to do with that.. :(

Nizzlea at 2007-7-29 18:25:06 > top of Java-index,Desktop,Core GUI APIs...
# 7

A really more important question is:

Why do you want to know that button X is at a specific grid position?

That seems to be somewhat an odd thing to need to know.

bsampieria at 2007-7-29 18:25:06 > top of Java-index,Desktop,Core GUI APIs...
# 8

> > layout.getConstraints(button).

> >

> > Read the API docs.

>

> to be honest I did read and I thought that too

>

> System.out.println(layout.getConstraints((JButto

> n)e.getSource()));

>

> gives me

> java.awt.GridBagConstraints@12498b5

>

> and I have no clue what to do with that.. :(

It returns a GridBagConstraints, the one that the button is associated with. And last I checked, GridBagConstraints objects have gridx and gridy fields, which seems to be what you want... for whatever strange reason.

bsampieria at 2007-7-29 18:25:06 > top of Java-index,Desktop,Core GUI APIs...
# 9

because I want to make some kind of FlowChart creating applet

so when I know the X location of the buttons that's pressed I could place a new button next to or under it for the next part of the FlowChart..

unless you'd know an other way to do it..

I'm a bit of a newbie so that's the best I could think of

Nizzlea at 2007-7-29 18:25:06 > top of Java-index,Desktop,Core GUI APIs...
# 10

Hmmm... I doubt GridBagLayout is the best for this. The one problem I see with GBL is that if you wanted to insert something between 2 other items, then you will have to do all this shifting around.

I'd probably look for a 3rd party library that is specifically designed to display a flowchart. It would have an API that is properly aligned with the functionality you want.

Never used it before, but here's one (although not free):

http://mindfusion.org/jdiagram.html

There might be something at jfree.org

bsampieria at 2007-7-29 18:25:06 > top of Java-index,Desktop,Core GUI APIs...
# 11

Otherwise, if I had to write something, I would start by creating a general model to structure the contents logically. Then create a custom layout manager or something that could work with that data and lay things out accordingly.

bsampieria at 2007-7-29 18:25:06 > top of Java-index,Desktop,Core GUI APIs...
# 12

> the problem is that I need to get the position variable from the other component

No, you need to get the position information from the parent container. You loop through all the compnents added to the container. When you find the component that matches the component you just clicked, then you know the index number of the component in the container and can then just add your new component in the next position.

Adding a component on the next row will be more difficult as you will need to compare the x and y values of each component to find the proper place to insert your new component. I'll let you figure out the algorithm for that.

camickra at 2007-7-29 18:25:06 > top of Java-index,Desktop,Core GUI APIs...
# 13

> Otherwise, if I had to write something, I would start

> by creating a general model to structure the contents

> logically. Then create a custom layout manager or

> something that could work with that data and lay

> things out accordingly.

I don't have a clue where to begin with that..

maybe I should discuss about buying software.. either that API that you said or some full program.. I've found one which pretty much like what we need

Nizzlea at 2007-7-29 18:25:06 > top of Java-index,Desktop,Core GUI APIs...
# 14

> > the problem is that I need to get the position

> variable from the other component

>

> No, you need to get the position information from the

> parent container. You loop through all the compnents

> added to the container. When you find the component

> that matches the component you just clicked, then you

> know the index number of the component in the

> container and can then just add your new component in

> the next position.

>

> Adding a component on the next row will be more

> difficult as you will need to compare the x and y

> values of each component to find the proper place to

> insert your new component. I'll let you figure out

> the algorithm for that.

whoa lol I don't have a clue what you're on about

that's getting way too advanced for a newbie like me

Nizzlea at 2007-7-29 18:25:06 > top of Java-index,Desktop,Core GUI APIs...
# 15

> No, you need to get the position information from the

> parent container. You loop through all the compnents

> added to the container.

He's talking (I think) about finding the index of a component in the container (components are stored in an array internally, indexed as added (usually)). But with a GridBagLayout, the index doesn't mean anything with regards to the layout positioning... hence all the fuss over constraints objects.

Message was edited by:

bsampieri

bsampieria at 2007-7-29 18:25:11 > top of Java-index,Desktop,Core GUI APIs...