[ask] alter the program to Jtextarea

just want to change program to JtextArea

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 2417 Views - Last Post: 04 November 2010 - 05:38 AM Rate Topic: ***-- 2 Votes

#1 JaCod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 27-October 10

[ask] alter the program to Jtextarea

Posted 27 October 2010 - 08:14 AM

Here is the source code for a simple compiler
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

class VisualKompilasi extends JFrame {
  private JLabel lblsource=new JLabel("Source Code");
  private JTextField txfsource=new JTextField();
  private TextArea txahasil=new TextArea();
  private JButton btnopen=new JButton("...");
  private JButton btncompile=new JButton("Compile");
  private JButton btnedit=new JButton("Edit");
  private JButton btnclear=new JButton("Clear");
  JFileChooser fc=new JFileChooser();
  File file;

  public VisualKompilasi() {
    setTitle("Simple Java Compiler");
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    int w=400;
    int h=250;
    int l = (dim.width - w)/2;
    int t = (dim.height - h)/2;
    setLocation(l,t);
    setSize(w,h);
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

  public void komponenVisual() {
    getContentPane().setLayout(null);
    getContentPane().add(lblsource);
    getContentPane().add(txfsource);
    getContentPane().add(txahasil);
    getContentPane().add(btnopen);
    getContentPane().add(btncompile);
    getContentPane().add(btnedit);
    getContentPane().add(btnclear);
    lblsource.setBounds(10,10,80,20);
    txfsource.setBounds(90,10,250,20);
    btnopen.setBounds(350,10,30,20);
    btncompile.setBounds(10,40,100,20);
    btnedit.setBounds(130,40,100,20);
    btnclear.setBounds(250,40,100,20);
    txahasil.setBounds(10,70,380,150);
    txahasil.setEditable(false);
    setVisible(true);
  }

  public void aksiReaksi() {
    btnopen.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e) {
        int returnVal=fc.showOpenDialog(VisualKompilasi.this);
        if (returnVal==JFileChooser.APPROVE_OPTION) {
          file=fc.getSelectedFile();
          txfsource.setText(file.getAbsolutePath());
        }
      }
    });

    btnedit.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        try {
          Runtime.getRuntime().exec("notepad "+file.getAbsolutePath());
        } catch (Exception ex) {
          try {
            Runtime.getRuntime().exec("gedit "+file.getAbsolutePath());
          } catch (Exception ext) {

          }
        }
      }
    });

    btncompile.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        try {
          Process pid = Runtime.getRuntime().exec("javac \""+file.getAbsolutePath()+"\"");
          BufferedReader in = new BufferedReader(new InputStreamReader(pid.getErrorStream()));
          String hasil;
          txahasil.setText("Hasil :\n");
          while((hasil=in.readLine())!=null) {
            txahasil.append(hasil);
            txahasil.append(System.getProperty("line.separator"));
          }
          if ((hasil=in.readLine())==null) {
            txahasil.append("Source "+file.getName()+" berhasil dikompilasi");
          }
          in.close();
        } catch (Exception ep) {
          JOptionPane.showMessageDialog(null,ep);
        }
      }
    });

    btnclear.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        txfsource.setText("");
        txahasil.setText("");
        txfsource.requestFocus(true);
      }
    });
  }

  public static void main(String args[]) {
    VisualKompilasi vk=new VisualKompilasi();
    vk.komponenVisual();
    vk.aksiReaksi();
  }
}





My next challenge is to change the program into a simple compiler like NetBeans or Ge, the way is to make it through Jtextarea, if anyone can help??

Is This A Good Question/Topic? 0
  • +

Replies To: [ask] alter the program to Jtextarea

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10387
  • View blog
  • Posts: 38,439
  • Joined: 27-December 08

Re: [ask] alter the program to Jtextarea

Posted 27 October 2010 - 10:17 AM

NetBeans is far from a simple IDE. What exactly are you trying to do? It seems you already have a TextArea (though you should use a JTexArea since this is a Swing GUI, not an AWT GUI).
Was This Post Helpful? 0
  • +
  • -

#3 JaCod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 27-October 10

