10 Replies - 703 Views - Last Post: 12 December 2011 - 03:19 PM Rate Topic: -----

#1 dubp65  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 10-October 11

Text editor - Need help with a few features (save, exit, etc)

Posted 11 December 2011 - 09:51 PM

Hey guys!

I'm working on a text editor and I've ran into a wall so to speak. I've managed to get some features working but for the others I have not been able to implement the right code and keep getting errors so I've come to you guys for some help.


I need to implement a save method which will save the current text, just like in notepad. I also need guidance in the 'exit' and 'about' methods as you would also see in notepad. Here's my code:

package editor;

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


public class Editor implements ActionListener
{
    JFrame myEditor = new JFrame();
    JMenuBar myMenuBar = new JMenuBar();
    JMenu fileMenu = new JMenu("File");
    JMenuItem openItem = new JMenuItem("Open");
    JMenuItem saveItem = new JMenuItem("Save");
    JMenuItem exitItem = new JMenuItem("Exit");
    
    JMenu editMenu = new JMenu("Edit");
    JMenuItem cutItem = new JMenuItem("Cut");
    JMenuItem copyItem = new JMenuItem("Copy");
    JMenuItem pasteItem = new JMenuItem("Paste");
    
    JMenu formatMenu = new JMenu("Format");
    JMenuItem fontItem = new JMenuItem("Font");
        
    JFileChooser fileChooser = new JFileChooser();
    
    JTextArea editArea = new JTextArea("Enter your text here");

   public Editor()
   {
      myEditor.setTitle("My Editor");
      myEditor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
      openItem.addActionListener(this);
      cutItem.addActionListener(this);
      copyItem.addActionListener(this);
      pasteItem.addActionListener(this);
      fontItem.addActionListener(this);
      
      fileMenu.add(exitItem);
      fileMenu.add(saveItem);
      fileMenu.add(openItem);
      editMenu.add(cutItem);
      editMenu.add(copyItem);
      editMenu.add(pasteItem);
      formatMenu.add(fontItem);    
      
      myMenuBar.add(fileMenu);
      myMenuBar.add(editMenu);
      myMenuBar.add(formatMenu);
      
      myEditor.setJMenuBar(myMenuBar);
      myEditor.add(editArea);
      myEditor.setVisible(true);
      myEditor.setSize(500,500);

   }
    public static void main(String[] args) {
        Editor firstEditor = new Editor();
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == openItem)
        {
            int returnVal = fileChooser.showOpenDialog(fileMenu);

            if (returnVal == JFileChooser.APPROVE_OPTION)
            {
                File file = fileChooser.getSelectedFile();
                ReadText reader = new ReadText(file);          
            } 
        if (e.getSource() == exitItem)
        {
            System.exit(0);
            
        }
        }
        
        
        if (e.getSource() == cutItem)
        {
            editArea.cut();
        }
        if (e.getSource() == copyItem)
        {
            editArea.copy();
        }
        if (e.getSource() == pasteItem)
        {
            editArea.paste();
        }
        if (e.getSource() == fontItem)
        {
            FontChooser fontDialogBox = new FontChooser(editArea);
            fontDialogBox.setVisible(true);
        }
    }
}


Any help is greatly appreciated :)!

Is This A Good Question/Topic? 0
  • +

Replies To: Text editor - Need help with a few features (save, exit, etc)

#2 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Text editor - Need help with a few features (save, exit, etc)

Posted 11 December 2011 - 10:51 PM

Have you learned about file input and output? JFileChooser is a good class to get to know. So are FileWriter and BufferedWriter.
Was This Post Helpful? 0
  • +
  • -

#3 dubp65  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 10-October 11

Re: Text editor - Need help with a few features (save, exit, etc)

Posted 11 December 2011 - 11:30 PM

View PostSheph, on 11 December 2011 - 10:51 PM, said:

Have you learned about file input and output? JFileChooser is a good class to get to know. So are FileWriter and BufferedWriter.


I've looked over it briefly, but I don't know it very well. I'll go ahead and look into those more.

Thanks
Was This Post Helpful? 0
  • +
  • -

#4 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Text editor - Need help with a few features (save, exit, etc)

Posted 11 December 2011 - 11:49 PM

