7 Replies Latest reply: Mar 18, 2013 2:34 PM by TPD-Opitz RSS

    text editor, problem with menu edit

    997387
      hi, i write this code for build a text editor but select all, copy, paste and cut don't work correctly :/ could someone help me please? thanks


      import java.awt.*;
      import javax.swing.*;
      import java.awt.event.*;
      import java.io.*;
      import java.util.*;

      public class trail extends JFrame implements ActionListener
      {
      MenuBar mbar;
      Menu file,edit,format,font,font1,font2;
      MenuItem item1,item2,item3,item4;
      MenuItem item5,item6,item7,item8,item9,item10;
      MenuItem fname1,fname2,fname3,fname4;
      MenuItem fstyle1,fstyle2,fstyle3,fstyle4;
      MenuItem fsize1,fsize2,fsize3,fsize4;

      JPanel mainpanel;
      TextArea text;

      Font f;
      String months[]={
      "Jan","Feb","Mar","Apr",
      "May","Jun","Jul","Aug",
      "Sep","Oct","Nov","Dec"};

      GregorianCalendar gcalendar;


      String command=" ";
      String str=" ";

      String str1=" ",str2=" ",str3=" ";
      String str4=" ";

      String str6=" ";
      String str7=" ",str8=" ",str9=" ";

      int len1;

      int i=0;
      int pos1;
      int len;

      public trail(String str)
      {

      super(str);

      mainpanel=new JPanel();
      mainpanel=(JPanel)getContentPane();
      mainpanel.setLayout(new FlowLayout());


      mbar=new MenuBar();
      setMenuBar(mbar);

      file=new Menu("File");
      edit=new Menu("Edit");
      format=new Menu("Format");
      font=new Menu("Font");
      font1=new Menu("Font Style");
      font2=new Menu("Size");

      file.add(item1=new MenuItem("New..."));
      file.add(item2=new MenuItem("Open"));
      file.add(item3=new MenuItem("Save As..."));
      file.add(item4=new MenuItem("Exit"));
      mbar.add(file);


      edit.add(item5=new MenuItem("Cut (Ctrl+X)"));
      edit.add(item6=new MenuItem("Copy (Ctrl+C)"));
      edit.add(item7=new MenuItem("Paste (Ctrl+V)"));
      edit.add(item8=new MenuItem("Delete"));
      edit.add(item10=new MenuItem("Select All (Ctrl+A)"));
      edit.add(item9=new MenuItem("Time/Date"));
      mbar.add(edit);

      format.add(font);
      format.add(font1);
      format.add(font2);

      font.add(fname1=new MenuItem("Courier"));
      font.add(fname2=new MenuItem("Sans Serif"));
      font.add(fname3=new MenuItem("Monospaced"));
      font.add(fname4=new MenuItem("Symbol"));

      font1.add(fstyle1=new MenuItem("Regular"));
      font1.add(fstyle2=new MenuItem("Bold"));
      font1.add(fstyle3=new MenuItem("Italic"));
      font1.add(fstyle4=new MenuItem("Bold Italic"));

      font2.add(fsize1=new MenuItem("12"));
      font2.add(fsize2=new MenuItem("14"));
      font2.add(fsize3=new MenuItem("18"));
      font2.add(fsize4=new MenuItem("20"));

      mbar.add(format);



      item1.addActionListener(this);
      item2.addActionListener(this);
      item3.addActionListener(this);
      item4.addActionListener(this);
      item5.addActionListener(this);
      item6.addActionListener(this);
      item7.addActionListener(this);
      item8.addActionListener(this);
      item9.addActionListener(this);
      item10.addActionListener(this);
      fname1.addActionListener(this);
      fname2.addActionListener(this);
      fname3.addActionListener(this);
      fname4.addActionListener(this);
      fstyle1.addActionListener(this);
      fstyle2.addActionListener(this);
      fstyle3.addActionListener(this);
      fstyle4.addActionListener(this);
      fsize1.addActionListener(this);
      fsize2.addActionListener(this);
      fsize3.addActionListener(this);
      fsize4.addActionListener(this);


      text=new TextArea(26,60);
      mainpanel.add(text);

      f=new Font("Monotype Corsiva",Font.PLAIN,15);
      text.setFont(f);
      }




      public void actionPerformed(ActionEvent ae)
      {


      command=(String)ae.getActionCommand();

      if(command.equals("New..."))
      {
      dispose();
      trail note1 = new trail("Untitled-Notepad");
      note1.setSize(500,500);
      note1.setVisible(true);
      }

      try
      {

      if(command.equals("Open"))
      {

      str4=" ";
      FileDialog dialog=new FileDialog(this,"Open");
      dialog.setVisible(true);

      str1=dialog.getDirectory();
      str2=dialog.getFile();
      str3=str1+str2;
      File f=new File(str3);
      FileInputStream fobj=new FileInputStream(f);
      len=(int)f.length();
      for(int j=0;j<len;j++)
      {
      char str5=(char)fobj.read();
      str4=str4 + str5;

      }

      text.setText(str4);

      }
      }
      catch(IOException e)
      {}


      try
      {

      if(command.equals("Save As..."))
      {
      FileDialog dialog1=new FileDialog(this,"Save As",FileDialog.SAVE);
      dialog1.setVisible(true);

      str7=dialog1.getDirectory();
      str8=dialog1.getFile();
      str9=str7+str8;


      str6=text.getText();
      len1=str6.length();
      byte buf[]=str6.getBytes();

      File f1=new File(str9);
      FileOutputStream fobj1=new FileOutputStream(f1);
      for(int k=0;k<len1;k++)
      {
      fobj1.write(buf[k]);
      }
      fobj1.close();
      }

      this.setTitle(str8);

      }
      catch(IOException e){}



      if(command.equals("Exit"))
      {
      System.exit(0);
      }

      if(command.equals("Cut"))
      {
      str=text.getSelectedText();
      i=text.getText().indexOf(str);
      text.replaceRange(" ",i,i+str.length());
      }

      if(command.equals("Copy"))
      {
      str=text.getSelectedText();
      }

      if(command.equals("Paste"))
      {
      pos1=text.getCaretPosition();
      text.insert(str,pos1);
      }
      if(command.equals("Delete"))
      {
      String msg=text.getSelectedText();
      i=text.getText().indexOf(msg);
      text.replaceRange(" ",i,i+msg.length());
      }
      if(command.equals("Time/Date"))
      {
      gcalendar=new GregorianCalendar();
      String h=String.valueOf(gcalendar.get(Calendar.HOUR));
      String m=String.valueOf(gcalendar.get(Calendar.MINUTE));
      String s=String.valueOf(gcalendar.get(Calendar.SECOND));
      String date=String.valueOf(gcalendar.get(Calendar.DATE));
      String mon=months[gcalendar.get(Calendar.MONTH)];
      String year=String.valueOf(gcalendar.get(Calendar.YEAR));
      String hms="Time"+" - "+h+":"+m+":"+s+" Date"+" - "+date+" "+mon+" "+year;
      int loc=text.getCaretPosition();
      text.insert(hms,loc);
      }
      if(command.equals("Courier"))
      {

      String fontName=f.getName();
      String fontFamily=f.getFamily();
      int fontSize=f.getSize();
      int fontStyle=f.getStyle();

      f=new Font("Courier",fontStyle,fontSize);
      text.setFont(f);
      }
      if(command.equals("Sans Serif"))
      {
      String fontName=f.getName();
      String fontFamily=f.getFamily();
      int fontSize=f.getSize();
      int fontStyle=f.getStyle();

      f=new Font("Sans Serif",fontStyle,fontSize);
      text.setFont(f);
      }
      if(command.equals("Monospaced"))
      {
      String fontName=f.getName();
      String fontFamily=f.getFamily();
      int fontSize=f.getSize();
      int fontStyle=f.getStyle();

      f=new Font("Monospaced",fontStyle,fontSize);
      text.setFont(f);
      }

      if(command.equals("Symbol"))
      {
      String fontName=f.getName();
      String fontFamily=f.getFamily();
      int fontSize=f.getSize();
      int fontStyle=f.getStyle();

      f=new Font("Symbol",fontStyle,fontSize);
      text.setFont(f);
      System.out.println(f.getFamily());
      }
      if(command.equals("Regular"))
      {
      String fontName=f.getName();
      String fontFamily=f.getFamily();
      int fontSize=f.getSize();
      int fontStyle=f.getStyle();

      f=new Font(fontName,Font.PLAIN,fontSize);
      text.setFont(f);
      }
      if(command.equals("Bold"))
      {
      String fontName=f.getName();
      String fontFamily=f.getFamily();
      int fontSize=f.getSize();
      int fontStyle=f.getStyle();

      f=new Font(fontName,Font.BOLD,fontSize);
      text.setFont(f);
      }
      if(command.equals("Italic"))
      {
      String fontName=f.getName();
      String fontFamily=f.getFamily();
      int fontSize=f.getSize();
      int fontStyle=f.getStyle();

      f=new Font(fontName,Font.ITALIC,fontSize);
      text.setFont(f);
      }
      if(command.equals("Bold Italic"))
      {
      String fontName=f.getName();
      String fontFamily=f.getFamily();
      int fontSize=f.getSize();
      int fontStyle=f.getStyle();

      f=new Font(fontName,Font.BOLD|Font.ITALIC,fontSize);
      text.setFont(f);
      }

      if(command.equals("12"))
      {
      String fontName=f.getName();
      String fontFamily=f.getFamily();
      int fontSize=f.getSize();
      int fontStyle=f.getStyle();

      f=new Font(fontName,fontStyle,12);
      text.setFont(f);
      }

      if(command.equals("14"))
      {
      String fontName=f.getName();
      String fontFamily=f.getFamily();
      int fontSize=f.getSize();
      int fontStyle=f.getStyle();

      f=new Font(fontName,fontStyle,14);
      text.setFont(f);
      }
      if(command.equals("18"))
      {
      String fontName=f.getName();
      String fontFamily=f.getFamily();
      int fontSize=f.getSize();
      int fontStyle=f.getStyle();

      f=new Font(fontName,fontStyle,18);
      text.setFont(f);
      }
      if(command.equals("20"))
      {
      String fontName=f.getName();
      String fontFamily=f.getFamily();
      int fontSize=f.getSize();
      int fontStyle=f.getStyle();

      f=new Font(fontName,fontStyle,20);
      text.setFont(f);
      }
      if(command.equals("Select All"))
      {
      String strText=text.getText();
      int strLen=strText.length();
      text.select(0,strLen);
      }


      }

      public static void main(String args[])
      {
      trail note = new trail("Untitled-Notepad");
      note.setSize(500,500);
      note.setVisible(true);
      }
      }
        • 1. Re: text editor, problem with menu edit
          TPD-Opitz
          Welcome to the forum.

          -----
          please read this and edit your post accordingly: https://forums.oracle.com/forums/ann.jspa?annID=1535
          also rread this: https://forums.oracle.com/forums/ann.jspa?annID=719

          -----
          994384 wrote:
          don't work correctly
          this is not a problem description.

          Tell us what you expect and how the codes behavior differes from your expectation.
          Also reduce the code porsted to a minimum nessesary to reproduce the problem (http://sscce.org/ ).

          bye
          TPD
          • 2. Re: text editor, problem with menu edit
            997387
            if(command.equals("Cut"))
            {
            str=text.getSelectedText();
            i=text.getText().indexOf(str);
            text.replaceRange(" ",i,i+str.length());
            }

            if(command.equals("Copy"))
            {
            str=text.getSelectedText();
            }

            if(command.equals("Paste"))
            {
            pos1=text.getCaretPosition();
            text.insert(str,pos1);
            }


            if(command.equals("Select All"))
            {
            String strText=text.getText();
            int strLen=strText.length();
            text.select(0,strLen);}


            Sorry, i think that my problems are all it these part of the code. After i run my code, all the "functions" , when i press the button in my interface don't do absolutely nothig. Select all should select ALL that i wrote by these moment, paste should print in the file all the part of the text that i selected ad copy before, cut should erase only the part of the text that i have selected.
            • 3. Re: text editor, problem with menu edit
              UdayaShankaraGandhi
              Sorry, i think that my problems are all it these part of the code. After i run my code, all the "functions" , when i press the button in my interface don't do absolutely nothig. Select all should select ALL that i wrote by these moment, paste should print in the file all the part of the text that i selected ad copy before, cut should erase only the part of the text that i have selected.
              The problem in your code is of setting the action command for a button.

              You said that your interface is not performing the intended operations when you press a button. You have created your Cut button like below
              edit.add(item5 = new MenuItem("Cut (Ctrl+X)"));
              nothing but you are creating a JMenuItem with an action command Cut (Ctrl+X) but you are expecting the action command to be Cut in actionPerformed() method.
              if(command.equals("Cut")) // But Cut Menu Item created above is set with Cut (Ctrl+X) action command
              {                        //  that why the code in if condition is not executing
              str=text.getSelectedText();
              i=text.getText().indexOf(str);
              text.replaceRange(" ",i,i+str.length());
              }
              So to avoid the issue, either you can check the action command in if like
              if (command.equals("Cut (Ctrl+X)")
              or you can set the action command to cut button like
              item5.setActionCommand("Cut");
              Here is the your modified code which is working
              import java.awt.*;
              import javax.swing.*;
              import java.awt.event.*;
              import java.io.*;
              import java.util.*;
              
              public class Trail extends JFrame implements ActionListener
              {
                  MenuBar           mbar;
                  Menu              file, edit, format, font, font1, font2;
                  MenuItem          item1, item2, item3, item4;
                  MenuItem          item5, item6, item7, item8, item9, item10;
                  MenuItem          fname1, fname2, fname3, fname4;
                  MenuItem          fstyle1, fstyle2, fstyle3, fstyle4;
                  MenuItem          fsize1, fsize2, fsize3, fsize4;
              
                  JPanel            mainpanel;
                  TextArea          text;
              
                  Font              f;
                  String            months[] = {
                      "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
              
                  GregorianCalendar gcalendar;
              
              
                  String            command  = " ";
                  String            str      = " ";
              
                  String            str1     = " ", str2 = " ", str3 = " ";
                  String            str4     = " ";
              
                  String            str6     = " ";
                  String            str7     = " ", str8 = " ", str9 = " ";
              
                  int               len1;
              
                  int               i        = 0;
                  int               pos1;
                  int               len;
              
                  public Trail(String str)
                  {
              
                      super(str);
              
                      mainpanel = new JPanel();
                      mainpanel = (JPanel) getContentPane();
                      mainpanel.setLayout(new FlowLayout());
              
              
                      mbar = new MenuBar();
                      setMenuBar(mbar);
              
                      file = new Menu("File");
                      edit = new Menu("Edit");
                      format = new Menu("Format");
                      font = new Menu("Font");
                      font1 = new Menu("Font Style");
                      font2 = new Menu("Size");
              
                      file.add(item1 = new MenuItem("New..."));
                      file.add(item2 = new MenuItem("Open"));
                      file.add(item3 = new MenuItem("Save As..."));
                      file.add(item4 = new MenuItem("Exit"));
                      mbar.add(file);
              
              
                      edit.add(item5 = new MenuItem("Cut (Ctrl+X)"));
                      item5.setActionCommand("Cut");
                      
                      edit.add(item6 = new MenuItem("Copy (Ctrl+C)"));
                      item6.setActionCommand("Copy");
                      
                      edit.add(item7 = new MenuItem("Paste (Ctrl+V)"));
                      item7.setActionCommand("Paste");
                      
                      edit.add(item8 = new MenuItem("Delete"));
                      edit.add(item10 = new MenuItem("Select All (Ctrl+A)"));
                      item10.setActionCommand("Select All");
                      
                      edit.add(item9 = new MenuItem("Time/Date"));
                      mbar.add(edit);
              
                      format.add(font);
                      format.add(font1);
                      format.add(font2);
              
                      font.add(fname1 = new MenuItem("Courier"));
                      font.add(fname2 = new MenuItem("Sans Serif"));
                      font.add(fname3 = new MenuItem("Monospaced"));
                      font.add(fname4 = new MenuItem("Symbol"));
              
                      font1.add(fstyle1 = new MenuItem("Regular"));
                      font1.add(fstyle2 = new MenuItem("Bold"));
                      font1.add(fstyle3 = new MenuItem("Italic"));
                      font1.add(fstyle4 = new MenuItem("Bold Italic"));
              
                      font2.add(fsize1 = new MenuItem("12"));
                      font2.add(fsize2 = new MenuItem("14"));
                      font2.add(fsize3 = new MenuItem("18"));
                      font2.add(fsize4 = new MenuItem("20"));
              
                      mbar.add(format);
              
              
                      item1.addActionListener(this);
                      item2.addActionListener(this);
                      item3.addActionListener(this);
                      item4.addActionListener(this);
                      item5.addActionListener(this);
                      item6.addActionListener(this);
                      item7.addActionListener(this);
                      item8.addActionListener(this);
                      item9.addActionListener(this);
                      item10.addActionListener(this);
                      fname1.addActionListener(this);
                      fname2.addActionListener(this);
                      fname3.addActionListener(this);
                      fname4.addActionListener(this);
                      fstyle1.addActionListener(this);
                      fstyle2.addActionListener(this);
                      fstyle3.addActionListener(this);
                      fstyle4.addActionListener(this);
                      fsize1.addActionListener(this);
                      fsize2.addActionListener(this);
                      fsize3.addActionListener(this);
                      fsize4.addActionListener(this);
              
              
                      text = new TextArea(26, 60);
                      mainpanel.add(text);
              
                      f = new Font("Monotype Corsiva", Font.PLAIN, 15);
                      text.setFont(f);
                  }
              
              
                  public void actionPerformed(ActionEvent ae)
                  {
              
              
                      command=(String)ae.getActionCommand();
              
                      if(command.equals("New..."))
                      {
                          dispose();
                          Trail note1 = new Trail("Untitled-Notepad");
                          note1.setSize(500,500);
                          note1.setVisible(true);
                      }
              
                      try
                      {
              
                          if(command.equals("Open"))
                          {
              
                              str4=" ";
                              FileDialog dialog=new FileDialog(this,"Open");
                              dialog.setVisible(true);
              
                              str1=dialog.getDirectory();
                              str2=dialog.getFile();
                              str3=str1+str2;
                              File f=new File(str3);
                              FileInputStream fobj=new FileInputStream(f);
                              len=(int)f.length();
                              for(int j=0;j<len;j++)
                              {
                                  char str5=(char)fobj.read();
                                  str4=str4 + str5;
              
                              }
              
                              text.setText(str4);
              
                          }
                      }
                      catch(IOException e)
                      {}
              
              
                      try
                      {
              
                          if(command.equals("Save As..."))
                          {
                              FileDialog dialog1=new FileDialog(this,"Save As",FileDialog.SAVE);
                              dialog1.setVisible(true);
              
                              str7=dialog1.getDirectory();
                              str8=dialog1.getFile();
                              str9=str7+str8;
              
              
                              str6=text.getText();
                              len1=str6.length();
                              byte buf[]=str6.getBytes();
              
                              File f1=new File(str9);
                              FileOutputStream fobj1=new FileOutputStream(f1);
                              for(int k=0;k<len1;k++)
                              {
                                  fobj1.write(buf[k]);
                              }
                              fobj1.close();
                          }
              
                          this.setTitle(str8);
              
                      }
                      catch(IOException e){}
              
              
              
                      if(command.equals("Exit"))
                      {
                          System.exit(0);
                      }
              
                      if(command.equals("Cut"))
                      {
                          str=text.getSelectedText();
                          i=text.getText().indexOf(str);
                          text.replaceRange(" ",i,i+str.length());
                      }
              
                      if(command.equals("Copy"))
                      {
                          str=text.getSelectedText();
                      }
              
                      if(command.equals("Paste"))
                      {
                          pos1=text.getCaretPosition();
                          text.insert(str,pos1);
                      }
                      if(command.equals("Delete"))
                      {
                          String msg=text.getSelectedText();
                          i=text.getText().indexOf(msg);
                          text.replaceRange(" ",i,i+msg.length());
                      }
                      if(command.equals("Time/Date"))
                      {
                          gcalendar=new GregorianCalendar();
                          String h=String.valueOf(gcalendar.get(Calendar.HOUR));
                          String m=String.valueOf(gcalendar.get(Calendar.MINUTE));
                          String s=String.valueOf(gcalendar.get(Calendar.SECOND));
                          String date=String.valueOf(gcalendar.get(Calendar.DATE));
                          String mon=months[gcalendar.get(Calendar.MONTH)];
                          String year=String.valueOf(gcalendar.get(Calendar.YEAR));
                          String hms="Time" + " - " +h+ ":"+m+":"+s+" Date"+" - "+date+" "+mon+" "+year;
                          int loc=text.getCaretPosition();
                          text.insert(hms,loc);
                      }
                      if(command.equals("Courier"))
                      {
              
                          String fontName=f.getName();
                          String fontFamily=f.getFamily();
                          int fontSize=f.getSize();
                          int fontStyle=f.getStyle();
              
                          f=new Font("Courier",fontStyle,fontSize);
                          text.setFont(f);
                      }
                      if(command.equals("Sans Serif"))
                      {
                          String fontName=f.getName();
                          String fontFamily=f.getFamily();
                          int fontSize=f.getSize();
                          int fontStyle=f.getStyle();
              
                          f=new Font("Sans Serif",fontStyle,fontSize);
                          text.setFont(f);
                      }
                      if(command.equals("Monospaced"))
                      {
                          String fontName=f.getName();
                          String fontFamily=f.getFamily();
                          int fontSize=f.getSize();
                          int fontStyle=f.getStyle();
              
                          f=new Font("Monospaced",fontStyle,fontSize);
                          text.setFont(f);
                      }
              
                      if(command.equals("Symbol"))
                      {
                          String fontName=f.getName();
                          String fontFamily=f.getFamily();
                          int fontSize=f.getSize();
                          int fontStyle=f.getStyle();
              
                          f=new Font("Symbol",fontStyle,fontSize);
                          text.setFont(f);
                          System.out.println(f.getFamily());
                      }
                      if(command.equals("Regular"))
                      {
                          String fontName=f.getName();
                          String fontFamily=f.getFamily();
                          int fontSize=f.getSize();
                          int fontStyle=f.getStyle();
              
                          f=new Font(fontName,Font.PLAIN,fontSize);
                          text.setFont(f);
                      }
                      if(command.equals("Bold"))
                      {
                          String fontName=f.getName();
                          String fontFamily=f.getFamily();
                          int fontSize=f.getSize();
                          int fontStyle=f.getStyle();
              
                          f=new Font(fontName,Font.BOLD,fontSize);
                          text.setFont(f);
                      }
                      if(command.equals("Italic"))
                      {
                          String fontName=f.getName();
                          String fontFamily=f.getFamily();
                          int fontSize=f.getSize();
                          int fontStyle=f.getStyle();
              
                          f=new Font(fontName,Font.ITALIC,fontSize);
                          text.setFont(f);
                      }
                      if(command.equals("Bold Italic"))
                      {
                          String fontName=f.getName();
                          String fontFamily=f.getFamily();
                          int fontSize=f.getSize();
                          int fontStyle=f.getStyle();
              
                          f=new Font(fontName,Font.BOLD|Font.ITALIC,fontSize);
                          text.setFont(f);
                      }
              
                      if(command.equals("12"))
                      {
                          String fontName=f.getName();
                          String fontFamily=f.getFamily();
                          int fontSize=f.getSize();
                          int fontStyle=f.getStyle();
              
                          f=new Font(fontName,fontStyle,12);
                          text.setFont(f);
                      }
              
                      if(command.equals("14"))
                      {
                          String fontName=f.getName();
                          String fontFamily=f.getFamily();
                          int fontSize=f.getSize();
                          int fontStyle=f.getStyle();
              
                          f=new Font(fontName,fontStyle,14);
                          text.setFont(f);
                      }
                      if(command.equals("18"))
                      {
                          String fontName=f.getName();
                          String fontFamily=f.getFamily();
                          int fontSize=f.getSize();
                          int fontStyle=f.getStyle();
              
                          f=new Font(fontName,fontStyle,18);
                          text.setFont(f);
                      }
                      if(command.equals("20"))
                      {
                          String fontName=f.getName();
                          String fontFamily=f.getFamily();
                          int fontSize=f.getSize();
                          int fontStyle=f.getStyle();
              
                          f=new Font(fontName,fontStyle,20);
                          text.setFont(f);
                      }
                      if(command.equals("Select All"))
                      {
                          String strText=text.getText();
                          int strLen=strText.length();
                          text.select(0,strLen);
                      }
              
              
                  }
              
                  public static void main(String args[])
                  {
                      Trail note = new Trail("Untitled-Notepad");
                      note.setSize(500, 500);
                      note.setVisible(true);
                  }
              }
              Edited by: Udaya Shankara Gandhi on Mar 18, 2013 2:11 AM
              • 4. Re: text editor, problem with menu edit
                TPD-Opitz
                Udaya Shankara Gandhi wrote:
                You said that your interface is not performing the intended operations when you press a button. You have created your Cut button like below
                edit.add(item5 = new MenuItem("Cut (Ctrl+X)"));
                nothing but you are creating a JMenuItem with an action command Cut (Ctrl+X) but you are expecting the action command to be Cut in actionPerformed() method.
                if(command.equals("Cut")) // But Cut Menu Item created above is set with Cut (Ctrl+X) action command
                {                        //  that why the code in if condition is not executing
                str=text.getSelectedText();
                i=text.getText().indexOf(str);
                text.replaceRange(" ",i,i+str.length());
                }
                So to avoid the issue, either you can check the action command in if like
                if (command.equals("Cut (Ctrl+X)")
                or you can set the action command to cut button like
                 item5.setActionCommand("Cut");
                While this is true the better choice was to use separate (anonymous) inner classes for each buttons ActionListener. Then there was no need to find out which button causes the action:
                item5.addActionListener(new ActionListener(){
                  public void actionPerformed(ActionEvent ae) {
                     doCutAction();
                  }
                });
                bye
                TPD
                • 5. Re: text editor, problem with menu edit
                  TPD-Opitz
                  unintended double post due to network problems....

                  Edited by: TPD Opitz-Consulting com on 18.03.2013 16:06
                  • 6. Re: text editor, problem with menu edit
                    997387
                    Really Thanks ! The code run now!!!!! I have a last question. In all text editor the change of the font is affettive only in the part of the text tha you selected. In my app the font changement became effettive in all the text that i have written.. How can i modified the function?
                    • 7. Re: text editor, problem with menu edit
                      TPD-Opitz
                      994384 wrote:
                      In all text editor the change of the font is affettive only in the part of the text tha you selected. In my app the font changement became effettive in all the text that i have written.. How can i modified the function?
                      Replace your JTextArea with an JEditorPane and use HTML/CSS to format your selected text.

                      bye
                      TPD