Trying to create a java notepad

Getting an error concerning the constructor

Page 1 of 1

3 Replies - 11679 Views - Last Post: 10 August 2010 - 12:04 PM Rate Topic: -----

#1 jingoria  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 96
  • Joined: 22-October 07

Trying to create a java notepad

Posted 09 August 2010 - 02:45 PM

Hi guys,
I just wrote a notepad application in java just for fun & help myself refreshing java. The basic application works fine but then I tried adding stuff like cut, copy, paste features etc. and ran into some trouble with the constructor. Note: The code may seem similar because not everything in this code is mine. I have wrote a whole but some of them I got online and have changed according to my program.

The error I get is on line 379 (marked in red): which is suppose to be a default constructor for the class BasicAction().
The error states: Illegal method declaration; return type required.
I am confused because constructor have no return type. So please help me with finding the real error.

My code:
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.awt.event.KeyAdapter;
import javax.swing.*;
import javax.swing.AbstractAction;
import javax.swing.text.*;
import javax.swing.JOptionPane;
import java.util.*;
import java.util.Scanner;
import java.io.*;

public class WritePad extends JFrame implements ActionListener//,KeyListener
{
// -----General Variables: Note: all variables of java that will require interface  have J type initialization------------------------------------------

String word;
Icon picture;

public BasicAction cut,copy,paste;

boolean txtChanged = false; //to check file changes
String fname = ""; //filename variable
String aArgument = " ";
JMenuBar mbar; //menubar
JFrame frame;
JMenu mnuFile, mnuEdit, mnuHelp; //3 different types of menus
JMenuItem NewFile, OpenFile, SaveFile, Exit; //Items in File Menu
JMenuItem Cut, Copy, Paste, SelectAll;  //Items in Edit Menu
JMenuItem About; //Items in Help Menu

JToolBar tlbr;  //toolbar
ImageIcon iconNew, iconOpen, iconSave; //images for file menu items
ImageIcon iconcut, iconcopy, iconpaste, iconselectAll; //images for edit menu items
ImageIcon iconAbout;

JButton buttonNew, buttonOpen, buttonSave; //creates buttons for file menu items
JButton buttoncut, buttoncopy, buttonpaste, buttonselectAll; // creates buttons for edit menu item
JButton buttonAbout; //creates button for Help menu item
 
JTextArea  txtPad; // variable that will be used for creating writing Area
Container c; // ?

// --------------------------- Actual Implementation starts here------------------------------------
WritePad() 
{
initComponents(); //Call to Function that Initializes Components 
setTitle("WritePad");
setSize (500, 400);
setVisible(true);

setDefaultCloseOperation(EXIT_ON_CLOSE);
addWindowListener(new WindowHandler());
}

//------------ Definition for the first function called by WritePad
void initComponents()
{
c = getContentPane(); // Container C gets a Content Pane..... ?
c.setLayout(new BorderLayout()); // sets a layout for content Pane.......?

initMenu(); // Calls another function to Initialize the Menu
initToolbar(); // Then Call to another function to Initialize the Toolbar

txtPad = new JTextArea(); //using the variable we Have Created new Writing Area
Font f = new Font("Comic sans MS", Font.PLAIN, 20); // Creates a variable for font setup & initialize it by new Font function
txtPad.setFont(f); // The new font is set on the writing area
//txtPad.addKeyListener(this); // Adds the functionality to all the stuff above.... ?

JScrollPane jscroll = new JScrollPane(txtPad); // Creates new Java ScrollBar variable 
c.add(jscroll, BorderLayout.CENTER);// adds a scrollbar to the container & adjusts the border layout

}

// Definition for the first function called by initComponents
//This function initializes all the menus
void initMenu()
{

mbar = new JMenuBar(); // creates a new menu bar

mnuFile = new JMenu("File"); // this creates a new menu called file
mnuEdit = new JMenu("Edit"); // this creates a new menu called edit
mnuHelp = new JMenu("Help"); //this creates a new menu called help

// using above MenuItem variables we create new menu items
NewFile = new JMenuItem("New");
OpenFile = new JMenuItem("Open");
SaveFile = new JMenuItem("Save");
Exit = new JMenuItem("Close");

Cut = new JMenuItem("Cut");
Copy = new JMenuItem("Copy");
Paste = new JMenuItem("Paste");
SelectAll = new JMenuItem("SelectAll");

About = new JMenuItem("About");



// Now we add the menu items to their corresponding menus

mnuFile.add(NewFile);
mnuFile.add(OpenFile);
mnuFile.add(SaveFile);
mnuFile.add(Exit);

mnuEdit.add(Cut);
mnuEdit.add(Copy);
mnuEdit.add(Paste);
mnuEdit.add(SelectAll);

mnuHelp.add(About);

// Now add the menus to the menu bar
mbar.add(mnuFile);
mbar.add(mnuEdit);
mbar.add(mnuHelp);

// The menubar is now finally added to the window ... i.e. menubar is given the abstraction
setJMenuBar(mbar);

// Now we add action for implementation to the menu items

NewFile.addActionListener(this);
OpenFile.addActionListener(this);
SaveFile.addActionListener(this);
Exit.addActionListener(this);

Cut.addActionListener(this);
Copy.addActionListener(this);
Paste.addActionListener(this);
SelectAll.addActionListener(this);

About.addActionListener(this);
}

//------------------Definition for second function called by by initComponents-------------------------------
// This function initializes the Toolbars

void initToolbar()
{
iconNew = new ImageIcon("C:/java/images/new.jpg");
iconOpen = new ImageIcon("C:/java/images/open.png");
iconSave = new ImageIcon("C:/java/images/save.jpg");

iconcut = new ImageIcon("C:/java/images/Cut.png");
iconcopy = new ImageIcon("C:/java/images/Copy.png");
iconpaste = new ImageIcon("C:/java/images/Paste.png");
//iconselectAll = new ImageIcon("C:/java/images/SelectAll.gif");

//iconAbout = new ImageIcon("C:/java/images/About.gif");

//Add button to the images of menu item .... i.e. make it look like clickable
buttonNew = new JButton(iconNew);
buttonOpen = new JButton(iconOpen);
buttonSave = new JButton (iconSave);

buttoncut = new JButton(iconcut);
buttoncopy = new JButton(iconcopy);
buttonpaste = new JButton(iconpaste);
//buttonselectAll = new JButton(iconselectAll);

//buttonAbout = new JButton(iconAbout);




//Now create a new toolbar
tlbr = new JToolBar();

//this adds the clickable icons on the toolbar & the images correspond to these icons 
tlbr.add(buttonNew);
tlbr.add(buttonOpen);
tlbr.add(buttonSave);

tlbr.add(buttoncut);
tlbr.add(buttoncopy);
tlbr.add(buttonpaste);
//tlbr.add(buttonselectAll);

//tlbr.add(buttonAbout);



//Add the  image toolbar created above to the window
c.add(tlbr,BorderLayout.NORTH);

//Now we add the actionlister to the buttons to make them working
buttonNew.addActionListener(this);
buttonOpen.addActionListener(this);
buttonSave.addActionListener(this);

buttoncut.addActionListener(this);
buttoncopy.addActionListener(this);
buttonpaste.addActionListener(this);
//buttonselectAll.addActionListener(this);

//buttonAbout.addActionListener(this);

}
//------------ End of second function definition-------------------------------------

//------------ Function that defines the action to be performed -----------------------

public void actionPerformed(ActionEvent e)
{
Object src = e.getSource();  //gets the info as to what is clicked & stores it into buffer

if(src.equals(buttonNew) || src.equals(NewFile))
{
newFile();
}

else if(src.equals(buttonOpen) || src.equals(OpenFile))
{
openFile();
}

else if (src.equals(buttonSave) || src.equals (SaveFile))
{
saveFile();
}

else if(src.equals(buttoncut) || src.equals (Cut))
{
Cut();
}

else if (src.equals(buttoncopy) || src.equals (Copy))
{
Copy();
}
else if(src.equals(buttonpaste) || src.equals (Paste))
{
Copy();
}
else if(src.equals (About))
{
About();
}
}
//------------------ Function definition ends here-----------------------------------------

//------------- Definition for functions called by above function---------------------------
void newFile()
{
JFileChooser newfile = new JFileChooser();
//int option = newfile.showNewDialog(this);
int option = JOptionPane.showConfirmDialog(frame,"Do you want to save the current file","",JOptionPane.YES_NO_CANCEL_OPTION);
if (option == JOptionPane.YES_OPTION)
{
saveFile();
}
if (option == JOptionPane.NO_OPTION)
{
this.txtPad.setText(""); 
}
if (option == JOptionPane.CANCEL_OPTION)
{
exitFile();
}
}

void saveFile()
{

JFileChooser save = new JFileChooser();
int option  = save.showSaveDialog(this);
if(option == JFileChooser.APPROVE_OPTION)
{
try
{
BufferedWriter out = new BufferedWriter(new FileWriter(save.getSelectedFile().getPath()));
out.write(this.txtPad.getText());
out.close();
}//Basically this will buffer the txt stuff to a file at the specified path & then save it there. 

catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
}

void openFile()
{
JFileChooser open = new JFileChooser();
int option = open.showOpenDialog(this);
if(option == JFileChooser.APPROVE_OPTION) //if I select a file to open (i.e. I click O.K.)
{
this.txtPad.setText(""); // empty the text pad for new file
try
{
Scanner scan = new Scanner(new FileReader(open.getSelectedFile().getPath())); // create a scanner to scan the file being opened
while(scan.hasNext()) //while there is more read
this.txtPad.append(scan.nextLine() +  "\n"); //read the file and append to next empty line
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
}
}

void exitFile()
{
System.out.println ("Do you want to exit the application?");
}

void Cut()
{
cutAction cut = new cutAction();
cut.CutAction(word, picture);
}

void Copy()
{
//TCPopUpEventQueue copy = new CopyAction();
//copy.CopyAction();
/**TextTransfer CopyObject = new TextTransfer();
CopyObject.setClipboardContents(aArgument);
Paste();**/
}

void Paste()
{
/***TextTransfer PstObject = new TextTransfer();
//PstObject.setClipboardContents(aArgument);
System.out.println("Clipboard Contents:" + PstObject.getClipboardContents());***/

//TCPopUpEventQueue pst = new PasteAction();
//pst.PasteAction();
}

void About()
{

JFrame Hframe = new JFrame ("About");
Hframe.setSize(300,300);
Hframe.setVisible(true);
Hframe.setDefaultCloseOperation(Hframe.HIDE_ON_CLOSE);

JLabel Hlabel = new JLabel("WritePad Version:1.1" +
						  "\n" + " Created By: Harsh Bhasin");
Hlabel.setFont(new Font("serif",Font.PLAIN,14));
Hframe.add(Hlabel);
}


class WindowHandler extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
exitFile();
}
}

public abstract class BasicAction extends AbstractAction
{

//BasicAction(String text,Icon icon);

JTextComponent comp;

public void BasicAction(String text, Icon icon)
{
super(text,icon);
putValue(Action.SHORT_DESCRIPTION, text);
}
public void setTextComponent(JTextComponent comp)
{
this.comp = comp;
}
public abstract void actionPerformed(ActionEvent e);
}


[color="#FF0000"]BasicAction()[/color] //default constructor
{
super(text,icon);
}

public class cutAction extends BasicAction
{
public void CutAction(String text,Icon icon)
{
super(text,icon);
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("Ctrl X"));
}
public void actionPerformed(ActionEvent e)
{
comp.cut();
}

public boolean isEnabled()
{
return ((comp != null) && (comp.isEditable()) && (comp.getSelectedText() != null));
}
}


 
public static void main (String args[])
{
WritePad WP = new WritePad();
}
}



Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Trying to create a java notepad

