-
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!
-
December 5th, 2008, 10:45 AM
#2
Re: Help with swing
You're declaring JTextArea edit as a local variable in the constructor. As soon as the constructor finishes, it goes out of scope. You need to declare it as a class field and initialize it in the constructor. Then it will be visible and available to all instance methods.
I have always wished that my computer would be as easy to use as my telephone. My wish has come true. I no longer know how to use my telephone...
Bjarne Stroustrup
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
December 5th, 2008, 12:33 PM
#3
Re: Help with swing
Thanks alot for your help!
Completely forgot about moving that one!
-
December 5th, 2008, 12:43 PM
#4
Re: Help with swing
Sorry another quick question, how do i add text to the text area without deleting the rest, eg at the moment im using:
Code:
edit.setText(line);
Which just puts the last line of text on the screen.
-
December 5th, 2008, 12:50 PM
#5
Re: Help with swing
like,
edit.setText(edit.getText()+text);
-
December 5th, 2008, 12:51 PM
#6
Re: Help with swing
Always read the API docs they list all the available methods such as append(text) which could be just what you are after.
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
|