Hi all

I copied the source code for a simple text editor from http://www.leepoint.net/notes-java/examples/components/editor/nutpad.html
I tried to modify it slightly by adding a help column to the menu bar so you would click Help> then a drop down list would appear with About and you click that and a frame would appear saying made by blah
Date: blah blah etc....
But i modifyed it and tried to compile it, i realised that there would be bugs but i had an error message Incompatible types which i dont understand.
I have placed
/********************me******************/
comments above most of the lines if code i added so you know what ive changed and the error was appearing on line 18
Heres the code

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

///////////////////////////////////////////////////////////////////////// NutPad
public class NutPad extends JFrame {
    //... Components 
    private JTextArea    _editArea;
    private JFileChooser _fileChooser = new JFileChooser();
    
    //... Create actions for menu items, buttons, ...
    private Action _openAction = new OpenAction();
    private Action _saveAction = new SaveAction();
    private Action _exitAction = new ExitAction();
    
    /*********me*******/
    private Action _aboutAction = new AboutAction(); /******************* error message: Incompatible types**************/
    
    //===================================================================== main
    public static void main(String[] args) {
        new NutPad();
    }
    
    //============================================================== constructor
    public NutPad() {
        //... Create scrollable text area.
        _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);
        
        //-- Create a content pane, set layout, add component.
        JPanel content = new JPanel();
        content.setLayout(new BorderLayout());
        content.add(scrollingText, BorderLayout.CENTER);
        
        //... Create menubar
        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = menuBar.add(new JMenu("File"));
        fileMenu.setMnemonic('F');
        fileMenu.add(_openAction);       // Note use of actions, not text.
        fileMenu.add(_saveAction);
        fileMenu.addSeparator(); 
        fileMenu.add(_exitAction);
        
        /*******me*******/
        //Create help button on menu bar
        JMenu helpMenu = menuBar.add(new JMenu("Help"));
        helpMenu.setMnemonic('H');
        helpMenu.add(_aboutAction);
        
        
        //... Set window content and menu.
        setContentPane(content);
        setJMenuBar(menuBar);
        
        //... Set other window characteristics.
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("NutPad");
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }
    
    ////////////////////////////////////////////////// inner class OpenAction
    class OpenAction extends AbstractAction {
        //============================================= constructor
        public OpenAction() {
            super("Open...");
            putValue(MNEMONIC_KEY, new Integer('O'));
        }
        
        //========================================= actionPerformed
        public void actionPerformed(ActionEvent e) {
            int retval = _fileChooser.showOpenDialog(NutPad.this);
            if (retval == JFileChooser.APPROVE_OPTION) {
                File f = _fileChooser.getSelectedFile();
                try {
                    FileReader reader = new FileReader(f);
                    _editArea.read(reader, "");  // Use TextComponent read
                } catch (IOException ioex) {
                    System.out.println(e);
                    System.exit(1);
                }
            }
        }
    }
    
    //////////////////////////////////////////////////// inner class SaveAction
    class SaveAction extends AbstractAction {
        //============================================= constructor
        SaveAction() {
            super("Save...");
            putValue(MNEMONIC_KEY, new Integer('S'));
        }
        
        //========================================= actionPerformed
        public void actionPerformed(ActionEvent e) {
            int retval = _fileChooser.showSaveDialog(NutPad.this);
            if (retval == JFileChooser.APPROVE_OPTION) {
                File f = _fileChooser.getSelectedFile();
                try {
                    FileWriter writer = new FileWriter(f);
                    _editArea.write(writer);  // Use TextComponent write
                } catch (IOException ioex) {
                    JOptionPane.showMessageDialog(NutPad.this, ioex);
                    System.exit(1);
                }
            }
        }
    }
    
    /*********me with ym help button but it doesnt work**********************/
    class AboutAction 
    {
    	public AboutAction()
    	{
    		JFrame About = new JFrame();
    		JLabel label = new JLabel("Hello World");
    		About.getContentPane().add(label);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setTitle("About");
            setVisible(true);
    		
    		
    	}
    }
    
    ///////////////////////////////////////////////////// inner class ExitAction
    class ExitAction extends AbstractAction {
        
        //============================================= constructor
        public ExitAction() {
            super("Exit");
            putValue(MNEMONIC_KEY, new Integer('X'));
        }
        
        //========================================= actionPerformed
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    }
}

Can someone please tell me what i ahve done wrong, i am a noob to java and this was my biggest venture yet, to edit this text editor, so please keep the explanations as simple as possible otherwise i might come back with "I dont understand what you said".

Many Thanks

HLA91

You can't declare "_aboutAction" as Action unless it implements the Action interface or extends AbstractAction, which your AboutAction does not.