Re: [ask] alter the program to Jtextarea

Posted 27 October 2010 - 10:26 AM

View Postmacosxnerd101, on 27 October 2010 - 09:17 AM, said:

NetBeans is far from a simple IDE. What exactly are you trying to do? It seems you already have a TextArea (though you should use a JTexArea since this is a Swing GUI, not an AWT GUI).


as above, to compile the source code already exists that is simply take the file extension. java and compile the results will be visible. the problem is how to we write in the textarea and then compile it using the button, so do not have to take the source code from outside.
any idea friends??
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10387
  • View blog
  • Posts: 38,439
  • Joined: 27-December 08

Re: [ask] alter the program to Jtextarea

Posted 27 October 2010 - 11:38 AM

You can write the text in the JTextArea to a File using tools in the java.io File, then compile that file as you have been doing. We have a bunch of tutorials on File I/O in the Java Tutorials Section if you want to check them out.
Was This Post Helpful? 0
  • +
  • -

#5 JaCod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 27-October 10

Re: [ask] alter the program to Jtextarea

Posted 27 October 2010 - 06:33 PM

View Postmacosxnerd101, on 27 October 2010 - 10:38 AM, said:

You can write the text in the JTextArea to a File using tools in the java.io File, then compile that file as you have been doing. We have a bunch of tutorials on File I/O in the Java Tutorials Section if you want to check them out.


please try. programs that use java io file, so take a file from the outside and then compiled.
yet we now want the version like GEL, so typing in the textarea then click on the compile it ....
so how to simplify the program, by typing the program in the textarea and then compiled ...
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10387
  • View blog
  • Posts: 38,439
  • Joined: 27-December 08

Re: [ask] alter the program to Jtextarea

Posted 27 October 2010 - 06:36 PM

Basically:
-Get the text from the JTextArea
-Write it to a File using one of the many tools in the java.io package (you can create the File or use an existing File)
-Compile that File as you have been doing
-Run the compiled .class File (if that is part of the assignment)

Quote

please try

We are happy to help you debug your modifications, but we will not make the necessary adjustments to the code itself. :)
Was This Post Helpful? 0
  • +
  • -

#7 JaCod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 27-October 10

Re: [ask] alter the program to Jtextarea

Posted 27 October 2010 - 06:40 PM

View Postmacosxnerd101, on 27 October 2010 - 05:36 PM, said:

Basically:
-Get the text from the JTextArea
-Write it to a File using one of the many tools in the java.io package (you can create the File or use an existing File)
-Compile that File as you have been doing
-Run the compiled .class File (if that is part of the assignment)

Quote

please try

We are happy to help you debug your modifications, but we will not make the necessary adjustments to the code itself. :)

yes you are absolutely right, that has not been met is the first step is, how do I not know?
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10387
  • View blog
  • Posts: 38,439
  • Joined: 27-December 08

Re: [ask] alter the program to Jtextarea

Posted 27 October 2010 - 06:42 PM

Not know what? Your question is a little ambiguous. Can you please clarify?
Was This Post Helpful? 0
  • +
  • -

#9 JaCod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 27-October 10

Re: [ask] alter the program to Jtextarea

Posted 02 November 2010 - 07:52 PM

sorry I need help for my research "java coding game."
coincidence in such research there is little about the compilation, you could say as GEL or JCREATOR.
coincidence that is so, this compilation is taken from external files, so my problem now is how to make a compilation directly from the textarea, so when after typing in the textarea can directly be compiled?
This script will probably be given the input ...

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

