Solved

Java programming using menubar

Posted on 2001-07-24
3
229 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now