java - JFileChooser won't work properly -
i working jfilechooser , want make show file selection dialog display when fileitem1 clicked. here swingmenu class.
question: missing/doing wrong in showfilechooser method? in advance.
import javax.swing.*; import javax.swing.filechooser.filenameextensionfilter; import java.awt.event.actionevent; import java.io.file; import java.awt.event.actionlistener; /* *this class encapsulates build of menu bar , called drawpanelmain add jframe */ public class swingmenu extends jmenubar { /* * initializes jmenuitems added menu. necessary * access actionevent handler. includes filechooser used * actionevent of clicking on fileitem1. */ private jmenuitem fileitem1 = null; private jmenuitem fileitem2 = null; private jmenuitem edititem1 = null; private jmenuitem helpitem1 = null; private jmenuitem toolsitem1 = null; /* * create jfilechooser used in actionevent click on fileitem1 */ jfilechooser chooser = new jfilechooser("desktop"); /* *these main items on menubar */ public swingmenu(){initmenubar();} private void initmenubar() { /* *initializes main items on menubar */ jmenu filemenu = new jmenu("file"); jmenu editmenu = new jmenu("edit"); jmenu toolsmenu = new jmenu("tools"); jmenu helpmenu = new jmenu("help"); /* *initializes components of main items */ jmenuitem fileitem1 = new jmenuitem("open"); jmenuitem fileitem2 = new jmenuitem("save"); jmenuitem edititem1 = new jmenuitem("edit configuration"); jmenuitem helpitem1 = new jmenuitem("user manual"); jmenuitem toolsitem1 = new jmenuitem("fetch configuration"); /* * add action listener fileitem1 use in jfilechooser actionevent */ fileitem1.addactionlistener(new actionlistener() { public void showfilechooser (actionevent e){ chooser.setfileselectionmode(jfilechooser.files_and_directories); filenameextensionfilter filter = new filenameextensionfilter("directories , xml files", "xml"); chooser.setfilefilter(filter); int returnval = chooser.showopendialog(this); if (returnval == jfilechooser.approve_option) { string path = chooser.getselectedfile().getabsolutepath(); joptionpane.showmessagedialog(null, "you selected" + path); } } }); /* *each component added assigned menu item */ filemenu.add(fileitem1); filemenu.add(fileitem2); editmenu.add(edititem1); toolsmenu.add(toolsitem1); helpmenu.add(helpitem1); /* *menu items added menubar */ add(filemenu); add(editmenu); add(toolsmenu); add(helpmenu); } }
the problem this: public void showfilechooser (actionevent e){
. in java, event handling method needs public void actionperformed(actionevent e) {
:
fileitem1.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { chooser.setfileselectionmode(jfilechooser.files_and_directories); filenameextensionfilter filter = new filenameextensionfilter("directories , xml files", "xml"); chooser.setfilefilter(filter); int returnval = chooser.showopendialog(this); if (returnval == jfilechooser.approve_option) { string path = chooser.getselectedfile().getabsolutepath(); joptionpane.showmessagedialog(null, "you selected" + path); } } });
Comments
Post a Comment