Class loader

Discussion in 'Java' started by Novello, Jul 21, 2005.

  1. Novello

    Novello Guest

    Hello,

    Imagine to have a menu on the left loaded dynamically, each item has its
    own command to run.
    In the rest of window I have a tabPane where I have to load the class I get
    from menu.

    Menu gives me the name of the class to load for example win01
    I have to get the ContentPane of this class and load it inside a new
    TabbedPane

    I tried with forName but I canno tcast it to a JFrame to get the
    contentPane.
    I know that the class I have to load is always a JFrame class.

    Any ideas?

    thank you all!
    Novello, Jul 21, 2005
    #1
    1. Advertising

  2. On Thu, 21 Jul 2005 18:52:24 +0200, Novello wrote:

    [snip description]

    > Any ideas?


    Supply an SSCCE, rather than code descriptions.
    <http://www.physci.org/codes/sscce.jsp>

    --
    Andrew Thompson
    physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
    When You See The Robot, Drink!
    Andrew Thompson, Jul 21, 2005
    #2
    1. Advertising

  3. Novello

    Novello Guest

    Code

    the following method of class Loader loads the class and return it but I
    cannot cast it to a JFrame to get the content, see next code section to
    understand.

    any ideas?

    public Class loadClass() throws Throwable {

    className = MODULES_REPO + className.toLowerCase() + "." +
    className;
    Class[] cArgs = new Class[1];
    cArgs[0] = String.class;

    Class aClass = Class.forName(className);
    Class[] argumentTypes = { java.lang.String.class };
    Constructor oneArgument = null;
    try {
    oneArgument = aClass.getConstructor( argumentTypes );
    } catch (SecurityException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    } catch (NoSuchMethodException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    Object[] arguments = { parm1 };
    try {
    oneArgument.newInstance( arguments );
    } catch (IllegalArgumentException e2) {
    // TODO Auto-generated catch block
    e2.printStackTrace();
    } catch (InstantiationException e2) {
    // TODO Auto-generated catch block
    e2.printStackTrace();
    } catch (IllegalAccessException e2) {
    // TODO Auto-generated catch block
    e2.printStackTrace();
    } catch (InvocationTargetException e2) {
    // TODO Auto-generated catch block
    e2.printStackTrace();
    }
    return aClass;
    }



    Here, where I call the Loader Class above

    Loader loader = new Loader(mItem.getClassName());
    JFrame l = (JFrame) loader.loadClass(); // cast does not work

    mainTabPane.addTab(mItem.getItemName(),icon,l.getContentPane(),mItem.getItem
    Name());

    thank you!
    Novello, Jul 22, 2005
    #3
  4. On Fri, 22 Jul 2005 08:37:32 +0200, Novello wrote:

    > Loader loader = new Loader(mItem.getClassName());
    > JFrame l = (JFrame) loader.loadClass(); // cast does not work


    'ClassLoader.loadClass()' returns byte[]s, not a class.

    See the preamble of the JavaDocs for ClassLoader to see mention
    of 'defineClass' which returns a Class of those bytes and..
    Class.newInstance() for a 'no args' constructed object.

    If you need one of the constructors that uses parameters,
    you may need to use Class methods to retrieve an appropriate
    Constructor and call newInstance() with your params.

    HTH

    --
    Andrew Thompson
    physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
    Dancing Space Potatoes? You Bet!
    Andrew Thompson, Jul 22, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    530
    Andrea Desole
    Dec 14, 2005
  2. =?iso-8859-1?q?Andr=E9_Wagner?=

    Loading a class using the applet class loader

    =?iso-8859-1?q?Andr=E9_Wagner?=, May 30, 2007, in forum: Java
    Replies:
    7
    Views:
    428
    Mark Space
    May 31, 2007
  3. Karsten Wutzke
    Replies:
    0
    Views:
    377
    Karsten Wutzke
    Jun 29, 2007
  4. gorudonu
    Replies:
    0
    Views:
    990
    gorudonu
    Aug 5, 2010
  5. Replies:
    0
    Views:
    148
Loading...

Share This Page