BoxLayout in Java Swing

Box layout arranges the components either horizontal or vertical.  javax.swing package  has a class BoxLayout   that is used to create the BoxLayout managers and Boxes. You use many classes to arrange the components in more sophisticated way. Several supporting classes are typically used javax.swing.BoxLayout, javax.swing.Box, and javax.swing.Box.Filler.  

there is following coonstructor to create the BoxLayout instance.
BoxLayout(Container target, int axis)
It creates a layout manager that will lay out components along the given axis. Here target is the container’s reference on which this Layout has to be applied and axis is the integer constant specifying the y-axis or x-axis.

here Box is the another lightweight container that uses a BoxLayout object as its layout manager. Box provides several class methods that are useful for containers using BoxLayout -- even non-Box containers. To create an instance of this container we simply pass the desired alignment to its constructor. To create an instance of this container we simply pass the desired alignment to its constructor.

There are some filler components used to fill the space between components. These are non visible empty components used to cover the specified space from the container. 

The Box class provides convenient static methods for the creation of three different variations: glue, struts, and rigid areas.

createHorizontalGlue(), createVerticalGlue(): returns a component which fills the space between its neighboring components, pushing them aside to occupy all available space (this functionality is more analogous to a spring than it is to glue).

createHorizontalStrut(int width), createVerticalStrut(int height): returns a fixed-width (height) component which provides a fixed gap between its neighbors.

createRigidArea(Dimension d): returns an invisible component of fixed width and height.

import javax.swing.*;
import java.awt.*;

class SwingDemo5
{
    public static void main(String ar[])
    {
        JFrame f=new JFrame("New Frame");
        Container c=f.getContentPane();
        
        Box b1=Box.createVerticalBox();
        //Box b1=Box.createHorizontalBox();
        b1.add(Box.createVerticalGlue());
        JPanel p1=new JPanel();p1.setBackground(new Color(134,200,200));
        b1.add(p1);
        
        JPanel p2=new JPanel();p2.setBackground(new Color(200,134,255));
        b1.add(p2);
        b1.add(Box.createVerticalStrut(20));
        JPanel p3=new JPanel();p3.setBackground(Color.gray.darker());
        b1.add(p3);
        
        c.add(b1);
        
        f.setBounds(100,100,400,300);
        f.setVisible(true);
    }
}

GridLayout in Java Swing


GridLayout place the components into rectangular grid. Space of container is divided into rows and columns and components are place at the particular cell.

It has following constructors
GridLayout()
It creates a layout with one column per component. Only one row is used.
GridLayout(int rows, int cols)
It creates a layout with the given number of rows and columns.
GridLayout(int rows, int cols, int hgap, int vgap)
It creates a layout with the given number of rows and columns, and the given size of horizontal and vertical gaps between each row and column.

import java.awt.*;
import java.applet.*;

/* 
 <applet code="GridLayoutDemo1" width=400 height=200>
 </applet>
 */
public class GridLayoutDemo1 extends Applet
{
    public void init()
    {    
        
        setLayout(new GridLayout(3,2));
         add(new Button("ONE"));
         add(new Button("TWO"));
         add(new Button("THREE"));
         add(new Button("FOUR"));
         add(new Button("FIVE"));
         add(new Button("SIX"));
    }
}

Java Program to get System properties.

List all the System properties.

class SysProperties{
    public static void main(String ar[]){
        java.util.Properties p=System.getProperties();
        java.util.Enumeration names=p.propertyNames();
        while(names.hasMoreElements()){
            String name=(String)names.nextElement();
            String value=p.getProperty(name);
            System.out.println(name+"\t=\t"+value+"\n");
        }
    }
}

Combine all source files of java program into single file without duplicate entry of contents into file.

This is the program that writes all java source program to single file without duplicate entry of contents into file.
/*
 This is the program that writes all java source program to single file without duplicate entry
 of contents into file.
 */

import java.io.*;
import java.util.*;