public void save(String fileName) throws IOException {
	File textFile = new File(fileName + ".txt");
	BufferedWriter out = new BufferedWriter( new FileWriter(textFile) );
		
	out.write("hi");
	// write what you need to the file
		
	out.close(); // closes steams and flushes data to file
}
That's what a save method could look like for the menu. My stupid cat stepped on my keyboard pressing the refresh button and knocking off my wireless at the same time while I was typing this! I'll have to remember to type everything in my editor first next time :)
Was This Post Helpful? 0
  • +
  • -

#5 x68zeppelin80x  Icon User is offline

  • D.I.C Addict

Reputation: 130
  • View blog
  • Posts: 576
  • Joined: 07-March 09

Re: Text editor - Need help with a few features (save, exit, etc)

Posted 12 December 2011 - 01:06 AM

View PostSheph, on 12 December 2011 - 06:49 AM, said:

BufferedWriter out = new BufferedWriter( new FileWriter(textFile) );



I thought PrintWriter was more efficient than BufferedWriter, because it supports printf(), format(), etc.
Was This Post Helpful? 0
  • +
  • -

#6 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Text editor - Need help with a few features (save, exit, etc)

Posted 12 December 2011 - 02:52 AM

Yeah you could use PrintWriter. I don't know if it's more efficient, but if it is easier to use for you, go for it.
Was This Post Helpful? 0
  • +
  • -

#7 dubp65  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 10-October 11

Re: Text editor - Need help with a few features (save, exit, etc)

Posted 12 December 2011 - 09:16 AM

View PostSheph, on 11 December 2011 - 11:49 PM, said:

public void save(String fileName) throws IOException {
	File textFile = new File(fileName + ".txt");
	BufferedWriter out = new BufferedWriter( new FileWriter(textFile) );
		
	out.write("hi");
	// write what you need to the file
		
	out.close(); // closes steams and flushes data to file
}
That's what a save method could look like for the menu. My stupid cat stepped on my keyboard pressing the refresh button and knocking off my wireless at the same time while I was typing this! I'll have to remember to type everything in my editor first next time :)

When I put that code in it doesn't seem to do anything. Am I missing something?
Was This Post Helpful? 0
  • +
  • -

#8 x68zeppelin80x  Icon User is offline

  • D.I.C Addict

Reputation: 130
  • View blog
  • Posts: 576
  • Joined: 07-March 09

Re: Text editor - Need help with a few features (save, exit, etc)

Posted 12 December 2011 - 09:56 AM

Try a JFileChooser, this will propt you with a save window. Just go to the folder you want, name the file and presto. A new file is created!

// Dont forget to include these
import java.io.FileWriter;
import java.io.PrintWriter;
import javax.swing.JFileChooser;

// Create a JFileChooser object called save
JFileChooser save = new JFileChooser();

// Show a save dialog and get selection 
int option = save.showSaveDialog(null);

// If user selected the "Open" option, do the following
if (option == JFileChooser.APPROVE_OPTION)
{
   String filename = save.getSelectedFile().getPath();

   try
   {
      PrintWriter out = new PrintWriter(new FileWriter(filename));
      out.write("This is a test.");
      out.close();
   } 
   catch (Exception e)
   {
      System.out.println(e.getMessage());
   }
}


This post has been edited by x68zeppelin80x: 12 December 2011 - 10:02 AM

Was This Post Helpful? 1
  • +
  • -

#9 dubp65  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 10-October 11

Re: Text editor - Need help with a few features (save, exit, etc)

Posted 12 December 2011 - 11:15 AM

Thanks for the response zepplin. 1 problem though, when I use your code the save as dialog opens up and when I hit cancel then my text editor will show. Not sure why this happens.

I looked online for a bit and found other code that has worked but I'm still having issues. Primarily:

-When I use the save feature, I have to click twice on save for the file to save.

-Also when using the save feature, I have to click cancel twice to cancel and then it says "your file has been saved". I need it to just close the save dialog box and return to the editor.

-2 windows pop up when I run my file. One is my text editor with all the features and the other is a blank window with no features or name. I need to get rid of this!

Here's my revised code since the OP.
package editor;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import simplerw.ReadText;


public class Editor extends JFrame implements ActionListener
{
    public static void main(String[] args){
     JFrame frm = new Editor();   
     frm.setVisible(true);
    }
    