#2 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: Trying to create a java notepad

Posted 09 August 2010 - 02:51 PM

The constructor is public or private or what ?.

and your class is abstract..humm abstract class can not be instantiated.

This post has been edited by guidojavier: 09 August 2010 - 03:11 PM

Was This Post Helpful? 0
  • +
  • -

#3 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Trying to create a java notepad

Posted 09 August 2010 - 02:53 PM

remove the void return type:

 public void BasicAction(String text, Icon icon) {


You are missing a default constructor as well. Here's a fix:

public abstract class BasicAction extends AbstractAction {
//BasicAction(String text,Icon icon) {};
    JTextComponent comp;

    public BasicAction() {
        super();
    };

    public BasicAction(String text, Icon icon) {
        super(text, icon);
        putValue(Action.SHORT_DESCRIPTION, text);
    }

    public void setTextComponent(JTextComponent comp) {
        this.comp = comp;
    }

    public abstract void actionPerformed(ActionEvent e);
}



You also are missing default a cutAction constructor:

public class cutAction extends BasicAction {

    public cutAction() {
        super();
    }
    public void CutAction(String text, Icon icon) {
        //super(text, icon); <<<<<<<< method, not constructor
        putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("Ctrl X"));
    }

    public void actionPerformed(ActionEvent e) {
        comp.cut();
    }

    public boolean isEnabled() {
        return ((comp != null) && (comp.isEditable()) && (comp.getSelectedText() != null));
    }
}


