by Dinesh Thakur Category: Swing

Glue is an invisible component which is similar to a strut but it pushes the components as far away from each other as possible within the bounds of the box itself. You create an invisible component that represents Glue by calling the createGl ue () method for a Box object For example: The statements

b. add (new JButton ("C")) ;

b.add(Box.createGlue());

b.add(new JButton(“C++"));

b.add(new JButton("Visual Basic"));

b.add(new JButton(“Java"));

move the second third and fourth buttons as far away from the first button as possible, based on the height of the box.

import javax.swing.*;

import java.awt.*;
class BoxLayoutFrame extends JFrame
{
     JButton [] Buttons;
     BoxLayoutFrame()
     {
       int i;
       Box b = Box.createVerticalBox();
       b.add(new JButton("C"));
       b.add(Box.createGlue());
       b.add(new JButton("C++"));
       b.add(new JButton("Visual Basic"));
       add(b);
     }
}
   class GlueJavaExample
   {
     public static void main(String args[])
     {
          BoxLayoutFrame frame = new BoxLayoutFrame();
          frame.setTitle("Glue Java Example");
          frame.setBounds(200,250,250,250);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setVisible(true);
     }
   }

GLUE in Java Swing Example

Popular Article