    JFrame myEditor = new JFrame();
    JMenuBar myMenuBar = new JMenuBar();
    JMenu fileMenu = new JMenu("File");
    JMenuItem openItem = new JMenuItem("Open");
    JMenuItem saveItem = new JMenuItem("Save");
    JMenuItem exitItem = new JMenuItem("Exit");
    
    JMenu editMenu = new JMenu("Edit");
    JMenuItem cutItem = new JMenuItem("Cut");
    JMenuItem copyItem = new JMenuItem("Copy");
    JMenuItem pasteItem = new JMenuItem("Paste");
    
    JMenu formatMenu = new JMenu("Format");
    JMenuItem fontItem = new JMenuItem("Font");
    
    private JFileChooser fileChooser = new JFileChooser();
   
    JTextArea editArea = new JTextArea("Enter your text here");
        
    private JTextField textField;
    
    
   public Editor()
   {
      myEditor.setTitle("My Editor");
      myEditor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
      exitItem.addActionListener(this);
      saveItem.addActionListener(this);
      openItem.addActionListener(this);
      cutItem.addActionListener(this);
      copyItem.addActionListener(this);
      pasteItem.addActionListener(this);
      fontItem.addActionListener(this);
      
      
      fileMenu.add(exitItem);
      fileMenu.add(saveItem);
      fileMenu.add(openItem);
      editMenu.add(cutItem);
      editMenu.add(copyItem);
      editMenu.add(pasteItem);
      formatMenu.add(fontItem);    
      
      myMenuBar.add(fileMenu);
      myMenuBar.add(editMenu);
      myMenuBar.add(formatMenu);
      
      myEditor.setJMenuBar(myMenuBar);
      myEditor.add(editArea);
      myEditor.setVisible(true);
      myEditor.setSize(500,500);
      

      
   }
  
protected void save() throws IOException{
    JFileChooser dialog = new JFileChooser();
    File file = new File("file.txt");
    dialog.setSelectedFile(file);
    if(dialog.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
        file = dialog.getSelectedFile();
        FileWriter writer = new FileWriter(file);
        writer.append(editArea.getText());
        writer.close();
    }
            if(dialog.showSaveDialog(this) == JFileChooser.CANCEL_OPTION) {
            dialog.cancelSelection();
            }
        }
    

    
    @Override
    public void actionPerformed(ActionEvent e)
    {
        
        String action = e.getActionCommand();
        
        if("Save".equals(action)){
            try{
                save();
                JOptionPane.showMessageDialog(null, "File has been saved.");
            }catch (IOException e1) {
                JOptionPane.showMessageDialog(null, "Could not save the file.");
            }
        }

        
            else if (e.getSource() == openItem)
        {
            int returnVal = fileChooser.showOpenDialog(fileMenu);

            if (returnVal == JFileChooser.APPROVE_OPTION)
            {
                File file = fileChooser.getSelectedFile();
                ReadText reader = new ReadText(file);          
            }
        } 
            else if (action.equals("Exit")){
                 int choice = JOptionPane.showConfirmDialog(null,
                 "Do you really want to exit?", "Confirm Exit", JOptionPane.YES_NO_OPTION);
		 if(choice == JOptionPane.YES_OPTION){
                     System.exit(0);
                 }    
            }   
                
        
            
        
            else if (e.getSource() == cutItem)
        {
            editArea.cut();
        }
            else  if (e.getSource() == copyItem)
        {
            editArea.copy();
        }
            else if (e.getSource() == pasteItem)
        {
            editArea.paste();
        }
            else  if (e.getSource() == fontItem)
        {
            FontChooser fontDialogBox = new FontChooser(editArea);
            fontDialogBox.setVisible(true);
        }
    }
 
}



Was This Post Helpful? 0
  • +
  • -

#10 dubp65  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 10-October 11

Re: Text editor - Need help with a few features (save, exit, etc)

Posted 12 December 2011 - 03:00 PM

Any ideas?
Was This Post Helpful? 0
  • +
  • -

#11 dubp65  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 10-October 11

Re: Text editor - Need help with a few features (save, exit, etc)

Posted 12 December 2011 - 03:19 PM

Sorry to keep bumping my thread but I figured out how to get the second window to stop popping up. Now the only issue is with the 2 clicks needed for save / cancel on the save file part
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1