8 Replies - 707 Views - Last Post: 26 May 2012 - 06:48 AM Rate Topic: -----

#1 AmandaEziphia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 25-May 12

help with simple text editor problem

Posted 25 May 2012 - 04:26 PM

Hi! this is my first post here, just registered (: I've just started to learn Java and am trying some different tutorials (I've done a little C++ and worked quite a lot with Python in Blender Game Engine, but now want to move on). I found this tutorial: http://www.leepoint....tor/nutpad.html

It really interested me, I thought it was very cool to try to make my own text editor, no matter how simple it is. So I wrote the code, reading the notes and understanding most of it, but it wont compile. it says it cant find the main class texttest001.TextTest001, I've tried to copy the tutorials script and renaming the directories to NutPad, still no luck ): I would greatly appreciate any help!

Heres my script:

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

public class TextTest001 extends JFrame {
    private JTextArea _editArea;
    private JFileChooser _fileChooser = new JFileChooser();
    
    private Action _openAction = new OpenAction();
    private Action _saveAction = new SaveAction();
    private Action _exitAction = new ExitAction();
    
    public static void main(String[] args) {
        new TextTest001();
    }
    
    public TextTest001() {
        _editArea = new JTextArea(15, 80);
        _editArea.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
        _editArea.setFont(new Font("monospaced", Font.PLAIN, 14));
        JScrollPane scrollingText = new JScrollPane(_editArea);
        
        JPanel content = new JPanel();
        content.setLayout(new BorderLayout());
        content.add(scrollingText, BorderLayout.CENTER);
        
        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = menuBar.add(new JMenu("File"));
        fileMenu.setMnemonic('F');
        fileMenu.add(_openAction);
        fileMenu.add(_saveAction);
        fileMenu.addSeparator();
        fileMenu.add(_exitAction);
        
        setContentPane(content);
        setJMenuBar(menuBar);
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("TextTest001");
        pack();
        setLocationRelativeTo(null);
        setVisible(true);        
    }
    class OpenAction extends AbstractAction {
        public OpenAction() {
            super("Open...");
            putValue(MNEMONIC_KEY, new Integer('O'));
        }
        
        public void actionPerformed(ActionEvent e) {
            int retval = _fileChooser.showOpenDialog(TextTest001.this);
            if (retval == JFileChooser.APPROVE_OPTION) {
                File f = _fileChooser.getSelectedFile();
                try {
                    FileReader reader = new FileReader(f);
                    _editArea.read(reader, "");
                } catch (IOException ioex) {
                    System.out.println(e);
                    System.exit(1);
                }
            }
        }        
    }
    class SaveAction extends AbstractAction {
        SaveAction() {
            super("Save...");
            putValue(MNEMONIC_KEY, new Integer('S'));
        }
        public void actionPerformed(ActionEvent e) {
            int retval = _fileChooser.showSaveDialog(TextTest001.this);
            if (retval == JFileChooser.APPROVE_OPTION) {
                file f = _fileChooser.getSelectedFile();
                try {
                    FileWriter writer = new FileWriter(f);
                    _editArea.write(writer);
                } catch (IOException ioex) {
                    JOptionPane.showMessageDialog(TextTest001.this, ioex);
                    System.exit(1);
                }
            }
        }       
    }
    class ExitAction extends AbstractAction {
        public ExitAction() {
            super("Exit");
            putValue(MNEMONIC_KEY, new Integer('X'));
        }
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }       
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: help with simple text editor problem

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,612
  • Joined: 27-December 08

Re: help with simple text editor problem

Posted 25 May 2012 - 06:32 PM

Your code actually fails to compile due to this line: file f = _fileChooser.getSelectedFile();. Remember that File is a class, so the 'f' should be capitalized.
Was This Post Helpful? 0
  • +
  • -

#3 AmandaEziphia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 25-May 12

Re: help with simple text editor problem

Posted 26 May 2012 - 01:26 AM

Thank you very much for your reply! I've changed "file" to "File", still same problem ):

I still get the error "can't find or can't load the main class texttest001.TextTest001"
Was This Post Helpful? 0
  • +
  • -

#4 AmandaEziphia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 25-May 12

Re: help with simple text editor problem

Posted 26 May 2012 - 01:33 AM

There is a red line under the "i" in the first line "import java.awt.*;", and when holding my mouse over it it says "Incorrect Package, Import section does not correspond to the specified code style rules."
Was This Post Helpful? 0
  • +
  • -

#5 AmandaEziphia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 25-May 12

Re: help with simple text editor problem

Posted 26 May 2012 - 02:49 AM

Yay I managed to solve it! Don't know why it works though...
I added "package texttest001;" at the top of the script, and now it works :P
Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: help with simple text editor problem

Posted 26 May 2012 - 03:39 AM

From you first post:

Quote

cant find the main class texttest001.TextTest001

Then your last:

Quote

Don't know why it works though...
I added "package texttest001;" at the top of the script

A pretty good first post. You found and used code tags correctly, and that's a struggle for many. Hopefully, you'll come back often and continue to grow as Java programmer, and here are a few hints for when you do:

Post your error messages and stack traces EXACTLY as they appear to you. Copy and paste them into your post. Put the error trace in code tags unless they're one or two lines long.

Java is not a scripting language. We don't call Java man-readable code "script" but "source code."

Based on your quotes at the beginning of my post, I'm guessing that you created a project and then a package in that project called texttest001. You then created and wrote a source file in the package called TextTest001.java, but you didn't include the required package statement as the first line (besides comments) of the source file. Most IDEs won't let you make that error, but as a beginner you may have forced it into the error, or you copied and pasted code from somewhere else.

When I'm learning something new by following a tutorial or studying someone else's code, I follow the same steps as I would if I had created the code from scratch, even typing the code myself. This helps me think about and "absorb" the code line by line. I find this technique helps me understand it much better, and I avoid technical mistakes like the one you encountered.

You can learn more about Java packages here.

Edit: Changed the link to a better one.

This post has been edited by GregBrannon: 26 May 2012 - 03:43 AM

Was This Post Helpful? 0
  • +
  • -

#7 AmandaEziphia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 25-May 12

Re: help with simple text editor problem

Posted 26 May 2012 - 06:13 AM

thanks for the reply! I didnt copy and paste the code, I agree with you and always type it myself when following tutorials. And I would copy paste the errors, but they are in Swedish, and I don't think many people here would understand it :P
Was This Post Helpful? 0
  • +
  • -

#8 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: help with simple text editor problem

Posted 26 May 2012 - 06:20 AM

Copy and paste them anyway, and add any translations you think are necessary. This will help us in two ways: helping you to interpret and use the error messages and stack traces, and reducing the amount of time spent trying to figure out what the real problem is.
Was This Post Helpful? 0
  • +
  • -

#9 AmandaEziphia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 25-May 12

Re: help with simple text editor problem

Posted 26 May 2012 - 06:48 AM

okay! Ill do that in the future! Thank you very much for your help (:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1