Solved

Java programming using menubar

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Eclipse IDE - Cannot copy/paste from console output 8 126
advertisement module in core php 4 144
object oriented programming comparison 5 52
java stored proc example 9 19
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

919 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

12 Experts available now in Live!

Get 1:1 Help Now