class VisualKompilasi extends JFrame {
private JLabel lblsource=new JLabel("Source Code");
private JTextField txfsource=new JTextField();
private TextArea txahasil=new TextArea();
private JButton btnopen=new JButton("...");
private JButton btncompile=new JButton("Compile");
private JButton btnedit=new JButton("Edit");
private JButton btnclear=new JButton("Clear");
JFileChooser fc=new JFileChooser();
File file;

public VisualKompilasi() {
setTitle("Simple Java Compiler");
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int w=400;
int h=250;
int l = (dim.width - w)/2;
int t = (dim.height - h)/2;
setLocation(l,t);
setSize(w,h);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void komponenVisual() {
getContentPane().setLayout(null);
getContentPane().add(lblsource);
getContentPane().add(txfsource);
getContentPane().add(txahasil);
getContentPane().add(btnopen);
getContentPane().add(btncompile);
getContentPane().add(btnedit);
getContentPane().add(btnclear);
lblsource.setBounds(10,10,80,20);
txfsource.setBounds(90,10,250,20);
btnopen.setBounds(350,10,30,20);
btncompile.setBounds(10,40,100,20);
btnedit.setBounds(130,40,100,20);
btnclear.setBounds(250,40,100,20);
txahasil.setBounds(10,70,380,150);
txahasil.setEditable(false);
setVisible(true);
}

public void aksiReaksi() {
btnopen.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int returnVal=fc.showOpenDialog(VisualKompilasi.this);
if (returnVal==JFileChooser.APPROVE_OPTION) {
file=fc.getSelectedFile();
txfsource.setText(file.getAbsolutePath());
}
}
});

btnedit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Runtime.getRuntime().exec("notepad "+file.getAbsolutePath());
} catch (Exception ex) {
try {
Runtime.getRuntime().exec("gedit "+file.getAbsolutePath());
} catch (Exception ext) {

}
}
}
});

btncompile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Process pid = Runtime.getRuntime().exec("javac \""+file.getAbsolutePath()+"\"");
BufferedReader in = new BufferedReader(new InputStreamReader(pid.getErrorStream()));
String hasil;
txahasil.setText("Hasil :\n");
while((hasil=in.readLine())!=null) {
txahasil.append(hasil);
txahasil.append(System.getProperty("line.separator"));
}
if ((hasil=in.readLine())==null) {
txahasil.append("Source "+file.getName()+" success compiled");
}
in.close();
} catch (Exception ep) {
JOptionPane.showMessageDialog(null,ep);
}
}
});

btnclear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
txfsource.setText("");
txahasil.setText("");
txfsource.requestFocus(true);
}
});
}

public static void main(String args[]) {
VisualKompilasi vk=new VisualKompilasi();
vk.komponenVisual();
vk.aksiReaksi();
}
}


Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: [ask] alter the program to Jtextarea

Posted 02 November 2010 - 08:04 PM

Ouf... post your code correctly indented

This code is not a fancy editor. It just offer you a menu to select a file. Then is opens that file in Notepad.... you can then edit the file using Notepad.
Then from the Notepad menu save the file.
You then go back to the program to ask it to compile the "registered" file.
It calls the java compiler and pipe back the error to its window.

Better to use one a multiple free IDEs.

Quite a good demo on how to open a BufferedRead on a process output stream thow :^:

Correctly indented code

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

class VisualKompilasi extends JFrame {
	private JLabel lblsource=new JLabel("Source Code");
	private JTextField txfsource=new JTextField();
	private TextArea txahasil=new TextArea();
	private JButton btnopen=new JButton("...");
	private JButton btncompile=new JButton("Compile");
	private JButton btnedit=new JButton("Edit");
	private JButton btnclear=new JButton("Clear");
	JFileChooser fc=new JFileChooser();
	File file;

