Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Java programming using menubar

Posted on 2001-07-24
3
Medium Priority
?
259 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
[X]
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
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 450 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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 …
Suggested Courses

722 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