I have two class files in a Java package. One is for the actual program, Padtest.java and the other is for loading a document, OpenBox.java. I have the them both working fine. The issue I am having is to get the output of the load file inputted to the textarea of the main file. I have recreated and tested on another project with the SAME syntax, just different class names, and it works. ??????????
I have been looking and looking and it may just be something simple. Any help would be appreciated. Thanks in advance.
BTW, I am new to Java and I am teaching myself. This program is justa simple notepad look-a-like.
Here is the main program:
Code:
package notePad;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.*;
public class Padtest extends JFrame {
JTextArea textarea = new JTextArea(5 ,30);
JMenu file = new JMenu("File");
JMenu edit = new JMenu("Edit");
JMenu format = new JMenu("Format");
JMenu view = new JMenu("View");
JMenu help = new JMenu("Help");
JMenuItem new1 = new JMenuItem("New Ctrl + N");
JMenuItem open = new JMenuItem("Open Ctrl + O");
JMenuItem save = new JMenuItem("Save Ctrl + S");
JMenuItem save1 = new JMenuItem("Save As... ");
JMenuItem exit = new JMenuItem("Exit ");
JMenuBar menubar = new JMenuBar();
event1 ex = new event1();
event2 op = new event2();
event3 sva = new event3();
public Padtest() {
super("Notepad");
setSize(400, 400);
setJMenuBar(menubar);
menubar.add(file);
menubar.add(edit);
menubar.add(format);
menubar.add(view);
menubar.add(help);
file.add(new1);
//add textarea
add(textarea);
//create list item in file and add event handler to it
file.add(open);
open.addActionListener(op);
//create save function
file.add(save);
//create save as event handler
file.add(save1);
save1.addActionListener(sva);
//create list item in file and add event handler to it
file.add(exit);
exit.addActionListener(ex);
//adds a scroll bar to textarea
JScrollPane scrollPane = new JScrollPane(textarea);
setPreferredSize(new Dimension(450, 110));
add(scrollPane, BorderLayout.CENTER);
}
public void insert(String s, int i) {
textarea.insert( s, 1);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Padtest padtest = new Padtest();
padtest.setVisible(true);
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception ex) {
ex.printStackTrace();
}
padtest.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public class event1 implements ActionListener {
public void actionPerformed(ActionEvent ex) {
System.exit(0);
}
}
public class event3 implements ActionListener {
public void actionPerformed(ActionEvent sva) {
SaveAsDialog sad = new SaveAsDialog();
}
}
public class event2 implements ActionListener {
public void actionPerformed(ActionEvent op) {
try {
OpenBox ope = new OpenBox();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void setText(String s) {
// TODO Auto-generated method stub
textarea.setText(s);
}
}
And here is the Loading program:
Code:
package notePad;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.text.StringContent;
public class OpenBox extends JFrame {
public OpenBox() throws IOException {
super();
Padtest textarea = new Padtest();
Frame frame = null;
FileDialog chooser;
String homedir;
homedir = System.getProperty("user.home");
chooser= new FileDialog( frame );
chooser.setLocationRelativeTo( null );
chooser.setLocation( 200, 200 );
chooser.setDirectory(homedir);
chooser.setVisible( true );
String filename = chooser.getFile();
String directory = chooser.getDirectory();
String file = directory + filename;
int i=0;
FileReader into = null;
try {
into = new FileReader(file); // And a char stream to read it
char[] buffer = new char[4096]; // Read 4K characters at a time
int len; // How many chars read each time
while ((len = into.read(buffer)) != -1)
{
String s = new String(buffer, 0, len);
textarea.insert( s, 1 );
i++;
System.out.println(s);
}
}
catch(IOException e) {
System.out.println("Error");
return;
}
}
/**
* @param args
* @param br
* @param strLine
* @param
* @param
* @throws IOException
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}