Java Box create filler

Introduction

A filler is an invisible component with minimum, maximum, and preferred sizes.

The Filler static nested class from the Box class represents a filler.

import java.awt.Dimension;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Main {
  public static void main(String[] args) {
    JFrame frame = new JFrame("FlowLayout Nesting");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Box hBox = Box.createHorizontalBox();
    hBox.add(new JButton("First"));

    // Create a filler, which acts like a glue. 
    // the glue is just a
    // filler with a minimum and preferred size set to zero
    // and a maximum size set to
    // Short.MAX_VALUE in both directions
    Dimension minSize = new Dimension(10, 10);
    Dimension prefSize = new Dimension(10, 10);
    Dimension maxSize = new Dimension(Short.MAX_VALUE, Short.MAX_VALUE);
    Box.Filler filler = new Box.Filler(minSize, prefSize, maxSize);

    hBox.add(filler);/*from   w  ww .ja  v  a  2 s .  c om*/
    
    hBox.add(new JButton("Last"));
    // Add JPanel to the content pane
    frame.getContentPane().add(hBox);

    frame.pack();
    frame.setVisible(true);
  }
}



PreviousNext

Related