How do I neatly indent some components using Java Swing layouts
Using Swing, what is the best way to indent some components underneath a checkbox or radio button? I need to make something in the style of Firefox 3.6's Opti开发者_StackOverflow中文版ons->Privacy dialog where some checkboxes are indented under a "main" checkbox. I can use any of the standard AWT/Swing layout's including GroupLayout. I also have JGoodies FormLayout available to me. I tried using setLeadingColumn offset in FormLayout at first, but it seemed like it was not going to work well unless I was indenting under a Separator. Maybe I was just doing it wrong?
Is there anything like SWT GridLayout's horizontalIndent setting? That would be perfect.
I am working with JDK1.6.0_23.
Create a JPanel for the sub components. Then you can add an EmptyBorder to the panel with the required indentation.
There are several ways to do this:
Set each components border to:
new EmptyBorder (0, 10, 0, 0)
.Use a
GridBagLayout
and use anInset (0, 10, 0, 0)
to pad the left side.Use a
GridBagLayout
and have the main checkbox span two columns, whilst the sub checkboxes are offset by placing them in the rightmost column.Supply custom checkbox icons that have some empty space added to their left hand edges.
etc.
My advice would be to learn the GridBagLayout
- it is somewhat unwieldy to use but it does give you pretty much all the layout power you could want. The JGoodies stuff is useful for when you want particular automatic column sizing behaviour that GBL won't give you without some additional code on your part.
You should be able to accomplish this just fine with FormLayout, just add another column for the sub items, and have the main item span 2 columns.
Another option is to use SpringLayout and add padding.
精彩评论