Find and Replace not working Java Swing -


i'm making simple text editor in java , reason find , replace actions won't work, made 2 separate frames, 1 find action, , other open frame 2 jtextfields find , replace. have in actionperformed

if(source == find){         jframe frame = new findframe();         frame.setvisible(true);     } 

and in findframe have action should carried out when user clicks on jmenuitem called "find".

class findframe extends jframe implements actionlistener{     jbutton find = new jbutton("find");     jtextfield findtext = new jtextfield("find what", 20);     findframe(){         setsize(400, 100);         setlocation(500, 300);         settitle("find...");         jpanel panel = new jpanel();         panel.add(find);         panel.add(findtext);         container cpane = getcontentpane();         cpane.add(panel, "center");         setvisible(true);         find.addactionlistener(this);     }      public void actionperformed(actionevent evt) {         object source = evt.getsource();         string command = evt.getactioncommand();         string search = findtext.gettext();         int n = textarea.gettext().indexof(search);         if(source == find){             menuframe.textarea.select(n,n+search.length());         }     } } 

in replace frame want user find word , replace it, type find textfield , click find button, finds first 3 characters on text field, , replace action doesn't work @ all, here replace frame

class replaceframe extends jframe implements actionlistener{ jbutton find = new jbutton("find"); jbutton replace = new jbutton("replace"); jtextfield replacetext = new jtextfield("enter text replace", 20); jtextfield findtext = new jtextfield("enter text find", 20); replaceframe(){     setsize(400, 100);     setlocation(500, 300);     settitle("replace");     jpanel panel = new jpanel();     panel.add(find);     panel.add(findtext);     panel.add(replace);     panel.add(replacetext);     container cpane = getcontentpane();     cpane.add(panel, "center");     setvisible(true);     find.addactionlistener(this);     replace.addactionlistener(this);  }  public void actionperformed(actionevent evt) {     object source = evt.getsource();     string command = evt.getactioncommand();     string search = find.gettext();     string replacing = replacetext.gettext();     int n = menuframe.textarea.gettext().indexof(search);     if(command.equals("find")){         menuframe.textarea.select(n,n+search.length());     }     if(command.equals("replace")){         menuframe.textarea.replacerange(replacing, n, n+search.length());     }  } 

}

the find , replace 2 separate jmenuitems, when user clicks on find opens findframe , when click on "replace" opens replace frame has find textfield , replace textfield.

here whole runnable code :

