help with passing class interface in getContructor() and newInstance()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ivansyah
    New Member
    • Aug 2011
    • 3

    #1

    help with passing class interface in getContructor() and newInstance()

    Can I passing class interface in getContructor and newInstance().. ..

    Code:
    public interface [B]face[/B]{
      public void message(String m) ;
    }
    public class Test {
      private Face [B]iFace [/B]= null ;
      private JPanel theBody = null ;
      private JFrame jFrame = null;
      public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            Test application = new Test ();
            application.getJFrame().setVisible(true);
          }
        });
      }
      private JFrame getJFrame() {
        if (jFrame == null) {
          iFace = new faceData() ;
          jFrame = new JFrame() ;
          body = jFrame.getContentPane() ;
          body.setLayout(new BorderLayout()) ;
          body.add(getBody(), BorderLayout.CENTER) ;
          body.add(getButton(), BorderLayout.SOUTH) ;
        }
        return jFrame;
      }
      private JPanel getBody(){
        if (theBody == null){
          theBody = new emptyBody(screen) ;
        }
        return theBody  ;
      }
      private JButton getButton(){
        JButton b = new JButton() ;
        b.setName(nama) ;
        b.addMouseListener(new MouseAdapter(){
          public void mousePressed(MouseEvent e){
            iFace.message("Home") ;
          }
        });
      }
      private class faceData implements [B]Face[/B]{
        public void message(String m) {
          body.remove(theBody) ;
          theBody = runObj(m, iFace);
          body.add(theBody , BorderLayout.CENTER);
          body.validate();
        }
      }
      private runObj(String m, [B]Face f[/B]){
        ClassLoader cld = null ;
        Class       cls = null ;
        Constructor con = null;
        Object      obj = null;
        File file = new File(System.getProperty("user.dir"));
        try {
    	URL url = file.toURL();
    	URL[] urls = new URL[]{url};
    	cld = new URLClassLoader(urls)
            cls = cld.loadClass(m);
    	con = cls.getConstructor(new Class[]{[B]Face.class[/B]});
    	obj = con.newInstance(new Object[] {[B]f[/B]});
        }
        catch(.....}
        return (JPanel) obj ;
       }
    }
    public class Home Extend JPanel{
      private [B]Face [/B]iFace = null ;
      public Home([B]Face [/B]f){
        iFace = f;
        initialize() ;
      }
      private void initialize{...}
    }
    is my code right or there's any good advice?
    Please help!!! thanks
    Last edited by Niheel; Aug 23 '11, 08:34 PM. Reason: code tags
Working...