Dynamically load laf
- From: Daniel <daik.no-spam@xxxxxxxxxxxxxxxxx>
- Date: Mon, 28 Nov 2005 07:01:49 GMT
Hello everyone!
I am writing an application and I would like to give the user the
option of choosing Look and Feel.
The look and feels that are part of standard java is not that exciting
so I would like to give the user the option of "installing" new look
and feels without recompile or anything.
My idea was to have a directory say lib/laf where the user would put a
the look and feel with the full name as filename say
"net.sourceforge.mlf.metouia.MetouiaLookAndFeel.jar"
Thus I would add that directory to my classpath and all would be
peaches. Well not quite. It seems this is not really working. So I
wrote my own classloader trying to solve this problem. But my
classLoader does not seem to work. (code included last) so i wonder if
anyone has any good ideas on how to solve this. Not neccerily using a
custom classloader, any other ideas are welcome.
regards
Daniel
ps. I searched for this on both google and deja, but got no good hits,
so maybe my keywords were poorly chosen, so if anyone has any websites
dealing with this problem i appreciate links.
code
public class LaFClassLoader
extends ClassLoader {
public LaFClassLoader() {
}
public Class findClass(String name) throws ClassNotFoundException {
File dir= new File("lib/LaF");
if(dir.exists()){
File[] lafs= dir.listFiles();
for(int i=0;i<lafs.length;i++){
if (lafs[i].getName().endsWith(".jar")) {
try{
ZipInputStream zin = new ZipInputStream(new
FileInputStream(lafs[i]));
ZipEntry entry;
while ( (entry = zin.getNextEntry()) != null) {
if (entry.isDirectory()) {
continue; // since this entry is just a directory
}
String s=entry.getName();
String tname=name.replace('.','/')+".class";
if (entry.getName().equals(tname)) {
long size = entry.getSize();
if (size != -1) {
byte[] clas = new byte[ (int) size];
zin.read(clas, 0, clas.length);
return super.defineClass(name, clas, 0, clas.length);
}
}
}
}catch(Exception e){
throw new ClassNotFoundException(name,e);
}
}
}
}
throw new ClassNotFoundException(name);
}
}
problem I get with this classloader is on the super.defineClass line
saying
"net/sourceforge/mlf/metouia/MetouiaLookAndFeel" contains invalid
UTF-8 character.
.
- Follow-Ups:
- Re: Dynamically load laf
- From: Thomas Hawtin
- Re: Dynamically load laf
- From: Roedy Green
- Re: Dynamically load laf
- Prev by Date: menonic without alt key (or similar)
- Next by Date: Re: Close a window
- Previous by thread: menonic without alt key (or similar)
- Next by thread: Re: Dynamically load laf
- Index(es):
Relevant Pages
|