Content

GO TO THE GERMAN SITE

Softhema Group [www.softhema.de]

Examples
Softhema Java Toolkit Collection Version 1.2

Examples
In the JTC-API is in the subdirectory samples a collection of Java source code examples. For the call of the examples you should have a JRE in the version 1.2 or better. Install the JTC-API in your JRE (see installation). For executing the examples you must call the Java interpreter java.exe with the appropriate file name of the example without an extension.

Here are the necessary steps:

  1. Open a MS-DOS-shell under Windows.
    Open a command-shell under Unix .
  2. Select the directory, in which the class file is located.
    Windows: cd .\samples\scanf
    Unix: cd ./samples/scanf
  3. Start the Java program by calling the Java interpreter.
    Windows: java.exe Samples1
    Unix: java Samples1
In the following paragraph some source code samples for different Sub-APIs are specified. The source code samples gives an overview of the available functions. Not all fields of application of the JTC-API examples are presented here. A complete list of the available functions is found in the Java API documentation. There, each function is explained in detail.

Sub API ToolkitArray
Just as the name suggests, This sub-API manipulates Java arrays. It serves to supplement the incomplete methods in the class java.util.Arrays.

The method elementsMix, mixes cards, just as it is customary in computer games.
The following example demonstrates mixing colors:

            import softhema.system.toolkits.ToolkitArray;
            ...
            String[] as = new String[5];
            as[0] = "red";
            as[1] = "yellow";
            as[2] = "blue";
            as[3] = "orange";
            as[4] = "white"; 
			
            //mix it
            ToolkitArray.elementsMix( as );
			
            for( int i = 0 ; i < 5 ; i++ )
              System.out.println( as[ i ] );
            
Sub API ToolkitAWT
AWT is not dead yet. For the programming of applets it is still recommended to use AWT instead of Swing. This is justified with the fact that there are still many webbrowsers with the JRE 1.1 in the internet. In this version swing was not yet a build-in component of the Java core class library. The class ToolkitAWT contains useful extensions for the AWT. The example presented here enables to modify the text of a selected item of a Choice control element.

  void choiceSelect_itemStateChanged(ItemEvent e)
  {
      txtSelect.setText( choiceSelect.getSelectedItem() );
  }

  void txtSelect_textValueChanged(TextEvent e)
  {
      ToolkitAWT.ChoiceItemReplace( choiceSelect,
                                    choiceSelect.getSelectedIndex(),
                                    txtSelect.getText(),
                                    true
                                   );
  }


Applet example for the method ChoiceItemReplace in the class ToolkitAWT
Applet example for the methods ListItemsSelectedMoveUp and ListItemsSelectedMoveDown in the class ToolkitAWT

Sub API ToolkitCollection
The class ToolkitCollection contains extensions to the Java Collection Framework. This way, the predecessor "java.util.Vector" of the Java Collection Framework is supported.
   import softhema.system.toolkits.ToolkitCollection;
   ...
   try
   {
     Vector vec = new Vector();
     vec.addElement( "red" );
     vec.addElement( "yellow" );
     vec.addElement( "blue" );
     vec.addElement( "orange" );
     vec.addElement( "white" );

     //reverse order
     ToolkitCollection.listReverseOrder( vec );
    
     //print new order
     ToolkitIO.printf("\nnew order:\n%s\n%s\n%s\n%s\n%s\n", vec);
   }
   catch( IOException e )
   {
     e.printStackTrace();
   }
Sub API ToolkitFile
The method listFiles of the class java.io.File lists only the files and sub directories of the next level. In order to determine all files of a directory, a recursive algorithm is necessary. The method listFilesRecursive of the sub API ToolkitFile returns an array of file objects, which contains all subordinated files.

     
     Vector v = new Vector();

     ToolkitIO.printf("%s", "Input a directory:");
     ToolkitIO.scanf("%ls", v);
     
     String sDirectory = (String) v.elementAt(0);
     File dir = new File( sDirectory );

     File[] aFilesAndSubDirs = ToolkitFile.listFilesRecursive( dir, true );
     File[] aFilesOnly = ToolkitFile.listFilesRecursive( dir, false );

     int countFiles = aFilesOnly.length;
     int countDirs = aFilesAndSubDirs.length - countFiles;
        
     ToolkitIO.printf("In the directory \"%s\" are %i files and %i subdirectories.\n",
	                   sDirectory, new Integer(countFiles), new Integer(countDirs) );     
    
Sub API ToolkitIO
In this toolkit are functions for input or output operations. Inputs are obtained from System.in. Outputs are written to System.out.

Conversion from Fahrenheit to Celsius:

            import softhema.system.toolkits.ToolkitIO;
            ...
					
            try
            {
               Vector v = new Vector();
     
               ToolkitIO.printf("\n%s", "Input the temperature in Fahrenheit:");
               ToolkitIO.scanf("%f", v );
     
               float fahrenheit = ( (Number) v.elementAt(0) ).floatValue();
               float celsius = (5.0f / 9.0f) * (fahrenheit - 32);

               v.addElement( new Float( celsius ) );

               ToolkitIO.printf("%.2f Fahrenheit is %.2f Celsius", v);
            }
            catch( IOException e )
            {
               e.printStackTrace();
            }
				  
		

Applet example for the method printf in the class ToolkitIO
Applet example for the method scanf in the class ToolkitIO

Sub API ToolkitString
ToolkitString supplies additional functions for analysis and manipulation of a strings. The method sprintf writes its output into a string, instead to a display. Comparing a string with a pattern can easily be executed by the method isLike.

      ToolkitIO.printf("\nInput a password:");
      String pwd = ToolkitIO.readStringLine();

      if( ToolkitString.isLike( pwd, "*s*s*" ) )
      {
        //The password must contains two s-characters.
        ToolkitIO.printf("\nThe password %s is correct.", pwd);
      }
      else
      {
        ToolkitIO.printf("\nThe password %s is not correct.", pwd);
      }	   
	   

Applet example for the method isLike in the class ToolkitString
Applet example for the method strReplaceLike in the class ToolkitString
Applet example for the method subStringLike in the class ToolkitString

Sub API ToolkitSwing
With Swing, efficient user interfaces can be created. Nevertheless to implement some functions in swing is with great difficult. The Toolkit API contains functions for extending control elements contained in Swing. Additionally some help functions are contained.

  void jBtnMoveUp_actionPerformed(ActionEvent e)
  {
     ToolkitSwing.ListItemsSelectedMoveUp( jList );
  }

  void jBtnMoveDown_actionPerformed(ActionEvent e)
  {
     ToolkitSwing.ListItemsSelectedMoveDown( jList );
  }
Contents