开发者

Align Swing Components across Panels

开发者 https://www.devze.com 2023-01-16 20:41 出处:网络
We have a JPanel which co开发者_运维问答ntains multiple JPanels which contain JComponents (let\'s say JLabels and JTextboxes)

We have a JPanel which co开发者_运维问答ntains multiple JPanels which contain JComponents (let's say JLabels and JTextboxes)

Inside each of the internal JPanels we use JGoodies Layout in order to ensure proper alignment of all Labels.

But of course we would like to have all the Labels aligned independently on which subpanel they are.

How could we do that, without fixing the width of the column which contains the JLabels?

We can't loose the JPanels since we have to have Borders around groups of Components.


I recommend to favor flat layouts over nested ones. In a single layout alignment is easy. Avoid TitledBorders and replace them with titled separators, separators, or just white space. That helps for the vast majority of editors and forms.

But if you want to align across multiple editors or forms, the above technique fails. The JGoodies FormLayout provides two levels to address this problems, and more generally to improve layout consistency: 1) lower bounds for sizes, 2) layout variables.

With 1) you can describe layouts that ensure a minimum width across forms. For example, if you want to say that all label columns have at least a width of 100px, you can say "[100px, pref]" for the label column.

2) goes beyond approach 1). And the motivation is to extract the 100px from your many forms. In FormLayout you can setup layout variables, for example $label that you configure as "[100px, pref]" or "right:[75dlu, pref]", etc. If you use the layout variable in all your editors, these will be consistent and you have a single place where you can configure all label columns for all editors.


There's no simple way to do that which I'm aware of. Your options:

  • Write your own layout manager (or extend an existing one) with that capability
  • Fixed column widths
  • Decide that panels that are visually separated by borders don't need to have their contents aligned after all


Just because the JPanels have borders, doesn't mean that they actually need to contain their apparent contents. Set the panels to transparent. Add the panels and the components to the enclosing panel. Add spacer components to mimic the insets of the panels in the layout. You will also need to switch off "optimised drawing", or some such, for overlapping components.


I've read suggestions about using JSeparator rather than multiple JPanels with borders, if aligning widgets is your priority.

JSeparator gives the visual effect of grouping widgets, but themselves are just another simple widget fitting into the same JPanel. Read the tutorial for important tips such as setting the preferred size. http://download.oracle.com/javase/tutorial/uiswing/components/separator.html

Also, Apple's current guidelines suggest simply using empty space as a divider between groupings, as an alternative to borders and separators.


This sample code can help to fix your alignment problem:

//c is instance of the content pane
c.setLayout(gl=new GridLayout(3,0));
//jp is a jpanel. I took this panel because there is no alignment
//setting for a specifiq cell of grid Layout.
jp.setAlignmentX(CENTER_ALIGNMENT);
//jl is the jLabel
jp.add(jl);
//finally I add that with the frame
c.add(jp);

Hope it will help you to solve your issue.

0

精彩评论

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

关注公众号