开发者

How to reset JLabel constraints after initializing it?

开发者 https://www.devze.com 2023-01-03 21:50 出处:网络
I initialize a JLabel in Java frame like this : contentPane.add(myLabel, cc.xywh(1, 1, 31, 6, CellConstraints.DEFAULT, CellConstraint开发者_高级运维s.BOTTOM));

I initialize a JLabel in Java frame like this :

contentPane.add(myLabel, cc.xywh(1, 1, 31, 6, CellConstraints.DEFAULT, CellConstraint开发者_高级运维s.BOTTOM));

But before showing the JFrame i make a small condition which if returns true i want to set myLabel to be set to DEFAULT instead of BOTTOM, but i can't find anyway except redefining it again like this :

contentPane.add(myLabel, cc.xywh(1, 1, 31, 6));

So is there a better way to just edit the vertical location property ?


Assuming you are using a jgoodies FormLayout, you are somewhat restricted in the reuse of your CellConstraints instances. It appears from the documentation and examples that each component is added to the panel and the CellConstraints instances are copied. Further, when trying to gather the constraints for the current component, a copy of the CellConstraints is returned. However, you do have an option: you can take that copy of the CellConstraints, modify the public vAlign instance var and then call setConstraints on your FormLayout with this updated constraints set.

FormLayout layout = (FormLayout)contentPane.getLayout(); 
cc = layout.getConstraints(myLabel);
cc.vAlign = CellConstraints.DEFAULT;
layout.setConstraints(myLabel, cc);

It is a little bit more verbose than the simple re-add that you have provided, but it removes the need to maintain the magic numbers (1,1,31,6) in your original instance.

0

精彩评论

暂无评论...
验证码 换一张
取 消