public class CombineJava4{
    static int count=0;
    static Check c=new Check();
    static FileOutputStream fos;
    static ArrayList<File> written=new ArrayList<File>();
    static File destFile;
    public static void main(String ar[])throws Exception{
        
        Scanner scan=new Scanner(System.in);
        System.out.println("Enter source Directory");
        String dir=scan.nextLine();
        System.out.println("Enter the Destination file name with ext.");
        String dest=scan.nextLine();
        destFile=new File(dest);
        //path of file in which data to be written
        fos=new FileOutputStream(dest);
        //PASS THE PATH OF DIRECTORY    
        combine(dir);
        fos.close();
        System.out.println("\nFinished");   
    }
    /**
     *this method will search file into directory recurcivaly
     */
    public static void combine( String dir)throws Exception{
    
        File f=new File(dir);
        String list[]=f.list();
        
        for(int i=0;i<list.length;i++){
            String filename=list[i];
            File f2=new File(f,filename);
            if(!f2.isFile()){
                System.out.println(f2.getPath());
                combine(f2.getPath());
            }else{
                if(f2.getName().endsWith(".java")&(!f2.getPath().equals(destFile.getPath()))){
                    if(!c.duplecate(written,f2)){
                        count++;
                        System.out.println("\tWriting  file ="+f2.getName());
                        writeToFile(f2);
                    }
                    //else
                        //System.out.println("Duplecate file ="+f2.getName());
                }
            }
        }    
    }
    /**
     *This method will write contend of file passing as parameter to output stream object [fos]
     */
    public static void writeToFile(File name) throws IOException{
        FileInputStream fis=new FileInputStream(name);
        //FileOutputStream fos=new FileOutputStream("E:/Tatal.java",true);
        //fos.write(new String("\n<<<<<<<<<<<<[ "+count+"-"+name.getPath()+" ]>>>>>>>>>>>>>>\n").getBytes());
        fos.write(new String("\n==================[ "+count+"-"+name.getName()+" ]====================\n").getBytes());
        int x=0;
        while((x=fis.read())!=-1){
            fos.write(x);
        }
        written.add(name);
    }
}
class Check{
    boolean isSame(File f1,File f2)throws Exception{
        boolean same =true;
        InputStreamReader isr1=new InputStreamReader(new FileInputStream(f1));
        InputStreamReader isr2=new InputStreamReader(new FileInputStream(f2));
        BufferedReader br1=new BufferedReader(isr1);
        BufferedReader br2=new BufferedReader(isr2);
        String s1,s2;
        while((s1=br1.readLine())!=null){
            s2=br2.readLine();
            //System.out.println("\t"+s1+"\n\t"+s2);
            if(!s1.equals(s2)){
                same=false;
                //System.out.println(f1.getPath()+"  is same as  "+f2.getPath());
                return same;
            }
        }
        return same;
    }
    
    boolean duplecate(ArrayList<File> list,File file)throws Exception{
        boolean exist=false;
        for(File f:list){
            if(f.getName().equals(file.getName()))
            {
                //System.out.println(f.getName()+" matches ");
                if(isSame(f,file))
                    return true;
            }
        }
        return exist;
    }
}


Java Generic Examples Source Code

/Simple generic class
class A<T>{
T obj;
T get(){
return obj;
}
void set(T o){
obj=o;
}
}

class Gen1{
public static void main(String ar[]){
//Creating object of Generic class
A<Integer> a1;//reference variable declaration
a1=new A<Integer>();//object creation
a1.set(123);//Autoboxing is used to convert int type to Integer type
int i=a1.get();//Auto un-boxing is used to convert Integer type to int type
System.out.println(i);
}
}


//Simple generic class
//with two type-parameters
class A<T,K>{
T obj1;
K obj2;

T getObj1(){
return obj1;
}
void setObj1(T o){
obj1=o;
}

K getObj2(){
return obj2;
}
void setObj2(K o){
obj2=o;
}
}

class Gen1{
public static void main(String ar[]){

A<Integer,String> a1;
a1=new A<Integer,String>();
a1.setObj1(123);
int i=a1.getObj1();
System.out.println(i);

a1.setObj2("This is Test");
String s=a1.getObj2();
System.out.println(s);
}
}


//Simple generic class
//Using type-parameter of user defined class
class X{int x=10;}
class Y{int y=20;}
class Z{int z=30;}

class A<T>{
T obj1;
T getObj1(){
return obj1;
}
void setObj1(T o){
obj1=o;
}
}

class Gen3{
public static void main(String ar[]){

A<X> a1=new A<X>();
a1.setObj1(new X());
X objX=a1.getObj1();
System.out.println(objX.x);
}
}


//Simple generic class
//If type-parameter are super type then object of sub classes can be hold
class X{int x=10;}
class Y extends X{int y=20;}
class Z extends X{int z=30;}

class A<T>{
T obj1;
T getObj1(){
return obj1;
}
void setObj1(T o){
obj1=o;
}
}

class Gen4{
public static void main(String ar[]){
A<X> a1=new A<X>();
a1.setObj1(new Y());
Y o1=(Y)a1.getObj1();
System.out.println(o1.y);

A<Y> a2=new A<Y>();
a2.setObj1(new Y());
Y o2=(Y)a2.getObj1();
System.out.println(o2.y);

/* It will give Error
A<Y> a3=new A<Y>();
a3.setObj1(new X());
X o3=(X)a3.getObj1();
System.out.println(o3.x);*/
}
}


//Simple generic class
//Bounded Type
class X{int x=10;}
class Y extends X{int y=20;}
class Z extends X{int z=30;}
class P {int p=40;}
//Here T can be relaced by X of subclass of X
class A<T extends X>{
T obj1;
T getObj1(){
return obj1;
}
void setObj1(T o){
obj1=o;
}
}

