Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Java programming using menubar

Posted on 2001-07-24
3
237 Views
Last Modified: 2006-11-17
Dear sir,

I have written a java code using menubar concept, everything is working but when it comes to edit menu as you know in edit menu you can find or search for a word or sentence but this is not working in my program, so could please rectify the error & make code runnable

thank you


import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class menudemo1 extends Frame implements ActionListener
{
     MenuBar mb;
     Menu file,edit,search;
     MenuItem i1,i2,i3,i4,i5,i6,i7,i8, i9;
     TextArea ta;
     String str=null,str1=null;

     public menudemo1()
     {
     mb=new MenuBar();
     file=new Menu("File");
     edit=new Menu("Edit");
     search=new Menu("Search");
     i1=new MenuItem("new");
     i2=new MenuItem("open");
     i3=new MenuItem("exit");
     i4=new MenuItem("save");
     i5=new MenuItem("copy");
     i6=new MenuItem("paste");
     i9 = new MenuItem("Cut");
     i7=new MenuItem("find");
     i8=new MenuItem("repalce");
     ta=new TextArea();
     file.add(i1);
     file.add(i2);
     file.add(i3);
     file.add(i4);
     edit.add(i5);
     edit.add(i6);
     edit.add(i9);
     search.add(i7);
     search.add(i8);
     mb.add(file);
     mb.add(edit);
     mb.add(search);
     setMenuBar(mb);
     add(ta);
     
     setSize(400,400);
     setVisible(true);
     i1.addActionListener(this);
     i2.addActionListener(this);
     i3.addActionListener(this);
     i4.addActionListener(this);
     i5.addActionListener(this);
     i6.addActionListener(this);
     i7.addActionListener(this);
     i8.addActionListener(this);
     i9.addActionListener(this);
     }
     public void actionPerformed(ActionEvent ae)
     {
          if(ae.getSource()==i1)
          ta.setText(" ");
          if(ae.getSource()==i2)
          {
          try
          {
          FileDialog fd1=new FileDialog(this,"open",FileDialog.LOAD);
          fd1.show();
          String path=fd1.getFile();
          FileReader fr=new FileReader(path);
          BufferedReader br=new BufferedReader(fr);
          while(br.ready())
          ta.append(br.readLine()+"\n");
          }catch(Exception e){}
          }
     if(ae.getSource()==i4)
     {
     FileDialog fd2=new FileDialog(this,"save",FileDialog.SAVE);
     fd2.show();
     }
     if(ae.getSource()==i3)
     dispose();
     
     if(ae.getSource()==i5)
     {
          str = ta.getSelectedText();
     
     
     }
     if(ae.getSource()==i6)
     {
          ta.append(str);
     }
     if(ae.getSource()==i7)
     {
     str=ta.getText();
     Dialog d1=new Dialog(this,"find");
     d1.setLayout(new FlowLayout());    
     Button b1=new Button("find next");
     TextField t1=new TextField(30);          
     d1.add(t1);
     d1.add(b1);
     d1.setSize(100,100);
     d1.show();
     str1=t1.getText();
     if(str1==str)
     {
     ta.getText();
     }
     }

     
     
     if(ae.getSource()==i9)
     {
          str = ta.getSelectedText();
          ta.replaceRange("", ta.getSelectionStart(), ta.getSelectionEnd());
     }

     }
     public static void main(String args[])
     {    
     new menudemo1();
     }
         
     }
     

     



















0
Comment
Question by:srini_bhat
3 Comments
 
LVL 4

Expert Comment

by:kylar
ID: 6314006
So you want someone to just write a search and replace function for you? I think you'd better hit the books a little harder.

Kylar
0
 
LVL 4

Expert Comment

by:Neutron
ID: 6314512
For "Find" you should add some action listener to your button, and move code after d2.show() in actionPerformed method of that listener. Then it might work...

In your "Replace" you should also display a dialog to let user specify what to replace with what.
After search and replace string are entered by user, again you have to handle action event from "Find and Replace" button on dialog. When the button is pressed, you can get String from your TextArea using getText method. In this string, using indexOf method of String class you can locate a substring to replace and then replace it using replaceRange method of TextArea.

