polymorphism - Java - Multiple object types in DefaultTreeNode. How to retrieve correct object without casting? -


this first question here, hope have posted in correct area.

i have jtree add nodes different user objects. example :

 aclass = new aclass();  defaultmutabletreenode cnodea = new defaultmutabletreenode();  cnodea.setuserobject(a);  //add tree code here   bclass b = new bclass();  defaultmutabletreenode cnodeb = new defaultmutabletreenode();  cnodeb.setuserobject(b);  //add tree code here 

when retrieve these nodes, how obtain correct object associated nodes without having cast it. i'm thinking there should better way achieve without having use getclass() or instanceof() in if:else statements or switch case statements.

in end, want use this, dosomething() method called in correct class associated node.

cnode.getuserobject().dosomething(); 

here simple mockup code.

any appreciated.

import java.awt.color; import java.awt.eventqueue;  import javax.swing.jframe; import javax.swing.jbutton;  import java.awt.borderlayout; import java.awt.event.actionlistener; import java.awt.event.actionevent;  import javax.swing.jtree; import javax.swing.tree.defaultmutabletreenode; import javax.swing.tree.defaulttreemodel; import javax.swing.tree.mutabletreenode; import javax.swing.tree.treepath; import javax.swing.tree.treeselectionmodel; import javax.swing.jpanel;  public class main {  private jframe frame; private jtree tree = null; private defaulttreemodel treemodel = null;  /**  * launch application.  */ public static void main(string[] args) {     eventqueue.invokelater(new runnable() {         public void run() {             try {                 main window = new main();                 window.frame.setvisible(true);             } catch (exception e) {                 e.printstacktrace();             }         }     }); }  /**  * create application.  */ public main() {     initialize(); }  /**  * initialize contents of frame.  */ private void initialize() {     frame = new jframe();     frame.setbounds(100, 100, 450, 300);     frame.setdefaultcloseoperation(jframe.exit_on_close);      jpanel panel = new jpanel();     panel.setbounds(141, 11, 151, 175);     frame.getcontentpane().add(panel);      tree = new jtree();     tree.setbackground(new color(255, 235, 205));     defaultmutabletreenode rootnode = new defaultmutabletreenode("files");     treemodel = new defaulttreemodel(rootnode);     tree.setmodel(treemodel);     tree.getselectionmodel().setselectionmode(             treeselectionmodel.single_tree_selection);     tree.setrootvisible(true);      panel.add(tree);      jbutton btnbobjbutton = new jbutton("b");     btnbobjbutton.setbounds(219, 239, 89, 23);     btnbobjbutton.addactionlistener(new actionlistener() {         public void actionperformed(actionevent arg0) {              bclass b = new bclass();             defaultmutabletreenode cnode = new defaultmutabletreenode();             cnode.setuserobject(b);             treemodel.insertnodeinto(cnode, (mutabletreenode) rootnode, 0);             tree.setselectionpath(new treepath(treemodel                     .getpathtoroot(cnode)));             tree.expandpath(new treepath(rootnode.getpath()));         }     });     frame.getcontentpane().setlayout(null);     frame.getcontentpane().add(btnbobjbutton);      jbutton btnaobjbutton = new jbutton("a");     btnaobjbutton.addactionlistener(new actionlistener() {         public void actionperformed(actionevent arg0) {              aclass = new aclass();             defaultmutabletreenode cnode = new defaultmutabletreenode();             cnode.setuserobject(a);             treemodel.insertnodeinto(cnode, (mutabletreenode) rootnode, 0);             tree.setselectionpath(new treepath(treemodel                     .getpathtoroot(cnode)));             tree.expandpath(new treepath(rootnode.getpath()));          }     });     btnaobjbutton.setbounds(120, 239, 89, 23);     frame.getcontentpane().add(btnaobjbutton);      jbutton btndosomethingbutton = new jbutton("do something");     btndosomethingbutton.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {              if (rootnode.getchildcount() > 0) {                 defaultmutabletreenode cnode = (defaultmutabletreenode) tree                         .getlastselectedpathcomponent();                 system.out.println(cnode.getuserobject());                  /*                  * if(cnode of type classa){ //do something;                  * ((classa)(cnode.getuserobject()).dosomething(); } else                  * if(cnode of type classb){                  * ((classb)(cnode.getuserobject()).dosomething(); }                  */             }         }      });     btndosomethingbutton.setbounds(153, 212, 127, 23);     frame.getcontentpane().add(btndosomethingbutton);     } } 

classa.java

public class aclass implements methodinterface {  @override public void dosomething() {     // todo auto-generated method stub  } } 

bclass.java

public class bclass implements methodinterface{  @override public void dosomething() {     // todo auto-generated method stub    } } 

methodinterface.java

public interface methodinterface {  void dosomething(); } 


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -