Archive for the 'Java' Category

Absolute Link

Tuesday, June 3rd, 2008

This is a fantastic link for finding information about EJB and Hibernate Annotations. So thanks to the guys/gals that produced it:

Link to Content

Applet File Drop

Tuesday, June 3rd, 2008

I needed to make a way for a person to drag a file off of the desktop and onto the browser using a java Applet. And this is the code to make that happen:

public class SomeApplet extends JApplet implements DropTargetListener {

The first step is to implement the DropTargetListener as you extend JApplet (or Applet). This is going to provide the applet the ability to contain a Drop Target. That is right, I said “contain”. I have tried this attaching the Drop Target Listener to the Applet itself unsuccessfully however; it attaches fine to a JPanel or even a Label.

JPanel jp1 = new JPanel();
DropTarget dt2 = new DropTarget(jp1, this);
jp1.setDropTarget(dt2);

Next declare the panel and a Drop Target then attach the drop target to the panel (once again this could be done with a label or whatever).

public void drop(DropTargetDropEvent dtde) {
int action = dtde.getDropAction();

dtde.acceptDrop(action);

fromTransferable(dtde.getTransferable());

dtde.dropComplete(true);
}

First you get the Drop Action from the dtde object which represents the Drop Target Event then accept the Drop using the acceptDrop function. This registers the accepted drop with the applet. Once you accept then you dispatch the function which handles the drop (uploading the file in our case) then tell the applet the drop is complete using the dropComplete function. This is a great place for error handling to return false if there is an error or true if the drop function was successful. Both will have different display consequences which can be seen through testing ;-)

Object data = t.getTransferData(DataFlavor.javaFileListFlavor);
java.util.List<File> fileList = (java.util.List<File>) data;

for (File file : fileList) {
if (!is_in_list(file_names, file.getAbsolutePath()) && !file.isDirectory()) {

Lastly we cycle through the list of files we got and decide whether to save them or what to do.

And there you have it: a medium level overview on how to drop a file into the browser on an applet and have it upload.

Applet and Javascript

Friday, May 30th, 2008

So you have an applet and you want to do one or both of the following things:

  1. Run javascript commands from within your applet
  2. Control your applet through javascript

Step 1 Setup the Object Tag:

<object type=”…”>
<param name=”mayscript” value=”true”>
<param name=”scriptable” value=”true”>
</object>

*Note - I did not show the archive, code or other params needed to setup an object as they are dependent on your applet.
*Note - This setup allows for javascript communication in and out of your applet

Step 2 From within the java call some javascript

import javascript.*;
….
JSObject win = JSObject.getWindow(this);
win.eval(”alert(’test’);”);

*Note - To make this work you need to add plugin.jar from within your jdk/lib folder in the project classpath and include it in your deployment (archive = “…, plugin.jar”).

And tada you can call java functions (yes the public ones) from javascript just by referencing the id of the object (dot) the function name. The translation between object types can get a bit tricky if you try to do a map or hash but with strings, ints and the like its pretty straight forward. The primary use for this was in setting up an event and using javascript to trigger it on submission of a form however the possibilites are endless ;-)

Oracle Blob (JDBC) - How to

Tuesday, May 27th, 2008

This morning I needed to insert a file’s contents into a blob on an oracle database so here is the method used to do that (look for a future post on dropping a file into an applet).

/* File Loaded here into a File Object as f */
PreparedStatement ps = conn.prepareStatement(”INSERT INTO TBL VALUES(?, ?)”);
ps.setString(1, f.getName());
FileInputStream fis = new FileInputStream(f);
ps.setBinaryStream(2, fis, fis.available());
ps.execute();
ps.close();

And tada your record will be loaded into the blob.

*** Not Shown ***
Oracle JDBC Connection - This example does not use any framework/connection helper (i.e. Hibernate/EJB)

SCJA

Friday, May 23rd, 2008

As most know I am studying to get my SCJA and making a much bigger deal out of it than it should be.  I have been through all the test prep books several times and am confident with the material however I find some odd aversion to taking the test.  I guess all of us experience fear of failure and more so when we work with the technology every day ;-).  Oh well I am going to purchase my exam ticket tomorrow and then just finally set a date to go take the thing.  Below is the link to find out more about the SCJA if needed.

http://www.sun.com/training/certification/java/scja.xml