class Gen5{
public static void main(String ar[]){
A<X> a1=new A<X>();
a1.setObj1(new X());
X o1=a1.getObj1();
System.out.println(o1.x);

A<Y> a2=new A<Y>();
a2.setObj1(new Y());
Y o2=a2.getObj1();
System.out.println(o2.y);
//Compile time Error
/*A<P> a3=new A<P>();
a3.setObj1(new P());
P o3=a3.getObj1();
System.out.println(o3.p);*/
}
}


//Simple generic class
//Bounded Type
class X{int x=10;}
class Y extends X{int y=20;}
class Z extends X{int z=30;}
class P {int p=40;}
//Here T can be relaced by X of subclass of X
class A<T extends X>{
T obj1;
T getObj1(){
return obj1;
}
void setObj1(A<?> o){
obj1=o;
}
}

class Gen5{
public static void main(String ar[]){
A<X> a1=new A<X>();
a1.setObj1(new P());
X o1=a1.getObj1();
System.out.println(o1.x);
}
}


//Simple generic class
//Wildcard
class X{int val=10;}
class Y extends X{int val=20;}
class Z extends X{int val=30;}
class P {int val=40;}
//Here T can be relaced by X of subclass of X
class A<T>{
T obj;
A(T o){
obj=o;
}
}
class B{
//this method will take argument of type A,
//but it no need to care about that, what type of type-parameter A class will have
void setObj1(A o){
System.out.println(o.obj.getClass().getName());
}
}
class Gen7{
public static void main(String ar[]){
B b=new B();
A<X> a1=new A<X>(new Y());
b.setObj1(a1);
}
}


//Simple generic class
//Bounded-Wildcard
class X{int val=10;}
class Y extends X{int val=20;}
class Z extends X{int val=30;}
class P {int val=40;}
//Here T can be relaced by X of subclass of X
class A<T>{
T obj;
A(T o){
obj=o;
}
}
class B{
//this method will take argument of type A,
//but it no need to care about that, what type of type-parameter A class will have
//here method will accept type-args of type of X or sub-class X
void setObj1(A<? extends X> o){
System.out.println(o.obj.getClass().getName());
}
}
class Gen8{
public static void main(String ar[]){
B b=new B();
A<Y> a1=new A<Y>(new Y());
b.setObj1(a1);
}
}


//Simple generic Method
class A{
<T>void print(T a)
{
System.out.println("Recieved value : "+a);
}
}
class Gen9{
public static void main(String ar[]){
A a=new A();
a.print(100);
}
}


//Simple generic Method with more type-parameter
class A{
<T,E>void print(T a,E b)
{
System.out.println("Recieved value : "+a);
System.out.println("Recieved value : "+b);
}
}
class Gen10{
public static void main(String ar[]){
A a=new A();
a.print(100,"Strig");
}
}


//Simple generic Method with relation b/w more type-parameter
class A{
<T,E extends T>void print(T a,E b)
{
System.out.println("Recieved value : "+a);
System.out.println("Recieved value : "+b);
}
}
class Gen11{
public static void main(String ar[]){
A a=new A();
//a.print(100,"Strig");
//second arg must of type of first arg or subtype of first arg.
a.print(100,200);
}
}


//Simple generic Method with relation b/w more type-parameter
class x{int val=10;}
class y extends x{int val=10;}
class A{
<T,E extends T>void print(T a,E b)
{
System.out.println("Recieved value : "+a);
System.out.println("Recieved value : "+b);
}
}
class Gen11{
public static void main(String ar[]){
A a=new A();
a.print(new x(),new y());
a.print(new x(),new x());
a.print(new y(),new y());
//a.print(new y(),new x());//Compile time error
}
}


//Simple generic Method with relation b/w more type-parameter
//generic return type
class x{int val=10;}
class y extends x{int val=10;}
class A{
<T,E extends T> T print(E a)
{
T var=a;
return var;
}
}
class Gen13{
public static void main(String ar[]){
A a=new A();
int p=a.print(10);
System.out.println(p);
}
}


//Simple generic constructor
class A{
<E> A(E a)
{
System.out.println(a);
}
}
class Gen14{
public static void main(String ar[]){
A a=new A(1);
}
}


//Generic interface
interface Intf<T>{
void print(T a);
}
class A<T> implements Intf<T>{
public void print(T a){
System.out.println(a);
}
}
class Gen15{
public static void main(String ar[]){
A a=new A();
a.print(10.98);
}
}


//Generic interface
interface Intf<T>{
void print(T a);
}
class A<E extends T> implements Intf<T>{
public void print(T a){
System.out.println(a);
}
}
class Gen15{
public static void main(String ar[]){
A a=new A();
a.print(10.98);
}
}


//Generic subclass and supercalss
class A<T>{
T o;
}
//E is own type-parameter of B and T is for Super class
class B<T,E> extends A<T>{
public void print(T a){
o=a;
System.out.println(a);
}
}
class Gen17{
public static void main(String ar[]){
B a=new B();
a.print(10.98);
}
}

Popular Posts