	public VisualKompilasi() {
		setTitle("Simple Java Compiler");
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		int w=400;
		int h=250;
		int l = (dim.width - w)/2;
		int t = (dim.height - h)/2;
		setLocation(l,t);
		setSize(w,h);
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public void komponenVisual() {
		getContentPane().setLayout(null);
		getContentPane().add(lblsource);
		getContentPane().add(txfsource);
		getContentPane().add(txahasil);
		getContentPane().add(btnopen);
		getContentPane().add(btncompile);
		getContentPane().add(btnedit);
		getContentPane().add(btnclear);
		lblsource.setBounds(10,10,80,20);
		txfsource.setBounds(90,10,250,20);
		btnopen.setBounds(350,10,30,20);
		btncompile.setBounds(10,40,100,20);
		btnedit.setBounds(130,40,100,20);
		btnclear.setBounds(250,40,100,20);
		txahasil.setBounds(10,70,380,150);
		txahasil.setEditable(false);
		setVisible(true);
	}

	public void aksiReaksi() {
		btnopen.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				int returnVal=fc.showOpenDialog(VisualKompilasi.this);
				if (returnVal==JFileChooser.APPROVE_OPTION) {
					file=fc.getSelectedFile();
					txfsource.setText(file.getAbsolutePath());
				}
			}
		});

		btnedit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					Runtime.getRuntime().exec("notepad "+file.getAbsolutePath());
				} catch (Exception ex) {
					try {
						Runtime.getRuntime().exec("gedit "+file.getAbsolutePath());
					} catch (Exception ext) {

					}
				}
			}
		});

		btncompile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					Process pid = Runtime.getRuntime().exec("javac \""+file.getAbsolutePath()+"\"");
					BufferedReader in = new BufferedReader(new InputStreamReader(pid.getErrorStream()));
					String hasil;
					txahasil.setText("Hasil :\n");
					while((hasil=in.readLine())!=null) {
						txahasil.append(hasil);
						txahasil.append(System.getProperty("line.separator"));
					}
					if ((hasil=in.readLine())==null) {
						txahasil.append("Source "+file.getName()+" success compiled");
					}
					in.close();
				} catch (Exception ep) {
					JOptionPane.showMessageDialog(null,ep);
				}
			}
		});

		btnclear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txfsource.setText("");
				txahasil.setText("");
				txfsource.requestFocus(true);
			}
		});
	}

	public static void main(String args[]) {
		VisualKompilasi vk=new VisualKompilasi();
		vk.komponenVisual();
		vk.aksiReaksi();
	}
}


This post has been edited by pbl: 02 November 2010 - 08:09 PM

Was This Post Helpful? 0
  • +
  • -

#11 JaCod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 27-October 10

Re: [ask] alter the program to Jtextarea

Posted 02 November 2010 - 08:12 PM

yes ... This code is correct.
I just want to turn it into more simple anymore. When we have finished typing in Jtextarea then we just press the compile button to see the results of the compilation ...
My difficulty is how to create or add jtextarea in the code, so without having to take from another file?
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10387
  • View blog
  • Posts: 38,439
  • Joined: 27-December 08

Re: [ask] alter the program to Jtextarea

Posted 02 November 2010 - 08:17 PM

In order to compile Java code, it must be saved to a .java File, and you must compile that File. As all Java editors do, your program should first force the user to create a .java File in the local system to store the code. Then when the compile button is hit, save the text to the file, then open a Process to compile the File.
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: [ask] alter the program to Jtextarea

Posted 02 November 2010 - 08:20 PM

View PostJaCod, on 02 November 2010 - 09:12 PM, said:

yes ... This code is correct.
I just want to turn it into more simple anymore. When we have finished typing in Jtextarea then we just press the compile button to see the results of the compilation ...
My difficulty is how to create or add jtextarea in the code, so without having to take from another file?

You are NOT typing in the JTextArea. You are typing in a Notepad session.
Oh see what you want. Yes you can create another JTextArea. Put the content of the file into it.
When to click the compile button, save the file and compile it. Pipe the error back. Then you will have a serious problem matching the error with the correct line in the JTextArea, and even if you manage so, then, the next time you will save the file, you will also save the error messages inot it,
Was This Post Helpful? 1
  • +
  • -

#14 JaCod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 27-October 10

Re: [ask] alter the program to Jtextarea

Posted 02 November 2010 - 09:41 PM

is there any reference for it? I struggled to make the program save while compiling ...
in this case if the compilation process right or wrong will be stored in a file and when it has found the truth then it will replace the wrong file ...
I really do not need to be saved, because in my research this is a game of "java coding game" so I just use this to check the truth of the written code that uses this game later on...
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: [ask] alter the program to Jtextarea

Posted 02 November 2010 - 09:46 PM

View PostJaCod, on 02 November 2010 - 10:41 PM, said:

is there any reference for it? I struggled to make the program save while compiling ...

Quite easy, in your actionPerformed() for the compile button just save the file and then invoke the Java compiler the way your original code does
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2