Skip to main content


Showing posts from February, 2009

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

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 ( "THRE

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 * ; 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 ) ;         

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 ){