java - How to open recent files added to menu in Swing? -
when open file added recent menu in file, when click added path of recent menu opened. how?
here code:
public class recentitemlist extends javax.swing.jframe { jtextarea tx; int i=0; int recentitems_count=0; string filename; string recentitem; queue<string> q; public recentitemlist() { q=new linkedlist<>(); initcomponents(); } @suppresswarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="generated code">//gen-begin:initcomponents private void initcomponents() { tp = new javax.swing.jtabbedpane(); jmenubar1 = new javax.swing.jmenubar(); jmenu1 = new javax.swing.jmenu(); create = new javax.swing.jmenuitem(); save = new javax.swing.jmenuitem(); open = new javax.swing.jmenuitem(); recentitems = new javax.swing.jmenu(); setdefaultcloseoperation(javax.swing.windowconstants.exit_on_close); jmenu1.settext("file"); open.settext("open"); open.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { openactionperformed(evt); } }); jmenu1.add(open); recentitems.settext("recent items....."); recentitems.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { recentitemsactionperformed(evt); } }); jmenu1.add(recentitems); jmenubar1.add(jmenu1); setjmenubar(jmenubar1); javax.swing.grouplayout layout = new javax.swing.grouplayout(getcontentpane()); getcontentpane().setlayout(layout); layout.sethorizontalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addcomponent(tp, javax.swing.grouplayout.alignment.trailing, javax.swing.grouplayout.default_size, 400, short.max_value) ); layout.setverticalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addcomponent(tp, javax.swing.grouplayout.alignment.trailing, javax.swing.grouplayout.default_size, 279, short.max_value) ); pack(); }// </editor-fold> private void openactionperformed(java.awt.event.actionevent evt) {//gen-first:event_openactionperformed filedialog fd = new filedialog(recentitemlist.this, "select file", filedialog.load); fd.show(); string title; string sts; if (fd.getfile() != null) { sts = fd.getdirectory() + fd.getfile(); title=fd.getfile(); system.out.println("title :"+sts); bufferedreader br = null; stringbuffer str = new stringbuffer(""); try { br = new bufferedreader(new filereader(sts)); string line; try { while ((line = br.readline()) != null) { str.append(line + "\n"); } } catch (ioexception ex) { logger.getlogger(recentitemlist.class.getname()).log(level.severe, null, ex); } } catch (filenotfoundexception ex) { logger.getlogger(recentitemlist.class.getname()).log(level.severe, null, ex); } string t = str.tostring(); final jinternalframe internalframe = new jinternalframe("",true,true); tx = new jtextarea(); internalframe.add(tx); i+=1; internalframe.setname("document"+i); internalframe.settitle(title); tp.add(internalframe); internalframe.setvisible(true); internalframe.addinternalframelistener(new internalframeadapter() { @override public void internalframeclosing(internalframeevent e) { tp.remove(internalframe); } }); tx.settext(t); q.add(sts); recentitems.add(sts); recentitems_count++; if(recentitems_count>2){ recentitem=(string)q.remove(); recentitems.removeall(); // recentitems_count--; (string string : q) { recentitems.add(string); } } try { br.close(); } catch (ioexception ex) { logger.getlogger(recentitemlist.class.getname()).log(level.severe, null, ex); } } } private void recentitemsactionperformed(java.awt.event.actionevent evt) {//gen-first:event_recentitemsactionperformed object[] selectedobjects = recentitems.getselectedobjects(); system.out.println(selectedobjects); }//gen-last:event_recentitemsactionperformed public static void main(string args[]) { /* set nimbus , feel */ //<editor-fold defaultstate="collapsed" desc=" , feel setting code (optional) "> /* if nimbus (introduced in java se 6) not available, stay default , feel. * details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { (javax.swing.uimanager.lookandfeelinfo info : javax.swing.uimanager.getinstalledlookandfeels()) { if ("nimbus".equals(info.getname())) { javax.swing.uimanager.setlookandfeel(info.getclassname()); break; } } } catch (classnotfoundexception ex) { java.util.logging.logger.getlogger(recentitemlist.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (instantiationexception ex) { java.util.logging.logger.getlogger(recentitemlist.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (illegalaccessexception ex) { java.util.logging.logger.getlogger(recentitemlist.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (javax.swing.unsupportedlookandfeelexception ex) { java.util.logging.logger.getlogger(recentitemlist.class.getname()).log(java.util.logging.level.severe, null, ex); } //</editor-fold> /* create , display form */ java.awt.eventqueue.invokelater(new runnable() { @override public void run() { new recentitemlist().setvisible(true); } }); } // variables declaration - not modify//gen-begin:variables private javax.swing.jmenuitem create; private javax.swing.jmenu jmenu1; private javax.swing.jmenubar jmenubar1; private javax.swing.jmenuitem open; private javax.swing.jmenu recentitems; private javax.swing.jmenuitem save; private javax.swing.jtabbedpane tp; // end of variables declaration//gen-end:variables }
don't add sub menus
recent items
string, add them menuitem
itself. tried fulfill requirement changing few lines in code:
full code:
public class recentitem extends javax.swing.jframe { jtextarea tx; int i=0; int recentitems_count=0; string filename; string recentitem; queue<string> q; public recentitem() { q=new linkedlist<>(); initcomponents(); } @suppresswarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="generated code">//gen-begin:initcomponents private void initcomponents() { tp = new javax.swing.jtabbedpane(); jmenubar1 = new javax.swing.jmenubar(); jmenu1 = new javax.swing.jmenu(); create = new javax.swing.jmenuitem(); save = new javax.swing.jmenuitem(); open = new javax.swing.jmenuitem(); recentitems = new javax.swing.jmenu(); setdefaultcloseoperation(javax.swing.windowconstants.exit_on_close); jmenu1.settext("file"); open.settext("open"); open.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { openactionperformed(evt); } }); jmenu1.add(open); recentitems.settext("recent items....."); // recentitems.addactionlistener(new java.awt.event.actionlistener() { // public void actionperformed(java.awt.event.actionevent evt) { // recentitemsactionperformed(evt); // } // }); /* no need these */ jmenu1.add(recentitems); jmenubar1.add(jmenu1); setjmenubar(jmenubar1); javax.swing.grouplayout layout = new javax.swing.grouplayout(getcontentpane()); getcontentpane().setlayout(layout); layout.sethorizontalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addcomponent(tp, javax.swing.grouplayout.alignment.trailing, javax.swing.grouplayout.default_size, 400, short.max_value) ); layout.setverticalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addcomponent(tp, javax.swing.grouplayout.alignment.trailing, javax.swing.grouplayout.default_size, 279, short.max_value) ); pack(); }// </editor-fold> private void openactionperformed(java.awt.event.actionevent evt) {//gen-first:event_openactionperformed filedialog fd = new filedialog(recentitem.this, "select file", filedialog.load); fd.show(); string title; string sts; if (fd.getfile() != null) { sts = fd.getdirectory() + fd.getfile(); title=fd.getfile(); //system.out.println("title :"+sts); bufferedreader br = null; stringbuffer str = new stringbuffer(""); try { br = new bufferedreader(new filereader(sts)); string line; try { while ((line = br.readline()) != null) { str.append(line + "\n"); } } catch (ioexception ex) { logger.getlogger(recentitem.class.getname()).log(level.severe, null, ex); } } catch (filenotfoundexception ex) { logger.getlogger(recentitem.class.getname()).log(level.severe, null, ex); } string t = str.tostring(); final jinternalframe internalframe = new jinternalframe("",true,true); tx = new jtextarea(); internalframe.add(tx); i+=1; internalframe.setname("document"+i); internalframe.settitle(title); tp.add(internalframe); internalframe.setvisible(true); internalframe.addinternalframelistener(new internalframeadapter() { @override public void internalframeclosing(internalframeevent e) { tp.remove(internalframe); } }); tx.settext(t); q.add(sts); /*changes*/ jmenuitem sts=new jmenuitem(sts); //creating new menu item string sts.addactionlistener(new java.awt.event.actionlistener() { //adding action listenner public void actionperformed(java.awt.event.actionevent evt) { submenuactionperformed(evt,sts); } }); recentitems.add(sts); //adding newly created item menu recentitems_count++; if(recentitems_count>2){ recentitem=(string)q.remove(); recentitems.removeall(); // recentitems_count--; (string string : q) { /*doing same, here again */ jmenuitem item=new jmenuitem(string); item.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { submenuactionperformed(evt,string); } }); recentitems.add(item); } } try { br.close(); } catch (ioexception ex) { logger.getlogger(recentitem.class.getname()).log(level.severe, null, ex); } } } /*event open file in new tab*/ private void submenuactionperformed(actionevent evt, string title) { bufferedreader br = null; stringbuffer str = new stringbuffer(""); string filename=new file(title).getname(); try { br = new bufferedreader(new filereader(title)); string line; try { while ((line = br.readline()) != null) { str.append(line + "\n"); } } catch (ioexception ex) { logger.getlogger(recentitem.class.getname()).log(level.severe, null, ex); } } catch (filenotfoundexception ex) { logger.getlogger(recentitem.class.getname()).log(level.severe, null, ex); } string t = str.tostring(); final jinternalframe internalframe = new jinternalframe("",true,true); tx = new jtextarea(); internalframe.add(tx); i+=1; internalframe.setname("document"+i); internalframe.settitle(filename); tp.add(internalframe); internalframe.setvisible(true); internalframe.addinternalframelistener(new internalframeadapter() { @override public void internalframeclosing(internalframeevent e) { tp.remove(internalframe); } }); tx.settext(t); try{ br.close(); } catch(ioexception e){} } // private void recentitemsactionperformed(java.awt.event.actionevent evt) {//gen-first:event_recentitemsactionperformed // object[] selectedobjects = recentitems.getselectedobjects(); // system.out.println(selectedobjects); // // }//gen-last:event_recentitemsactionperformed public static void main(string args[]) { /* set nimbus , feel */ //<editor-fold defaultstate="collapsed" desc=" , feel setting code (optional) "> /* if nimbus (introduced in java se 6) not available, stay default , feel. * details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { (javax.swing.uimanager.lookandfeelinfo info : javax.swing.uimanager.getinstalledlookandfeels()) { if ("nimbus".equals(info.getname())) { javax.swing.uimanager.setlookandfeel(info.getclassname()); break; } } } catch (classnotfoundexception ex) { java.util.logging.logger.getlogger(recentitem.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (instantiationexception ex) { java.util.logging.logger.getlogger(recentitem.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (illegalaccessexception ex) { java.util.logging.logger.getlogger(recentitem.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (javax.swing.unsupportedlookandfeelexception ex) { java.util.logging.logger.getlogger(recentitem.class.getname()).log(java.util.logging.level.severe, null, ex); } //</editor-fold> /* create , display form */ java.awt.eventqueue.invokelater(new runnable() { @override public void run() { new recentitem().setvisible(true); } }); } // variables declaration - not modify//gen-begin:variables private javax.swing.jmenuitem create; private javax.swing.jmenu jmenu1; private javax.swing.jmenubar jmenubar1; private javax.swing.jmenuitem open; private javax.swing.jmenu recentitems; private javax.swing.jmenuitem save; private javax.swing.jtabbedpane tp; // end of variables declaration//gen-end:variables }
the changed portion @ adding of menus in recent items
menu:
jmenuitem sts=new jmenuitem(sts); //creating new menu item string sts.addactionlistener(new java.awt.event.actionlistener() { //adding action listenner public void actionperformed(java.awt.event.actionevent evt) { submenuactionperformed(evt,sts); } }); recentitems.add(sts); //adding newly created item menu recentitems_count++; if(recentitems_count>2){ recentitem=(string)q.remove(); recentitems.removeall(); // recentitems_count--; (string string : q) { /*doing same, here again */ jmenuitem item=new jmenuitem(string); item.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { submenuactionperformed(evt,string); } }); recentitems.add(item); } }
and submenuactionperformed(evt,string)
method:
private void submenuactionperformed(actionevent evt, string title) { bufferedreader br = null; stringbuffer str = new stringbuffer(""); string filename=new file(title).getname(); try { br = new bufferedreader(new filereader(title)); string line; try { while ((line = br.readline()) != null) { str.append(line + "\n"); } } catch (ioexception ex) { logger.getlogger(recentitem.class.getname()).log(level.severe, null, ex); } } catch (filenotfoundexception ex) { logger.getlogger(recentitem.class.getname()).log(level.severe, null, ex); } string t = str.tostring(); final jinternalframe internalframe = new jinternalframe("",true,true); tx = new jtextarea(); internalframe.add(tx); i+=1; internalframe.setname("document"+i); internalframe.settitle(filename); tp.add(internalframe); internalframe.setvisible(true); internalframe.addinternalframelistener(new internalframeadapter() { @override public void internalframeclosing(internalframeevent e) { tp.remove(internalframe); } }); tx.settext(t); try{ br.close(); } catch(ioexception e){} }
Comments
Post a Comment