//programmer aly badran – project 10  import java.awt.*;  import java.awt.event.*; import javax.swing.*; import javax.swing.border.border; import javax.swing.event.menuevent; import javax.swing.event.menulistener; import javax.swing.text.defaulteditorkit; import javax.swing.text.defaulteditorkit.cutaction;  class saveframe extends jframe implements actionlistener{ jpanel panel = new jpanel(); jpanel labelpanel = new jpanel(); jbutton savebutton = new jbutton("save"); jbutton dontsave = new jbutton("dont save"); jlabel savelabel = new jlabel("are sure want close"?);                                                      public saveframe(){ setsize(400,100); setlocation(500, 300); settitle("saving..."); labelpanel.add(savelabel); panel.add(savebutton); panel.add(dontsave); container cpane = getcontentpane(); cpane.add(panel, "south"); cpane.add(labelpanel, "center"); setvisible(true); savebutton.addactionlistener(this); dontsave.addactionlistener(this); } public void actionperformed(actionevent evt){     object source = evt.getsource();     if(source == savebutton){         system.exit(0);     }     else if(source == dontsave){         system.exit(0);     }  }  } class replaceframe extends jframe implements actionlistener{ jbutton find = new jbutton("find"); jbutton replace = new jbutton("replace"); jtextfield replacetext = new jtextfield("enter text replace", 20); jtextfield findtext = new jtextfield("enter text find", 20); replaceframe(){     setsize(400, 100);     setlocation(500, 300);     settitle("replace");     jpanel panel = new jpanel();     panel.add(find);     panel.add(findtext);     panel.add(replace);     panel.add(replacetext);     container cpane = getcontentpane();     cpane.add(panel, "center");     setvisible(true);     find.addactionlistener(this);     replace.addactionlistener(this);  }  public void actionperformed(actionevent evt) {     object source = evt.getsource();     string command = evt.getactioncommand();     string search = find.gettext();     string replacing = replacetext.gettext();     int n = menuframe.textarea.gettext().indexof(search);     if(command.equals("find")){         menuframe.textarea.select(n,n+search.length());     }     if(command.equals("replace")){         menuframe.textarea.replacerange(replacing, n, n+search.length());     }  } }   class menuframe extends jframe implements actionlistener, mouselistener{ static  jtextarea textarea = new jtextarea(); static jmenubar menubar = new jmenubar(); jmenu file = new jmenu("file"); jmenu edit = new jmenu("edit"); jmenuitem copy = new jmenuitem(new defaulteditorkit.copyaction()); jmenuitem cut = new jmenuitem(new defaulteditorkit.cutaction()); jmenuitem paste = new jmenuitem(new defaulteditorkit.pasteaction()); jmenuitem copyaction = new jmenuitem(new defaulteditorkit.copyaction()); jmenuitem cutaction = new jmenuitem(new defaulteditorkit.cutaction()); jmenuitem pasteaction = new jmenuitem(new defaulteditorkit.pasteaction()); jmenu search = new jmenu("search"); jmenuitem open = new jmenuitem("open"); jmenuitem close = new jmenuitem("close"); jmenuitem quit = new jmenuitem("quit"); jmenuitem find  = new jmenuitem("find"); jmenuitem replace = new jmenuitem("replace"); jmenu font = new jmenu("font"); jcheckboxmenuitem bold = new jcheckboxmenuitem("bold"); jcheckboxmenuitem italic = new jcheckboxmenuitem("italic"); jpopupmenu popup;   public menuframe(){     container cpane = getcontentpane();     cpane.add(textarea);     toolkit tk = toolkit.getdefaulttoolkit();     dimension d = tk.getscreensize();     int screenheight = d.height;     int screenwidth = d.width;     setsize(screenwidth, screenheight);     jpanel panel = new jpanel();     imageicon closeicon = new imageicon("bin/close.png");     imageicon openicon = new imageicon("bin/open.png");     imageicon quiticon = new imageicon("bin/quit.jpeg");     imageicon findicon = new imageicon("bin/find.png");     imageicon replaceicon = new imageicon("bin/replace.png");     imageicon boldicon = new imageicon("bin/bold.png");     imageicon italicicon = new imageicon("bin/italic.png");     imageicon copyicon = new imageicon("bin/copy.png");     imageicon cuticon = new imageicon("bin/cut.png");     imageicon pasteicon = new imageicon("bin/paste.png");     border matte = borderfactory.creatematteborder(1, 1, 1, 1, color.black);     border etched = borderfactory.createetchedborder();     popup = new jpopupmenu();      copy.settext("copy");     cut.settext("cut");     paste.settext("paste");     copy.seticon(copyicon);     cut.seticon(cuticon);     paste.seticon(pasteicon);      copyaction.settext("copy");     cutaction.settext("cut");     pasteaction.settext("paste");     copyaction.seticon(copyicon);     cutaction.seticon(cuticon);     pasteaction.seticon(pasteicon);      popup.add(cut);     popup.addseparator();     popup.add(copy);     popup.addseparator();     popup.add(paste);     popup.addseparator();     popup.add(find);     popup.addseparator();     popup.add(replace);      edit.add(cutaction);     edit.addseparator();     edit.add(copyaction);     edit.addseparator();     edit.add(pasteaction);      find.seticon(findicon);     replace.seticon(replaceicon);     close.seticon(closeicon);     menubar = new jmenubar();     textarea = new jtextarea("he has achieved success.", d.width, d.height);     jscrollpane scroll = new jscrollpane(textarea);     cpane.add(scroll);     open = new jmenuitem("open", openicon);     close = new jmenuitem("close", closeicon);     quit = new jmenuitem("quit", quiticon);     find = new jmenuitem("find", findicon);     replace = new jmenuitem("replace", replaceicon);     bold = new jcheckboxmenuitem("bold", boldicon);     italic = new jcheckboxmenuitem("italic", italicicon);     textarea.setlinewrap(true);     file.add(open);     file.addseparator();     file.add(close);     file.addseparator();     file.add(quit);     menubar.add(file);     menubar.add(edit);     menubar.add(search);     menubar.add(font);     search.add(find);     search.addseparator();     search.add(replace);     font.add(bold);     font.addseparator();     font.add(italic);     copy.setenabled(false);     cut.setenabled(false);     paste.setenabled(false);     find.addactionlistener(this);     italic.addactionlistener(this);     bold.addactionlistener(this);     quit.addactionlistener(this);     close.addactionlistener(this);     find.addactionlistener(this);     replace.addactionlistener(this);     cut.addactionlistener(this);     copy.addactionlistener(this);     paste.addactionlistener(this);     cut.addmouselistener(this);          textarea.addmouselistener(this);     copy.addmouselistener(this);     paste.addmouselistener(this);     textarea.setcomponentpopupmenu(popup);     file.setbackground(color.black);     edit.setbackground(color.black);     search.setbackground(color.black);     font.setbackground(color.black);     panel.add(menubar);     menubar.setborder(matte);     menubar.setbackground(color.black);     textarea.setborder(etched); } public void actionperformed(actionevent evt){     object source = evt.getsource();     string command = evt.getactioncommand();     string s = textarea.getselectedtext();     font f = new font("italic", font.italic, 13);     font f2 = new font("bold", font.bold, 13);      if(italic.isselected()){                     textarea.setfont(f);     }     if(bold.isselected()){         textarea.setfont(f2);     }     if(bold.isselected() && italic.isselected()){         textarea.setfont(f);         textarea.setfont(f2);     }     if(command.equals("quit"))             system.exit(0);     if(command.equals("close")){         jframe frame = new saveframe();         frame.setvisible(true);     }     if(source == find){         jframe frame = new findframe();         frame.setvisible(true);     }     if(command.equals("replace")){         jframe frame2 = new replaceframe();         frame2.setvisible(true);     } }  public void mouseclicked(mouseevent e) {     boolean s = textarea.getselectedtext() != null;     if(s){         copy.setenabled(true);         cut.setenabled(true);         paste.setenabled(true);      } }   public void mouseentered(mouseevent e) {  }   public void mouseexited(mouseevent e) {  }   public void mousepressed(mouseevent e) {  }   public void mousereleased(mouseevent e) {  } class findframe extends jframe implements actionlistener{     jbutton find = new jbutton("find");     jtextfield findtext = new jtextfield("find what", 20);     findframe(){         setsize(400, 100);         setlocation(500, 300);         settitle("find...");         jpanel panel = new jpanel();         panel.add(find);         panel.add(findtext);         container cpane = getcontentpane();         cpane.add(panel, "center");         setvisible(true);         find.addactionlistener(this);     }      public void actionperformed(actionevent evt) {         object source = evt.getsource();         string command = evt.getactioncommand();         string search = findtext.gettext();         int n = textarea.gettext().indexof(search);         if(source == find){             menuframe.textarea.select(n,n+search.length());         }     } } }   public class menus { public static void main(string [] args){      jframe frame = new menuframe();     frame.setjmenubar(menuframe.menubar);     frame.setvisible(true);     frame.setdefaultcloseoperation(windowconstants.exit_on_close); }  } 

