0

I am having trouble aligning my components in my panel.

Currently, it looks like this:

|system...power|

|fuel...................|

|(slider)..............|

|go.....................|

I want it to look like this (with fuel, slider, and go all horizontally aligned in the middle):

|system...power|

|..........fuel.........|

|.......(slider)...... |

|.......... go......... |

(Forgive me for the confusing layout of these examples, I do not have the reputation to post images)

The code corresponding to the layout is this:]

layout.setVerticalGroup(
            layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup()
                    .addComponent(systems_box)
                    .addComponent(fuelamount)   
                    .addComponent(power_box))
            .addComponent(fuel_input)
            .addComponent(gobutton)     
    );

    layout.setHorizontalGroup(
            layout.createSequentialGroup()
            .addComponent(systems_box)
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
                    .addComponent(fuelamount)
                    .addComponent(fuel_input)
                    .addComponent(gobutton))
            .addComponent(power_box)
    );

I am wondering if it is possible to make it how I want it to look using GroupLayout? I also don't want to just have the system and power on each end of the fuel component, as it is a fairly long component.

Thanks in advance :)

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Is it a ready-made code from netbeans ? – CoderNeji Jun 26 '15 at 07:06
  • IMHO Avoid `GroupLayout` if you're coding by hand, use MigLayout or `GridBagLayout` or just about anything else in combinations of. `GroupLayout` is not really "hand" friendly and is really meant for form editors :P – MadProgrammer Jun 26 '15 at 07:18
  • Ah okay, thanks for the reply, I will try out a few others :) – Aaron Barnard Jun 26 '15 at 07:47
  • If you're still trying to do this please post a complete example. It's not clear what type of components fuelamount, fuel_input, etc are and that could affect the alignment. However, I also noticed that while you're setting the alignment to center on your horizontal group, you are not doing that on the vertical group - which from your "diagram" it appears that those are the ones you want centered. You may also consider using BoxLayout as it naturally aligns things along the horizontal or vertical axis. – Amber Jun 26 '15 at 14:08
  • Hi Amber, I managed to sort it out by making an upper and a lower panel and using GroupLayout for their alignment and the alignment of their components, and it worked really well :) – Aaron Barnard Jun 28 '15 at 03:21

1 Answers1

0

To overcome this, I ended up creating an upper and lower panel which were aligned using GroupLayout and then used GroupLayout again to align their corresponding components. Although it may be a bit unnecessary to do it this way, I'm glad I managed to work out how it is possible using only GroupLayout.