Well, i have now tried to copy the way the exit choice works, but obviously instead of exiting it makes the About window appear but now i am getting error messages about " ; expected on line 124 " but i dont understand as you dont put semi colons on

public void actionPerformed(ActionEvent e)

anyway i probably havent explained myself well enough so heres what i have redone so far

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

///////////////////////////////////////////////////////////////////////// NutPad
public class NutPad extends JFrame {
    //... Components 
    private JTextArea    _editArea;
    private JFileChooser _fileChooser = new JFileChooser();
    
    //... Create actions for menu items, buttons, ...
    private Action _openAction = new OpenAction();
    private Action _saveAction = new SaveAction();
    private Action _exitAction = new ExitAction();
    
    /*********me*******/
     /******************* error message: Incompatible types**************/
    
    //===================================================================== main
    public static void main(String[] args) {
        new NutPad();
    }
    
    //============================================================== constructor
    public NutPad() {
        //... Create scrollable text area.
        _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);
        
        //-- Create a content pane, set layout, add component.
        JPanel content = new JPanel();
        content.setLayout(new BorderLayout());
        content.add(scrollingText, BorderLayout.CENTER);
        
        //... Create menubar
        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = menuBar.add(new JMenu("File"));
        fileMenu.setMnemonic('F');
        fileMenu.add(_openAction);       // Note use of actions, not text.
        fileMenu.add(_saveAction);
        fileMenu.addSeparator(); 
        fileMenu.add(_exitAction);
        
        /*******me*******/
        //Create help button on menu bar
        JMenu helpMenu = menuBar.add(new JMenu("Help"));
        helpMenu.setMnemonic('H');
        helpMenu.add(_aboutAction);
        
        
        //... Set window content and menu.
        setContentPane(content);
        setJMenuBar(menuBar);
        
        //... Set other window characteristics.
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("NutPad");
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }
    
    ////////////////////////////////////////////////// inner class OpenAction
    class OpenAction extends AbstractAction {
        //============================================= constructor
        public OpenAction() {
            super("Open...");
            putValue(MNEMONIC_KEY, new Integer('O'));
        }
        
        //========================================= actionPerformed
        public void actionPerformed(ActionEvent e) {
            int retval = _fileChooser.showOpenDialog(NutPad.this);
            if (retval == JFileChooser.APPROVE_OPTION) {
                File f = _fileChooser.getSelectedFile();
                try {
                    FileReader reader = new FileReader(f);
                    _editArea.read(reader, "");  // Use TextComponent read
                } catch (IOException ioex) {
                    System.out.println(e);
                    System.exit(1);
                }
            }
        }
    }
    
    //////////////////////////////////////////////////// inner class SaveAction
    class SaveAction extends AbstractAction {
        //============================================= constructor
        SaveAction() {
            super("Save...");
            putValue(MNEMONIC_KEY, new Integer('S'));
        }
        
        //========================================= actionPerformed
        public void actionPerformed(ActionEvent e) {
            int retval = _fileChooser.showSaveDialog(NutPad.this);
            if (retval == JFileChooser.APPROVE_OPTION) {
                File f = _fileChooser.getSelectedFile();
                try {
                    FileWriter writer = new FileWriter(f);
                    _editArea.write(writer);  // Use TextComponent write
                } catch (IOException ioex) {
                    JOptionPane.showMessageDialog(NutPad.this, ioex);
                    System.exit(1);
                }
            }
        }
    }
    
    /*********me with ym help button but it doesnt work**********************/
    class AboutAction 
    {
    	public AboutAction()
    	{
    		super("About");
            putValue(MNEMONIC_KEY, new Integer('A'));
    		
    		
            
            public void actionPerformed(ActionEvent e)
            {
            	JFrame About = new JFrame();
    			JLabel label = new JLabel("Hello World");
            	About.getContentPane().add(label);
    			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            	setTitle("About");
            	setVisible(true);
            	
            }
            
    		
    		
    	}
    }
    
    ///////////////////////////////////////////////////// inner class ExitAction
    class ExitAction extends AbstractAction {
        
        //============================================= constructor
        public ExitAction() {
            super("Exit");
            putValue(MNEMONIC_KEY, new Integer('X'));
        }
        
        //========================================= actionPerformed
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    }
}

Hope im not making a mountain out of a molehill

many thanks

HLA91

You have declared the actionPerformed() method inside your constructor. You cannot declare methods within methods.

OK, i have fixed it so it compiles fine and i get the Help option in the menu bar but when i click it i get a blank drop down menu (please run the code in this post and you will understand what i mean) , i click the blank drop down and the title of my frame changes to About, so it is half working apart form
a) the word About is not appearing in the Help> drop down list
b) It isnt creating a new frame when you click the blank box in the Help> drop down list it is simply renaming the current frame to About

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

