¿Cómo puedo sangrar cuidadosamente algunos componentes usando diseños de Java Swing?

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 Options->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.

preguntado el 08 de enero de 11 a las 19:01

4 Respuestas

Hay varias maneras de hacer esto:

  1. Set each components border to: new EmptyBorder (0, 10, 0, 0).

  2. Utilizar GridBagLayout y usa un Inset (0, 10, 0, 0) to pad the left side.

  3. Utilizar GridBagLayout and have the main checkbox span two columns, whilst the sub checkboxes are offset by placing them in the rightmost column.

  4. 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.

Respondido 09 Jul 15, 05:07

Create a JPanel for the sub components. Then you can add an EmptyBorder to the panel with the required indentation.

Respondido el 08 de enero de 11 a las 22:01

Way easier for simple fixes than the GridBagLayout, and it won't mess with the focus indicator like an EmptyBorder haría. - 2rs2ts

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.

Respondido el 08 de enero de 11 a las 22:01

Another option is to use SpringLayout and add padding.

Respondido el 08 de enero de 11 a las 23:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.