Overblog Suivre ce blog
Editer l'article Administration Créer mon blog
11 septembre 2007 2 11 /09 /septembre /2007 21:10


Here is a Java Bean that allows reading, displaying with scaling and writing images.

(See the last  evolution of this solution here)


This Bean allows scaling images to fit the target item size, and includes a FileChooser with image preview dialog.


This JavaBean connects to the database through the JDBC driver to read and write the images, and does not need the Webutil library to pick up the image files from the JAR file, the client machine files or the Internet.

The Java code

     HandleImage.java     LectureBlob.java        GetImageFileName.java

The implementation class of the Bean Item


The methods you can call

Set the connection string

e.g. :
Set_Custom_Property( 'BL.BEAN', 1, '
SETCONN', 'jdbc:oracle:thin:@my-machine:1521:XE' ) ;   
(Provide the complete jdbc:oracle:thin:... syntax)

Set the username


Set the password


Set the
database image table information


p1 is the table name that holds the image Blob column
p2 is the Blob column name that holds the image

Set_Custom_Property( 'BLZ.BEAN', 1, 'SETTABLEINFO', 'PHOTOS,IMAGE' ) ;  

The instruction above indicates that the PHOTOS table handle images in the IMAGE Blob column..

Read an image from a file


Set_Custom_Property( 'BLZ.BEAN', 1, 'READIMGFILE', 'D:/image.jpg' ) ; 

Read an image from the database table


Set_Custom_Property( 'BLZ.BEAN', 1, 'READIMGBASE', 'IDENTIFIANT=20' ) ;  

where_clause is the SQL clause to identify a unique row to update in the table. The above instruction will be interpreted as :


Put this method into the When-New-Record-Instance block-level trigger

Write an image to the database table


Set_Custom_Property( 'BLZ.BEAN', 1, 'WRITEIMGBASE', 'IDENTIFIANT=20' ) ;  

where_clause is the SQL clause to identify a unique row to update in the table.
The above instruction will be interpreted as :

   Update PHOTOS Set IMAGE = ? Where IDENTIFIANT=20

Put this method into the Post-Insert and Post-Update block-level triggers

Clear the image

Set_Custom_Property( 'BL.BEAN', 1, '
CLEAR' , '' ) ;

This function has to be called in a When-New-Record-Instance trigger if the record does not exist:

When-New-Record-Instance trigger:
If :PHOTOS.IDENTIFIANT is not null Then
   Set_Custom_Property( 'BLZ.BEAN', 1, 'READIMGBASE', 'IDENTIFIANT=12' ) ;
   Set_Custom_Property( 'BLZ.BEAN', 1, 'CLEAR', '' ) ;
End if ;
:BLZ.IMG_SIZE := Get_Custom_Property( 'BLZ.BEAN', 1, 'GETSIZE' ) ;

Set the display image scalling

This function concerns only the image displayed. The image stored with the WRITEIMGBASE method will be the initial image.

Set_Custom_Property( 'BL.BEAN', 1, '
' , 'Width=x,Height=y' ) ;

Both Width and Height parameters must be transmitted

x and y can take the following values:

Any value greater than 0 that express an absolute size in pixels
the special value: FIT that extend to fit the exact size of the image panel' size
-1 that to keep the corresponding aspect ratio
0 that indicates no scale at all


-- Scale the image to fit horizontally and vertically the image item --

Set_Custom_Property( 'BLZ.BEAN', 1, 'SCALE_IMAGE', 'Width=FIT,Height=FIT' ) ;

-- Scale the image to 200x100 pixels --
Set_Custom_Property( 'BLZ.BEAN', 1, 'SCALE_IMAGE', 'Width=200,Height=100' ) ;

-- Scale the image to 200 horizontal pixels and keep vertical aspect ratio --
Set_Custom_Property( 'BLZ.BEAN', 1, 'SCALE_IMAGE', 'Width=200,Height=-1' ) ;

Display/hide the image panel' scrollbars


Set_Custom_Property( 'BLZ.BEAN', 1, 'SETSCROLL', 'true' ) ; 

Set the JFileChooser title and starting directory


Set_Custom_Property( 'BLZ.BEAN', 1, 'SET_FILECHOOSER_TITLE', 'Select a file,D:/' ) ; 

Set the JFileChooser Look and Feel


-- use the current O.S. L&F --
Set_Custom_Property( 'BLZ.BEAN', 1, 'SET_FILECHOOSER_LAF', 'SYSTEM' ) ;

-- use the Metal L&F --
Set_Custom_Property( 'BLZ.BEAN', 1, 'SET_FILECHOOSER_LAF', 'JAVA' ) ;

Set the log mode to output the Bean messages

Set_Custom_Property( 'BLZ
.BEAN', 1, 'SETLOG' , 'true|false' ) ;

In the sample dialog provided, here is the code used in the When-New-Form-Instance trigger:

  -- switch the log to true --
  Set_Custom_Property( 'BLZ.BEAN', 1, 'SETLOG', 'true' ) ;
  -- set the baen image bckground color --
  Set_Custom_Property( 'BLZ.BEAN', 1, 'SETBG', '255,255,255' ) ;
  -- set the JDBC connection information --
  Set_Custom_Property( 'BLZ.BEAN', 1, 'SETCONN', 'jdbc:oracle:thin:@my-machine:1521:XE' ) ;
  Set_Custom_Property( 'BLZ.BEAN', 1, 'SETUSER', 'tutoforms' ) ;
  Set_Custom_Property( 'BLZ.BEAN', 1, 'SETPWD',  'tutoforms' ) ;
  -- set the image table and column name --
  -- Set the JFileChooser title --
  Set_Custom_Property( 'BLZ.BEAN', 1, 'SET_FILECHOOSER_TITLE',  'Select an image file' ) ;
  -- Set the JFileChooser L&F --
  Set_Custom_Property( 'BLZ.BEAN', 1, 'SET_FILECHOOSER_LAF', 'SYSTEM' ) ;
  -- scalling properties --
  Set_Custom_Property( 'BLZ.BEAN', 1, 'SCALE_IMAGE' , 'Width=FIT,Height=FIT' ) ;
  -- display scrollbars policy --
  Set_Custom_Property( 'BLZ.BEAN', 1, 'SETSCROLL',  'false' ) ;

Here is the description of the database table used:

      "NOM" VARCHAR2(50 BYTE),
      "PHOTO" BLOB,

The properties you can get from the JavaBean

Get the image size

Varchar2 :=
Get_Custom_Property( 'BL.BEAN', 1, 'GETSIZE'
) ;

The format returned (width,height) is like the following : 50,120

Get the
selected filename (JFileChooser)

Varchar2 :=
Get_Custom_Property( 'BL.BEAN', 1, 'GET_FILE_NAME'
) ;

This method, both open the JFileChooser dialog box, then returns the selected file.

Accessing to a remote database

Because the JavaBean is executed within an applet, it is not possible to connect to a remote database without a little adaptation:

   - first, the jar file must be signed.
   - second : you have to update the java.policy file stored in the Jinitiator directory

For example, to access to the database located on the machine-name server on the port 1524, add the following lines to the java.policy file:

C:/Program Files/Oracle/JInitiator 1.3.1.xx/lib/security/java.policy file


 permission java.net.SocketPermission "machine-name:1524-", "accept,connect,listen,resolve"; 

permission java.security.AllPermission;

The sample dialog

     . Download the handleimage2.zip file
     . Unzip the file
     . run the create_table.sql under the Oracle user you want to test (It creates the sample image table).
     . copy the handleimage2.jar file in the <ORACLE_HOME>/forms/java directory
     . Edit your /forms/server/formsweb.cfg file to add both handleimage2.jar and classes12.jar (the classes12.jar is located in the <DEV_HOME>/jdbc/lib directory. add a copy of this file in the <DEV_HOME>/forms/java directory).
     . Open the HANDLEIMAGE2.fmb module (Oracle Forms 10.1.2)
     . Compile all and run the module

Note : If you rebuild the jar file, it has to be signed. (the handleimage2.jar file provided in this article is already signed).

Partager cet article

Repost 0
Published by Oracle Forms community - dans Bean - general
commenter cet article


Greg 28/04/2009 21:29

I have added the lines to the Java Policy. Works when forms server and db on same machine but not separate. 1.Would I need to add another line to the  java policy for the forms server?2.Do I need to sign the Jar file even if I have not changed it? Thanks

Oracle Forms community 29/04/2009 07:17

Yes, sign the JAR.

Greg 28/04/2009 17:58

Handleimage2 client server works but cannot read or write to the db when the form moved to server. Our db and app server are on differnt machines.Error:
error connection database : java.security.AccessControlException: access denied (java.net.SocketPermission xxxx1.machine_name.XX-XX resolve)thanks

Oracle Forms community 28/04/2009 21:16

Read the "Accessing to a remote database" paragraph in this article.

jot 20/03/2008 09:46


Oracle Forms community 20/03/2008 10:30

It is, maybe, the reason. I am using the Sung JRE 1.5 that works fine.

jot 20/03/2008 09:20

I try with re-loaded jar file and it gives the same result.I just use HandleImage2.fmb and copy the bean item into a bean2 item. Then i change the code of the readimagefile button : I replace existing code by : Set_Custom_Property('BLZ.BEAN', 1, 'READIMGFILE', 'c:tempimg1.jpg'); Then, when image is loaded, it appears into the two beans ...

Oracle Forms community 20/03/2008 09:41

What plugin do you use?

jot 19/03/2008 16:04

I finnaly found what was wrong in my code.If background (SETBG) is not set for every bean of the form,  each time an image is loaded into a bean, every bean of the form displays this image (it seems that image loaded becomes the backgroung of every image bean).Setting the background at form startup for every bean gives a correct result.Thank you very much for your time and for your nice code, that helps a lot 

Oracle Forms community 19/03/2008 17:13

Try re-loading the JAR file, because I'm able to have the three different images without any background set at all.