///////////////////////////////////////////////////////////////////////// NutPad
public class NutPad extends JFrame {
    //... Components 
    private JTextArea    _editArea;
    private JFileChooser _fileChooser = new JFileChooser();
    
    //... Create actions for menu items, buttons, ...
    private Action _openAction = new OpenAction();
    private Action _saveAction = new SaveAction();
    private Action _exitAction = new ExitAction();
    
    /*********me*******/
     private Action _aboutAction = new AboutAction();
    
    //===================================================================== main
    public static void main(String[] args) {
        new NutPad();
    }
    
    //============================================================== constructor
    public NutPad() {
        //... Create scrollable text area.
        _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);
        
        //-- Create a content pane, set layout, add component.
        JPanel content = new JPanel();
        content.setLayout(new BorderLayout());
        content.add(scrollingText, BorderLayout.CENTER);
        
        //... Create menubar
        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = menuBar.add(new JMenu("File"));
        fileMenu.setMnemonic('F');
        fileMenu.add(_openAction);       // Note use of actions, not text.
        fileMenu.add(_saveAction);
        fileMenu.addSeparator(); 
        fileMenu.add(_exitAction);
        
        /*******me*******/
        //Create help button on menu bar
        JMenu helpMenu = menuBar.add(new JMenu("Help"));
        helpMenu.setMnemonic('H');
        helpMenu.add(_aboutAction);
        
        
        //... Set window content and menu.
        setContentPane(content);
        setJMenuBar(menuBar);
        
        //... Set other window characteristics.
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("NutPad");
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }
    
    ////////////////////////////////////////////////// inner class OpenAction
    class OpenAction extends AbstractAction {
        //============================================= constructor
        public OpenAction() {
            super("Open...");
            putValue(MNEMONIC_KEY, new Integer('O'));
        }
        
        //========================================= actionPerformed
        public void actionPerformed(ActionEvent e) {
            int retval = _fileChooser.showOpenDialog(NutPad.this);
            if (retval == JFileChooser.APPROVE_OPTION) {
                File f = _fileChooser.getSelectedFile();
                try {
                    FileReader reader = new FileReader(f);
                    _editArea.read(reader, "");  // Use TextComponent read
                } catch (IOException ioex) {
                    System.out.println(e);
                    System.exit(1);
                }
            }
        }
    }
    
    //////////////////////////////////////////////////// inner class SaveAction
    class SaveAction extends AbstractAction {
        //============================================= constructor
        SaveAction() {
            super("Save...");
            putValue(MNEMONIC_KEY, new Integer('S'));
        }
        
        //========================================= actionPerformed
        public void actionPerformed(ActionEvent e) {
            int retval = _fileChooser.showSaveDialog(NutPad.this);
            if (retval == JFileChooser.APPROVE_OPTION) {
                File f = _fileChooser.getSelectedFile();
                try {
                    FileWriter writer = new FileWriter(f);
                    _editArea.write(writer);  // Use TextComponent write
                } catch (IOException ioex) {
                    JOptionPane.showMessageDialog(NutPad.this, ioex);
                    System.exit(1);
                }
            }
        }
    }
    
    /*********me with ym help button but it doesnt work**********************/
    class AboutAction extends AbstractAction
    {
    	public AboutAction()
    	{
    		
    	}
    		
    		
            
            public void actionPerformed(ActionEvent e)
            {
            	JFrame About = new JFrame();
    			JLabel label = new JLabel("Hello World");
            	About.getContentPane().add(label);
    			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            	setTitle("About");
            	setVisible(true);
            	
            }
            
    		
    		
    	
    }
    
    ///////////////////////////////////////////////////// inner class ExitAction
    class ExitAction extends AbstractAction {
        
        //============================================= constructor
        public ExitAction() {
            super("Exit");
            putValue(MNEMONIC_KEY, new Integer('X'));
        }
        
        //========================================= actionPerformed
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    }
}

Thanks for your time

HLA91

Well

public void actionPerformed(ActionEvent e)
            {
            	JFrame About = new JFrame();
    			JLabel label = new JLabel("Hello World");
            	About.getContentPane().add(label);
    			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            	setTitle("About");
            	setVisible(true);
            	
            }

only one of those statements is calling a method on your "About" frame - the rest are acting on your current frame class. The errors of the last two posts just show a lack of attention rather than a lack of knowledge. If you have an error or improper behavior in the code, look at it critically and try to see why it could possibly be doing that before you post it.

As far as the blank menu, look at the super() calls in the constructors of the other menu actions and look at your own action class constructor.

Thanks alot Ezzaral i have finally done it with your help, hopefully i should not have any other problems, but if i do i know where to come you are the best

This article has been dead for over six months. Start a new discussion instead.