Thursday, 16 April 2015

VID 20141222 124349614

Friday, 21 November 2014

Simple java IDE in awt and swing.

  • Copy code and save it as Main.java
  • Compile file which generate main.calss
  •  

    import java.awt.*;
    import java.awt.Color.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.io.*;
    import java.lang.*;


     class main implements ItemListener , WindowListener ,ActionListener
    {JFrame frame;
    Container c;
    JMenuBar menubar;
    JButton sub;
    JButton sub1;
    JMenu file1 ;
    JMenu file2 ;
    JMenu file3 ;
    JMenu file4 ;
    JMenuItem new1;
    JMenuItem open;
    JMenuItem save;
    JMenuItem save_as;
    JMenuItem exit;
    JMenuItem compile;
    JMenuItem run;
    JScrollPane scpane;
    JPanel penel;
     JTextArea ta;
    String spath;
    String filename;
    public main()
    {

    //creating GUI componnet
     frame=new JFrame("IDE");
     c=frame.getContentPane();
    c.setLayout(new BorderLayout());

    JOptionPane.showMessageDialog(frame, "Press OK to contenue ");//for dialog


    frame. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//set exit button
    //creating new GUI componets...
     menubar=new JMenuBar();
     sub=new JButton("sub");
     sub1=new JButton("sub1");
     ta=new JTextArea(10,10);

    ta.setLineWrap(true);

    //set text size...
    ta.setFont(  new Font("MS Outlook", 6, 20)  );
    //Font getFont()



    //set color in text area
    ta.setForeground(Color.red);
    //Color getForeground()



     file1 =new JMenu("file");
     file2 =new JMenu("edit");
     file3 =new JMenu("view");
     file4 =new JMenu("Run");

     new1=   new JMenuItem("New");
     open=   new JMenuItem("Open");
     save=   new JMenuItem("Save");
     save_as=new JMenuItem("Save as");
     exit=   new JMenuItem("Exit");
     compile=new JMenuItem("Compile");
     run=    new JMenuItem("Run");

     penel=new JPanel();
        scpane = new JScrollPane(ta); //scrollpane  and add textarea to scrollpane
    //add GUI component
    penel.add(sub);
    penel.add(sub1);
    c.add(penel);

    c.add(scpane);


    file1.add(new1);
    file1.add(open);
    file1.add(save);
    file1.add(save_as);
    file1.add(exit);
    file4.add(compile);
    file4.add(run);

    menubar.add(file1);
    menubar.add(file2);
    menubar.add(file3);
    menubar.add(file4);

    c.add(menubar);


    //resister


    sub.addActionListener(this);
    sub.addItemListener(this);
    //ta.addActionListener(this);
    new1.addActionListener(this);
    open.addActionListener(this);
    save.addActionListener(this);
    save_as.addActionListener(this);
    exit.addActionListener(this);
    compile.addActionListener(this);
    run.addActionListener(this);

    //set lyer in Frame
    c.add(scpane,BorderLayout.CENTER);
    //c.add(sub,BorderLayout.SOUTH);
    c.add(menubar,BorderLayout.NORTH);
    c.add(penel,BorderLayout.WEST);



    JFrame.setDefaultLookAndFeelDecorated(true);//.............




    //set size
    frame.setSize(800,600);
    //set visible
    frame.setVisible(true);
    }//constucter
    public void itemStateChanged(ItemEvent ie)
    {
    }




    public void actionPerformed(ActionEvent ae)
    {
    String str = ae.getActionCommand();
    String str1= ta.getText();
    String path;
    if(str=="Save")
    {


    JFileChooser input=new JFileChooser();

    int ret = input.showSaveDialog(null);
    if(ret==input.APPROVE_OPTION)
    {
    File set=input.getSelectedFile();
    filename= set.getName();
     path = input.getSelectedFile().getAbsolutePath();//get path of file.....
    spath=path;
    System.out.println(path);



    try{
     FileWriter fstream = new FileWriter(path);
            BufferedWriter out = new BufferedWriter(fstream);
            out.write(str1);
            out.close();
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }











    }




    }
    else if(str=="Save as")
    {

       JFileChooser input=new JFileChooser();

    int ret = input.showOpenDialog(null);
    if(ret==input.APPROVE_OPTION)
    {
    File set=input.getSelectedFile();
     path = input.getSelectedFile().getAbsolutePath();//get path of file.....
    spath=path;
    filename= set.getName();
    System.out.println(path + filename);
    }
    }
    else if (str=="Exit")
    {
    System.exit(0);
    }


    else if(str=="Open")
    {
       JFileChooser input=new JFileChooser();

    int ret = input.showOpenDialog(null);
    if(ret==input.APPROVE_OPTION)
    {
    File set=input.getSelectedFile();
    //set text__--__NULL
    ta.setText("");

     path = input.getSelectedFile().getAbsolutePath();//get path of file.....
    filename= set.getName();
    String str7=filename.replace(".java","");
    System.out.println(path + str7);
    spath=path;
    try
    {




     
        FileReader read =new FileReader(path);   
           BufferedReader in =new BufferedReader(read);
      String line;
         while((line=in.readLine())!=null)
                {
                   //line=in.readLine();
                  // ta.setPage(line);
                   ta.append(line + "\n");
                 }
    }
    catch(Exception e2)
    {
    System.out.println("\u0007");
    }
    }

    }

    //to run


    else if(str=="Compile")
    {String cmd= "javac " + spath;
    try{

    Process pr1 =Runtime.getRuntime().exec(cmd);


    //Process pr1 =Runtime.getRuntime().exec("javac"  +  spath);
    }
    catch(Exception e)
    {//e.}
    }
    }
    else if(str == "Run")
    {
    String file=filename.replace(".java"," ");
    String opath=spath.replace(filename,"");
    String gopath;
    if(opath=="c:\\"||opath=="d:\\"||opath=="e:\\"||opath=="f:\\"||opath=="G:\\"||opath=="H:\\"||opath=="I:\\")
    {

     gopath=opath.replace(":\\",":");

    }
    else{
    gopath= "cd " + opath;

    }
    String cmd1=  gopath + "\n" ;
    String cmd2= "java.exe  " + file + "\n ";
    System.out.println(cmd1);
    String cm="cmd /c calc";


    try
    {

    File file1 = new File("b.bat");


    if(!file1.exists())
    {
    file1.createNewFile();
    }

    FileWriter fw =new FileWriter(file1.getAbsoluteFile());
    BufferedWriter bw =new BufferedWriter(fw);
    bw.write(cmd1);
    bw.write(cmd2);
    bw.close();





      ProcessBuilder builder = new ProcessBuilder(
     "cmd.exe","/c", "start b.bat" );

            builder.redirectErrorStream(true);
      
         Process p = builder.start();

            BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
      
         String line;

         while (true) {
        
           line = r.readLine();
              
     if (line == null)
    { break; }
         
          System.out.println(line);
         
      }









    /*ProcessBuilder builder1 = new ProcessBuilder("cmd.exe","/c","start",cmd1,"java",file);



    System.out.println("\u0007" + "\u0007");






    builder1.redirectErrorStream(true);



    Process p1 =builder1.start();
    BufferedReader r= new BufferedReader(new InputStreamReader(p1.getInputStream()) );
    String line;
    while(true)
    {
    line=r.readLine();
    if(line==null)
    {break;}
    System.out.println(line);
    }

    */



    /*Process pr1 =Runtime.getRuntime().exec(cmd1);
    Process pr2 =Runtime.getRuntime().exec(cmd2);
    Process pr3=Runtime.getRuntime().exec(cm);*/
    }
    catch(Exception e)
    {}

    }

    else
    {}







    }

    //window listener
    public void windowDeactivated(WindowEvent we)
    {

    }


    //
    public void windowActivated(WindowEvent we)
    {System.exit(0);
    }



     public void windowClosed(WindowEvent we)
    {}



    public void windowClosing(WindowEvent we)
    {System.exit(0);
    }


    public void windowIconified(WindowEvent we)
    {System.exit(0);}



     public void windowDeiconified(WindowEvent we)
    {System.exit(0);}



    public void windowOpened(WindowEvent we)
    {System.exit(0);}



     
    public static void main(String args[])
    {new main();
    }



    }

    • Copy main.c and compile the same
    • main.exe and main.java should be in one foleder or diractory
    • Run main.exe and enjoy the IDE.
Read more »