This post has been edited by n8wxs: 09 August 2010 - 03:22 PM

Was This Post Helpful? 1
  • +
  • -

#4 jingoria  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 96
  • Joined: 22-October 07

Re: Trying to create a java notepad

Posted 10 August 2010 - 12:04 PM

View Postn8wxs, on 09 August 2010 - 01:53 PM, said:

remove the void return type:

 public void BasicAction(String text, Icon icon) {


You are missing a default constructor as well. Here's a fix:

public abstract class BasicAction extends AbstractAction {
//BasicAction(String text,Icon icon) {};
    JTextComponent comp;

    public BasicAction() {
        super();
    };

    public BasicAction(String text, Icon icon) {
        super(text, icon);
        putValue(Action.SHORT_DESCRIPTION, text);
    }

    public void setTextComponent(JTextComponent comp) {
        this.comp = comp;
    }

    public abstract void actionPerformed(ActionEvent e);
}



You also are missing default a cutAction constructor:

public class cutAction extends BasicAction {

    public cutAction() {
        super();
    }
    public void CutAction(String text, Icon icon) {
        //super(text, icon); <<<<<<<< method, not constructor
        putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("Ctrl X"));
    }

    public void actionPerformed(ActionEvent e) {
        comp.cut();
    }

    public boolean isEnabled() {
        return ((comp != null) && (comp.isEditable()) && (comp.getSelectedText() != null));
    }
}




Thanks a lot n8wxs. That worked and makes sense.

Once again Thanks for the detailed explaination.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1