Solved

Java programming using menubar

Posted on 2001-07-24
3
234 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
topping2 challenge 13 90
javap not working 8 44
check java version using powershell 13 82
tomcat startup error 5 59
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

785 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