How did I center a jpanel in a jpanel with netbeans?
- by Jim Nordlander
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! :)