your basic problem comes down fact "selection" highlight won't painted while jtextarea doesn't have focus, know, awesome.

however, use highlighter instead, example...

if (source == find) {     try {         defaulthighlighter.defaulthighlightpainter highlighter = new defaulthighlighter.defaulthighlightpainter(uimanager.getcolor("textarea.selectionbackground"));          menuframe.textarea.gethighlighter().addhighlight(n, n + search.length(), highlighter);     } catch (badlocationexception ex) {         ex.printstacktrace();     } } 

which paint "highlight" on specified area.

you may want have @ this away remove it

this , this may of interest

in replace method, using text of find jbutton instead of findtext jtextfield

//string search = find.gettext(); string search = findtext.gettext(); string replacing = replacetext.gettext(); int n = menuframe.textarea.gettext().indexof(search); 

your codes mess (sorry, took me while navigate it).

for example, you're adding actionlistener twice find menu item , others, caused find windows appear.

i'd avoid using toolkit#getscreensize(); in connection jframe#setsize, better solution use jframe#setextendedstate , pass jframe#maximized_both, take consideration other ui assets os may have fixed on screen (like docks , task bars).

you should using dialogs instead of jframes "short term input" mechanisms

your recreating bunch of stuff you've created, example...

static jtextarea textarea = new jtextarea(); //... textarea = new jtextarea("he has achieved success.", d.width, d.height); 

what's worse, add original instance of textarea frame before create new one, runs risk of not knowing component dealing with...which compounded fact you're using static reference other classes can access it, shouldn't allowed do...


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

c# - Retrieve google contact -

javascript - How to insert selected radio button value into table cell -