Java Swing BoxLayout newBoxForComponent(Component c)

Here you can find the source of newBoxForComponent(Component c)

Description

Create a box with a coponent aligned to left.

License

Apache License

Parameter

Parameter Description
c component

Return

Box with the compoenent

Declaration

public static Component newBoxForComponent(Component c) 

Method Source Code


//package com.java2s;
/*//w w w . j  a  v a2s . c  o  m
 * Copyright 2008-2011 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.awt.Component;

import javax.swing.Box;

public class Main {
    /**
     * Create a box with an aligned component using Component constants for right, center and left.
     * @param c component
     * @param alignment aligment.
     * @return Box with compoenent
     */
    public static Component newBoxForComponent(Component c, float alignment) {
        Box box = Box.createHorizontalBox();
        if (Component.RIGHT_ALIGNMENT == alignment) {
            box.add(Box.createHorizontalGlue());
            box.add(c);
        } else if (Component.CENTER_ALIGNMENT == alignment) {
            box.add(Box.createHorizontalGlue());
            box.add(c);
            box.add(Box.createHorizontalGlue());
        } else {
            // default to left
            box.add(c);
            box.add(Box.createHorizontalGlue());
        }

        return box;
    }

    /**
     * Create a box with a coponent aligned to left.
     * @param c component
     * @return Box with the compoenent
     */
    public static Component newBoxForComponent(Component c) {
        return newBoxForComponent(c, Component.LEFT_ALIGNMENT);
    }
}

Related

  1. createVerticalBoxLayout(Component... components)
  2. execLoop(JComponent editor)
  3. hbox(Component[] components, int spacing)
  4. makeVerticalBoxPanel(int margin)
  5. newBox(String name, String... items)
  6. show(JComponent comp, String windowName)
  7. show(JComponent component)
  8. vbox(Component c1, Component c2)
  9. xBoxLayoutPanel(Component... components)