-
December 5th, 2008, 09:59 AM
#1
Help with swing
Hey ive only been programming java for about a month now and im a little stuck:/
Basically im making a basic text editor in java, at the moment im trying to load text from a file to a text area but i get this compile error
Code:
TextEditor.java:123: cannot find symbol
symbol : variable edit
location: class TextEditor
edit.setText(line);
^
Anyone have any idea what im doing wrong?
Heres my code:
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class TextEditor extends JFrame implements ActionListener {
//create icons used in toolbar
ImageIcon loadIcon = new ImageIcon("load.gif");
ImageIcon saveIcon = new ImageIcon("save.gif");
ImageIcon spellIcon = new ImageIcon("spelling.gif");
ImageIcon undoIcon = new ImageIcon("back.gif");
ImageIcon newIcon = new ImageIcon("subscribe.gif");
//create buttons
JButton load = new JButton("Load", loadIcon);
JButton save = new JButton("Save", saveIcon);
JButton spell = new JButton("Spell Check", spellIcon);
JButton undo = new JButton("Undo", undoIcon);
JButton p_new = new JButton("New", newIcon);
//constructor
public TextEditor() {
super("Text Editor");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//add buttons to the toolbar
JToolBar bar = new JToolBar();
bar.add(p_new);
bar.add(load);
bar.add(save);
bar.add(spell);
bar.add(undo);
//create menu
//menu items
JMenuItem menuLoad = new JMenuItem("Load");
JMenuItem menuSave = new JMenuItem("Save");
JMenuItem menuSpell = new JMenuItem("Spell Check");
JMenuItem menuUndo = new JMenuItem("Undo");
JMenuItem menuNew = new JMenuItem("New");
JMenuItem menuPrint = new JMenuItem("Print");
JMenuItem menuAbout = new JMenuItem("About");
JMenuItem menuToolbar = new JMenuItem("Toolbar");
JMenuItem menuStatusbar = new JMenuItem("Statusbar");
//5 menus
JMenuBar menubar = new JMenuBar();
JMenu menu = new JMenu("File");
JMenu menu2 = new JMenu("Edit");
JMenu menu3 = new JMenu("Tools");
JMenu menu4 = new JMenu("View");
JMenu menu5 = new JMenu("Help");
//add items to menu
menu.add(menuNew);
menu.addSeparator();
menu.add(menuLoad);
menu.add(menuSave);
menu.addSeparator();
menu.add(menuPrint);
menu3.add(menuSpell);
menu2.add(menuUndo);
menu5.add(menuAbout);
menu4.add(menuToolbar);
menu4.add(menuStatusbar);
menubar.add(menu);
menubar.add(menu2);
menubar.add(menu3);
menubar.add(menu4);
menubar.add(menu5);
//listen for button clicks
load.addActionListener(this);
save.addActionListener(this);
p_new.addActionListener(this);
spell.addActionListener(this);
undo.addActionListener(this);
//prepare user interface
JTextArea edit = new JTextArea(40, 120);
JScrollPane scroll = new JScrollPane(edit);
BorderLayout bord = new BorderLayout();
setLayout(bord);
add("North", bar);
add("Center", scroll);
setJMenuBar(menubar);
setSize(800, 600);
pack();
setVisible(true);
}
//button clicks
public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
if (source == load) {
loadFile();
}
if (source == save) {
saveFile();
}
if (source == p_new) {
setTitle("New!");
repaint();
}
if (source == spell) {
setTitle("Spell!");
repaint();
}
if (source == undo) {
setTitle("Undo!");
repaint();
}
}
//Loads text into text editor
void loadFile()
{
try {
FileReader file = new
FileReader("myfile.txt");
BufferedReader buff = new
BufferedReader(file);
boolean eof = false;
while (!eof) {
String line = buff.readLine();
if (line == null)
eof = true;
else
edit.setText(line);
}
buff.close();
} catch (IOException e) {
System.out.println("Error -- " + e.toString());
}
}
//saves text into file
void saveFile()
{
FileOutputStream fout;
try
{
fout = new FileOutputStream ("myfile.txt");
new PrintStream(fout).println ("hello world!");
fout.close();
}
catch (IOException e)
{
System.err.println ("Unable to write to file");
System.exit(-1);
}
}
//main function
public static void main(String[] arguments) {
TextEditor main = new TextEditor();
}
}
Thanks for any help!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|