Asked 7 years ago
9 Feb 2017
Views 2144
sandee

sandee posted

how to use GroupLayout in Java Swing ?

how to use GroupLayout in Java Swing ?

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());


what is meaning of above statement in Java Swing ?

how one can adjust Layout In Java Swing ?
jagdish

jagdish
answered Nov 30 '-1 00:00

to place component like JScrollPane in AWT container object , we need to group it for layout . to group GroupLayout is used , one can configure GroupLayout in horizontal and vertical group.

by java docs ::
GroupLayout


GroupLayout is a LayoutManager that hierarchically groups components in order to position them in a Container.




         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

getContentPane() return
Abstract Window Toolkit(AWT) container object.

GroupLayout take host(container) as argument.

GroupLayout define layout of component , its reponsible for order of component

GroupLayout have setHorizontalGroup and setVerticalGroup method which define the order of the horizontal and vertical.

working example code

  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
         layout.creat
        
         layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 234, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 61, Short.MAX_VALUE))
            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
        );
Post Answer