Question

I trying to learn how to use Java Swing's GroupLayout.

First of all I just want to make a grid made from JLabels.

The problem I'm having is that the JLabels are being rendered directly on top of each other (i.e. in exactly the same spot so that one obscures the other).

Below is my code for what should be 3 JLabels in a column:

layout.setHorizontalGroup(
    layout.createSequentialGroup()
        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(one)
            .addComponent(two)
            .addComponent(three))
);
layout.setVerticalGroup(
    layout.createParallelGroup(GroupLayout.Alignment.LEADING)
        .addComponent(one)
        .addComponent(two)
        .addComponent(three)
);

How do I get the labels to be positioned correctly.

Thanks

Was it helpful?

Solution

In the vertical layout you are also building a parallel group which should be a sequential one instead (you wan't distinct rows and not all in one).

layout.setVerticalGroup(
    layout.createSequentialGroup()
    .addComponent(one)
    .addComponent(two)
    .addComponent(three)
);

Remark: For this example you also do not need the sequential group in the horizontal layout.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top