Replace dot with a space and reverse using java swing -
i want display dot instead of space whenever click on view-spaces checkbox menuitem.my problem when click checkbox replacing space dot but,after when add text textarea , click on viewspace jcheckbox menuitem,it had taken previous text , replaced.i have tried this,please run code can understand problem.please give suggestions.....thank you. here code:
public class visiblespaces extends javax.swing.jframe { int i=0; jtextarea tx,lnnum; jscrollpane scrollpane; public visiblespaces() { initcomponents(); } @suppresswarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="generated code"> private void initcomponents() { tp = new javax.swing.jtabbedpane(); jmenubar1 = new javax.swing.jmenubar(); jmenu1 = new javax.swing.jmenu(); create = new javax.swing.jmenuitem(); viewspace = new javax.swing.jcheckboxmenuitem(); setdefaultcloseoperation(javax.swing.windowconstants.exit_on_close); jmenu1.settext("file"); create.settext("create"); create.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { createactionperformed(evt); } }); jmenu1.add(create); viewspace.setselected(true); viewspace.settext("viewspaces"); viewspace.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { viewspaceactionperformed(evt); } }); jmenu1.add(viewspace); 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 viewspaceactionperformed(java.awt.event.actionevent evt) { viewspace.additemlistener(new itemlistener() { string str=tx.gettext(); string previous=str; @override public void itemstatechanged(itemevent ie) { if(viewspace.getstate()){ tx.settext(previous.replace(" ",".")); } else tx.settext(str); } }); } private void createactionperformed(java.awt.event.actionevent evt) { final jinternalframe internalframe = new jinternalframe(""); i++; internalframe.setname("document"+i); internalframe.setclosable(true); internalframe.setautoscrolls(true); tx=new jtextarea(); tx.setfont(new java.awt.font("miriam fixed", 0, 13)); scrollpane=new jscrollpane(tx); internalframe.add(scrollpane); tp.add(internalframe); internalframe.setsize(internalframe.getmaximumsize()); internalframe.pack(); internalframe.setvisible(true); } 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(visiblespaces.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (instantiationexception ex) { java.util.logging.logger.getlogger(visiblespaces.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (illegalaccessexception ex) { java.util.logging.logger.getlogger(visiblespaces.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (javax.swing.unsupportedlookandfeelexception ex) { java.util.logging.logger.getlogger(visiblespaces.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 visiblespaces().setvisible(true); } }); } // variables declaration - not modify private javax.swing.jmenuitem create; private javax.swing.jcheckboxmenuitem viewspace; private javax.swing.jmenu jmenu1; private javax.swing.jmenubar jmenubar1; private javax.swing.jtabbedpane tp; // end of variables declaration
}
your code not proper, full bad practice. after lot of effort understood problem. giving solution of specific problem, try follow comments given question better understanding of uses of java swing components.
solution:
change portion of code
public void itemstatechanged(itemevent ie) { if(viewspace.getstate()){ tx.settext(previous.replace(" ",".")); } else tx.settext(str); }
with one
public void itemstatechanged(itemevent ie) { if(viewspace.getstate()){ tx.settext(tx.gettext().replace(" ",".")); } else { string last = tx.gettext().substring(tx.gettext().length()-1, tx.gettext().length()); string rest = tx.gettext().substring(0, tx.gettext().length()-1); if(!last.equals(".")) tx.settext(tx.gettext().replace("."," ")); else { rest=rest.replace("."," "); tx.settext(rest+"."); } }
Comments
Post a Comment