How can I know when an instance variable raise a event in java? -
i have created class create userinterface
of program, program has instance of userinterface
.
the useriterface
class has listeners, problem need know program main class when instance variable used create user interface raises events example tree valuechanged.
this userinterface class:
import java.awt.eventqueue; import javax.swing.jfilechooser; import javax.swing.jframe; import javax.swing.jmenubar; import javax.swing.jmenuitem; import javax.swing.jtoolbar; import java.awt.borderlayout; import javax.swing.jbutton; import javax.swing.jtree; import javax.swing.jsplitpane; import javax.swing.jpanel; import javax.swing.event.treeselectionevent; import javax.swing.event.treeselectionlistener; import javax.swing.filechooser.filenameextensionfilter; import javax.swing.tree.defaulttreemodel; import javax.swing.tree.defaultmutabletreenode; import javax.swing.tree.treeselectionmodel; import org.eclipse.swt.widgets.tree; import java.awt.dimension; import java.awt.event.actionlistener; import java.awt.event.actionevent; import java.io.filenotfoundexception; import java.io.ioexception; public class userinterfacewindow implements treeselectionlistener{ public jframe frame; public jtree tree; public jpanel panel; public jfilechooser filechooser; public ifffile ifffile; public customranaccfile inputrandaccfile; /** * create application. */ public userinterfacewindow() { initialize(); frame.setvisible(true); } /** * initialize contents of frame. */ private void initialize() { frame = new jframe(); frame.setbounds(100, 100, 758, 549); frame.setdefaultcloseoperation(jframe.exit_on_close); jtoolbar toolbar = new jtoolbar(); frame.getcontentpane().add(toolbar, borderlayout.north); jbutton btnopenfile = new jbutton("open file"); btnopenfile.addactionlistener(new openbuttonclick()); toolbar.add(btnopenfile); jbutton button = new jbutton("new button"); toolbar.add(button); jsplitpane splitpane = new jsplitpane(); splitpane.setminimumsize(new dimension(300, 25)); frame.getcontentpane().add(splitpane, borderlayout.center); tree = new jtree(); tree.setmodel(new defaulttreemodel( new defaultmutabletreenode("file") { /** * */ private static final long serialversionuid = 1l; { } } )); tree.setminimumsize(new dimension(200, 0)); tree.setmaximumsize(new dimension(200, 64)); tree.getselectionmodel().setselectionmode(treeselectionmodel.single_tree_selection); tree.addtreeselectionlistener(this); splitpane.setleftcomponent(tree); panel = new jpanel(); splitpane.setrightcomponent(panel); } public class openbuttonclick implements actionlistener{ @override public void actionperformed(actionevent e) { // todo auto-generated method stub if(filechooser==null) setupfilechooser(); if(filechooser.showopendialog(frame)==jfilechooser.approve_option){ try { if(inputrandaccfile!=null) inputrandaccfile.close(); inputrandaccfile= new customranaccfile(filechooser.getselectedfile(),"rw"); ifffile=iffutils.createifffileinfo(inputrandaccfile); iffutils.obtainsubfilelist2k14(ifffile, inputrandaccfile); userinterfacewindow.this.settreeelements(ifffile,userinterfacewindow.this.tree); } catch (filenotfoundexception e1) { // todo auto-generated catch block e1.printstacktrace(); } catch (ioexception e1) { // todo auto-generated catch block e1.printstacktrace(); } } } } public void settreeelements(ifffile ifffile, jtree tree){ defaultmutabletreenode iffnode= new defaultmutabletreenode(ifffile); defaulttreemodel model= new defaulttreemodel(iffnode, true); for(iffbasesubfile subfile: ifffile.getsubfilelist()){ iffnode.add(new defaultmutabletreenode(subfile,false)); } tree.setmodel(model); } public void setupfilechooser(){ filechooser= new jfilechooser(); filechooser.setdialogtitle("select file"); filenameextensionfilter iffcdffilter= new filenameextensionfilter("iff/cdf files","iff","cdf files","cdf"); filechooser.addchoosablefilefilter(iffcdffilter); filechooser.setacceptallfilefilterused(false); filechooser.setfilefilter(iffcdffilter); } @override public void valuechanged(treeselectionevent e) { // todo auto-generated method stub } }
and myprogram:
public class mymain { userinterfacewindow uiwindow; /** * @param args */ public static void main(string[] args) { // todo auto-generated method stub mymain mymain= new mymain(); mymain.uiwindow= new userinterfacewindow(); } }
the event treeselectionevent triggered when user clicks on node of tree.
Comments
Post a Comment