4

Is there a way using JGoodies FormLayout to change the alignment of a component once it has been set?

for example,

CellConstraints cc = new CellCosntraints();
panel.add(component,cc.xy(1,1,CellConstraints.DEFAULT,CellConstraints.FILL));

If I want to change component to be have a row constraint of DEFAULT instead of FILL, is there a way to change it now that it has been set without removing and re-adding the component?

Jeff Storey
  • 56,312
  • 72
  • 233
  • 406

2 Answers2

5

It looks like you can:

FormLayout l = new FormLayout();
...
l.setContraints(component, newconstraints);

then probably do a revalidate() on the container to update things.

Jim
  • 3,476
  • 4
  • 23
  • 33
  • Ah, I was thinking I wasn't going to use that because it takes a whole new cell constraints object. I'll just modify the current one and then reset it for that component. I'll give that a try shortly, thanks. – Jeff Storey Mar 05 '12 at 18:24
  • The code clones the constraint object, so that should be safe. [source](http://grepcode.com/file/repo1.maven.org/maven2/com.jgoodies/forms/1.2.1/com/jgoodies/forms/layout/FormLayout.java#FormLayout.setConstraints%28java.awt.Component%2Ccom.jgoodies.forms.layout.CellConstraints%29) – Jim Mar 05 '12 at 18:28
  • Are you sure about that? I didn't think it actually cloned the object. But I still think this will work since you can reuse the cellconstraints object for multiple components. – Jeff Storey Mar 05 '12 at 18:59
  • I linked the source code which shows that it does clone() the constraints. – Jim Mar 05 '12 at 21:31
0

there are two ways (@Jim +1 for correct direction)

1) fills available Dimension inside container without resize for container

revalidate() //covered validate()
repaint() // required in some cases for JLabel, JTextComponents, JScrollPane ...

2) fills available Dimension inside container with resize for container

pack();

this code can help you

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319