Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Cannot print text from a file to a JtextArea

  1. #1
    stevenfriz is offline Member
    Join Date
    Feb 2012
    Posts
    28
    Rep Power
    0

    Post Cannot print text from a file to a JtextArea

    Hello, I am trying to create my own customized text editor, but I can't figure out how to have the contents of the file read and printed to my JTextArea text. The code needs to be placed in the openFileHandler() method. Also, I can't figure out how to have the text wrap to the next line if it comes to the border of the screen. Anyway, below is my code. I do appreciate any help.


    Java Code:
    package texteditor;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.*;
    
    public class TextEditor extends JFrame implements ActionListener
    {
        
        private JMenuItem newFile = new JMenuItem("New");
        private JMenuItem openFile = new JMenuItem("Open");
        private JMenuItem exit = new JMenuItem("Exit");
        private JMenuItem save = new JMenuItem("Save");
        private JMenuItem saveAs = new JMenuItem("Save as");
        JFrame frame = new JFrame("FrizPad");
        JTextArea text = new JTextArea();
        
        private FileWriter write;
        private PrintWriter print;
        
        public void frameHandler()
        {
            
        
        JScrollPane scroll = new JScrollPane(text);
        
        text.setWrapStyleWord(true);
        
        JMenuBar menubar = new JMenuBar();
        JMenu file = new JMenu("File");
        JMenu edit = new JMenu("Edit");
        JMenu view = new JMenu("View");
        
        
        newFile.addActionListener(this);
        openFile.addActionListener(this);
        save.addActionListener(this);
        saveAs.addActionListener(this);
        exit.addActionListener(this);
        
        file.add(newFile);
        file.addSeparator();
        file.add(openFile);
        file.addSeparator();
        file.add(save);
        file.addSeparator();
        file.add(saveAs);
        file.addSeparator();
        file.add(exit);
        
        menubar.add(file);
        menubar.add(edit);
        menubar.add(view);
        
        frame.setJMenuBar(menubar);
        frame.add(scroll);
        frame.setSize(450,450);
        frame.setResizable(true);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                }
        public void newFileHandler()
        {
            JFileChooser chooser = new JFileChooser();
            chooser.setDialogTitle("New File");
            chooser.showSaveDialog(null);
        }
        public void openFileHandler() throws FileNotFoundException, IOException
        {
            int status;
            JFileChooser chooser = new JFileChooser();
            chooser.setDialogTitle("Open File");
            status = chooser.showOpenDialog(null);
         
            if(status == JFileChooser.APPROVE_OPTION)
            {
                
                
                
                
                
            }
            else if(status == JFileChooser.CANCEL_OPTION)
            {
                status = JFileChooser.ABORT;
            }
            
        }
        public void saveFileHandler()
        {
            
        }
        public void saveAsFileHandler()
        {
            
        }
        @Override
        public void actionPerformed(ActionEvent n)
        {
            if(n.getSource() == newFile)
            {
                TextEditor initiate = new TextEditor();
                initiate.newFileHandler();
            }
            if(n.getSource() == openFile)
            {
                TextEditor initiate = new TextEditor();
                try {
                    initiate.openFileHandler();
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(TextEditor.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(TextEditor.class.getName()).log(Level.SEVERE, null, ex);
                }
                  
                
            }
            if(n.getSource() == save)
            {
                TextEditor initiate = new TextEditor();
                initiate.saveFileHandler();
            }
            if(n.getSource() == saveAs)
            {
                TextEditor initiate = new TextEditor();
                initiate.saveAsFileHandler();
            }
            if(n.getSource() == exit)
            {
                System.exit(0);
            }
        }
        
        public static void main(String args[])
        {
            TextEditor initiate = new TextEditor();
             initiate.frameHandler();
        }
    
        
        }
    Last edited by Fubarable; 02-07-2012 at 03:14 AM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Cannot print text from a file to a JtextArea

    I can't see any code in there that even attempts to read the file. Anyway JTextComponent already has a method to do this: read().

  3. #3
    stevenfriz is offline Member
    Join Date
    Feb 2012
    Posts
    28
    Rep Power
    0

    Default Re: Cannot print text from a file to a JtextArea

    That's the thing. I don't know the code. The read method does not work for me. I think it has to do with the fact the JFrame and the JTextArea runs first. I'm making my own version of notepad, and when I click open and open a text file, it needs to load the file into the JTextArea. But it doesn't.

    Any ideas other than the read method suggested?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Cannot print text from a file to a JtextArea

    Quote Originally Posted by stevenfriz View Post
    The read method does not work for me.
    Post a SSCCE (Short, Self Contained, Compilable and Executable) example that shows read() doesn't work or it didn't happen.

    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    stevenfriz is offline Member
    Join Date
    Feb 2012
    Posts
    28
    Rep Power
    0

    Default Re: Cannot print text from a file to a JtextArea

    Java Code:
    public void openFileHandler() throws FileNotFoundException, IOException
        {
            int status;
            JFileChooser chooser = new JFileChooser();
            status = chooser.showOpenDialog(null);
            
            if(status == JFileChooser.APPROVE_OPTION)
            {
     
            File opened = chooser.getSelectedFile();
            
            
            FileReader fileReader = new FileReader(opened);
            BufferedReader buffReader = new BufferedReader(fileReader);
            
            text.read(buffReader, "opened");
            }
            else if(status == JFileChooser.CANCEL_OPTION)
            {
                status = JFileChooser.ABORT;
            }
            
            
            
            
            
        }
    Here is the method that has the read() method. I've used it in many different ways from different help websites that involve the read() method, but nothing will work. The JTextArea refuses to display the text.
    Last edited by Fubarable; 02-07-2012 at 03:14 AM. Reason: code tags added

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Cannot print text from a file to a JtextArea

    If you seriously need our help, then I second Darryl's important advice above: consider creating and posting an SSCCE (the link will explain what's involved) as this will offer you the best chance of getting decent help quickly. It is asking some extra effort from you, and is purely optional on your part, and I'd say don't bother doing it if our help isn't that important to you right now. But on the other hand, if your back's against the wall, do consider it.

    Regardless, best of luck.

    edit: [code] [/code] tags added to your two posts above with code.
    Last edited by Fubarable; 02-07-2012 at 03:15 AM.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Cannot print text from a file to a JtextArea

    One thing I do notice in your code posts is that you're creating a new TextEditor object that is distinct from the current visualized one and then trying to read a file into the new non-displayed object. This may work -- the text may be read into the JTextField of this TextEditor object, but since it's not the TextEditor object that is displayed, it won't do you much good. Perhaps you should instead consider not creating a new TextEditor object and instead simply using the TextEditor object that already exists, the so-called (and somewhat hard to describe) this.

  8. #8
    stevenfriz is offline Member
    Join Date
    Feb 2012
    Posts
    28
    Rep Power
    0

    Default Re: Cannot print text from a file to a JtextArea

    Are you talking about the ActionListeners that have TextEditor initiate = new TextEditor(); ? If so, are you saying I need to replace the initiate. with this.?

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Cannot print text from a file to a JtextArea

    Quote Originally Posted by stevenfriz View Post
    Are you talking about the ActionListeners that have TextEditor initiate = new TextEditor(); ? If so, are you saying I need to replace the initiate. with this.?
    Yes. Again initiate holds a reference to a TextEditor object that is completely distinct from the current TextEditor object that is being displayed. It makes no sense to make another object when the current one, the "this" one, is active and displayed. Worse than that, it serves no purpose to try to read text into the distinct object since it's not visualized. Just get rid of this line: TextEditor initiate = new TextEditor(); and make your method calls directly.

  10. #10
    stevenfriz is offline Member
    Join Date
    Feb 2012
    Posts
    28
    Rep Power
    0

    Default Re: Cannot print text from a file to a JtextArea

    Fubarable, that advice right there is just what I needed! You were completely right. It works now. I appreciate it.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Cannot print text from a file to a JtextArea

    Quote Originally Posted by stevenfriz View Post
    Fubarable, that advice right there is just what I needed! You were completely right. It works now. I appreciate it.
    You're welcome. But still, you'd have had your answer a lot sooner if you posted an SSCCE. Also, please read my link on code tags.

    Luck!

  12. #12
    stevenfriz is offline Member
    Join Date
    Feb 2012
    Posts
    28
    Rep Power
    0

    Default Re: Cannot print text from a file to a JtextArea

    I understand. I'm brand new to forums; however, I will most certainly do as suggested next time I require help!

Similar Threads

  1. Replies: 10
    Last Post: 05-24-2010, 09:25 PM
  2. Replies: 17
    Last Post: 07-13-2009, 06:44 AM
  3. Print the text file and print preview them
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-23-2008, 12:04 AM
  4. Replies: 0
    Last Post: 07-17-2007, 04:30 PM
  5. viewing the contents of a text file in JTextArea
    By warship in forum New To Java
    Replies: 0
    Last Post: 07-17-2007, 03:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •