Wednesday, March 5, 2008

[Harmless Hacks] Hacking Servoy's Frame Title & Icon


This post is only available in PDF format. You can download it here.


Have you ever wanted to change the title or the icon image when you deploy your Servoy solution?

In this blog post I will show you how it can be done using some Java knowledge...


The Servoy Code

//2008.March.01
/*
Sets the top level window title and/or icon.
Author: T. Parry, Prospect IT Consulting Inc, venture@compmore.net

Ensure this is called after servoy has finished messing with the title.

Default image is/was greek letter psi: 'd:/temp/icons/images/Math/psi.gif';
Default title is/was: 'IT Service & Support Toolkit by Prospect IT Consulting'

The place where it is best to use this is...
*/
var myTitle = null;//no change
var myIconImageLoc = null;//no change
if (arguments && (arguments.length > 0)) {
if ( arguments[0] ) {
myTitle = arguments[0];
}
if (arguments[1]) {
var myIconImageLoc = arguments[1];
}

var frames = Packages.java.awt.Frame.getFrames();
if (myTitle) {
var prevTitle = frames[0].getTitle();//save it or do something with it
frames[0].setTitle(myTitle);// [0] is the top most frame - set a new title
}
if (myIconImageLoc) {
//var myIconImage = new Packages.java.awt.image.BufferedImage;

var kit = new Packages.java.awt.Toolkit.getDefaultToolkit();
var imageToUse = kit.getImage(myIconImageLoc);
frames[0].setIconImage(imageToUse);
}
}//if arguments

return;

The Servoy Code (Using an Image From the Media Library)

frames[0].setIconImage(new Packages.javax.swing.ImageIcon(new Packages.java.net.URL("media:///my_image.gif")).getImage());

IMPORTANT: Please be advised that you should always backup your Servoy solution before implementing or attempting any of the techniques described in this blog. None of the techniques described are guaranteed to work in any way shape or form, or for any specific purpose. Use them entirely at your own risk.

5 comments:

Data Mosaic said...

Great tip!

Mike Sedita said...

When I try to get the frames with:

var larrframes = new Packages.java.awt.Frame.getFrames();

I get error:

[JavaPackage java.awt.Frames.getFrames] is not a function.

What am I doing wrong?

Tom Parry said...

Hello Mike,
First of all recognize that the getFrames method is a "static" method and therefore does not require the "new" in front. The method actually returns an array.

Secondly when I did try it with a new I did not have any problem. So I suspect that perhaps that you do not have the Java Development Kit in your classpath when you are running Servoy Developer.

I tested this out using jdk 1.6.0_05 and Servoy 3.5.5

Regards,
Tom

Jeff Bader said...

Hi Mike,

I assume that you have already tried copying Tom's code exactly as it is in the blog entry into your Servoy solution?

If not, try the "copy to clipboard" link above the code in the post and give his code a shot as is.

Mike Sedita said...

OK - not only did I not have the JDK in the classpath, it wasn't even installed. As you may surmise, I'm new to Java. I downloaded the JDK, made sure the classpath pointed to it, and now it works fine. Thanks for all the help.

Post a Comment