views:

112

answers:

1

With Netbeans I've succeded center a jpanel with fixed size, within an other jpanel. Now I can't repeat it - only copy it.

How did I do? (or should I do to center x and y a jpanel with fixed size in another jpanel).

The result differs in code:

Working - search for .addContainerGap( and see next not working:

        javax.swing.GroupLayout center3Layout = new javax.swing.GroupLayout(center3);
        center3.setLayout(center3Layout);
        center3Layout.setHorizontalGroup(
            center3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 1064, Short.MAX_VALUE)
            .addGroup(center3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(center3Layout.createSequentialGroup()
                    .addContainerGap(30, Short.MAX_VALUE)
                    .addComponent(mainPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 
javax.swing.GroupLayout.DEFAULT_SIZE, 
javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(30, Short.MAX_VALUE)))
        );
        center3Layout.setVerticalGroup(
            center3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 650, Short.MAX_VALUE)
            .addGroup(center3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(center3Layout.createSequentialGroup()
                    .addContainerGap(23, Short.MAX_VALUE)
                    .addComponent(mainPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(23, Short.MAX_VALUE)))
        );

Not working - search for .addGap compared with above working.

    javax.swing.GroupLayout center2Layout = new javax.swing.GroupLayout(center2);
    center2.setLayout(center2Layout);
    center2Layout.setHorizontalGroup(
        center2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 1073, Short.MAX_VALUE)
        .addGroup(center2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(center2Layout.createSequentialGroup()
                .addGap(0, 34, Short.MAX_VALUE)
                .addComponent(mainPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 35, Short.MAX_VALUE)))
    );
    center2Layout.setVerticalGroup(
        center2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 654, Short.MAX_VALUE)
        .addGroup(center2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(center2Layout.createSequentialGroup()
                .addGap(0, 25, Short.MAX_VALUE)
                .addComponent(mainPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 25, Short.MAX_VALUE)))
    );

I've looked side by side in properties etc - please help! :)

+1  A: 

I can't reproduce as well a way to center a JPanel in another, in Netbeans (using the GroupLayout, so). If someone can find it, I would be glad to know as well.

However, I can advise you to change the layout of your outer panel to a GridBagLayout (you can do this in Netbeans as well with "setLayout" in the contextual menu).
The default GridBagConstraints should be exactly what you need, the inner panel will be centered, with its preferred size. So you shouldn't actually need to dive into specifying them yourself.

Gnoupi