I hope that this helps you.

Btw, this information is for free :o)

Good luck,
    Ntr:)
0
 
LVL 1

Accepted Solution

by:
poluru earned 150 total points
ID: 6315996
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class menudemo1 extends Frame implements ActionListener
{
    MenuBar mb;
    Menu file,edit,search;
    MenuItem i1,i2,i3,i4,i5,i6,i7,i8, i9;
    TextArea ta;
    String str=null,str1=null;
     Button b1;
     TextField t1,t2;
    public menudemo1()
    {
    mb=new MenuBar();
    file=new Menu("File");
    edit=new Menu("Edit");
    search=new Menu("Search");
    i1=new MenuItem("new");
    i2=new MenuItem("open");
    i3=new MenuItem("exit");
    i4=new MenuItem("save");
    i5=new MenuItem("copy");
    i6=new MenuItem("paste");
    i9 = new MenuItem("Cut");
    i7=new MenuItem("find");
    i8=new MenuItem("repalce");
    ta=new TextArea();
    file.add(i1);
    file.add(i2);
    file.add(i3);
    file.add(i4);
    edit.add(i5);
    edit.add(i6);
    edit.add(i9);
    search.add(i7);
    search.add(i8);
    mb.add(file);
    mb.add(edit);
    mb.add(search);
    setMenuBar(mb);
    add(ta);

    setSize(400,400);
    setVisible(true);
    i1.addActionListener(this);
    i2.addActionListener(this);
    i3.addActionListener(this);
    i4.addActionListener(this);
    i5.addActionListener(this);
    i6.addActionListener(this);
    i7.addActionListener(this);
    i8.addActionListener(this);
    i9.addActionListener(this);
    }
    public void actionPerformed(ActionEvent ae)
    {
         if(ae.getSource()==i1)
         ta.setText(" ");
         if(ae.getSource()==i2)
         {
         try
         {
         FileDialog fd1=new FileDialog(this,"open",FileDialog.LOAD);
         fd1.show();
         String path=fd1.getFile();
         FileReader fr=new FileReader(path);
         BufferedReader br=new BufferedReader(fr);
         while(br.ready())
         ta.append(br.readLine()+"\n");
         }catch(Exception e){}
         }
    if(ae.getSource()==i4)
    {
    FileDialog fd2=new FileDialog(this,"save",FileDialog.SAVE);
    fd2.show();
    }
    if(ae.getSource()==i3)
    dispose();

    if(ae.getSource()==i5)
    {
         str = ta.getSelectedText();


    }
    if(ae.getSource()==i6)
    {
         ta.append(str);
    }
    if(ae.getSource()==i7)
    {
    str=ta.getText();
    Dialog d1=new Dialog(this,"find");
    d1.setLayout(new FlowLayout());
    b1=new Button("Find");
    b1.addActionListener(this);
    t1=new TextField(30);
    d1.add(t1);
    d1.add(b1);
    d1.setSize(300,200);
    d1.show();

    }



    if(ae.getSource()==i8)
    {
           Dialog d1=new Dialog(this,"find",true);
          d1.setLayout(new FlowLayout());
          b1=new Button("ReplaceNext");
          b1.addActionListener(this);
          t1=new TextField("Find What");
          t2=new TextField("Replace With");
          d1.add(t1);
          d1.add(t2);
          d1.add(b1);
          d1.setSize(300,200);
         d1.show();
           str = ta.getSelectedText();
        ta.replaceRange("", ta.getSelectionStart(), ta.getSelectionEnd());
    }
     else if(ae.getSource()==b1)
     {
         if(ae.getActionCommand().equals("Find"))
         {
              str=ta.getText();
              str1=t1.getText();
              ta.select(str.indexOf(str1),str.indexOf(str1)+str1.length());
          }
          else if(ae.getActionCommand().equals("ReplaceNext"))
          {
               str=ta.getText();
              str1=t1.getText();
              String str2=t2.getText();
              ta.replaceRange(str2,str.indexOf(str1),str.indexOf(str1)+str1.length());

          }
    }
    }
    public static void main(String args[])
    {
    new menudemo1();
    }

    }
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

838 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question