Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: JTextArea output problem

  1. #1
    Junior Member grimx's Avatar
    Join Date
    Jul 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default JTextArea output problem

    using sun-jdk 1.6.0.20 on Gentoo 32 bit

    When i create a text file and then open it back up with the below applcation,
    i get little squares appended to the end of the text.
    The appended characters only show up in the JTextArea, but they do not show up in any text editors on my computer.

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
     
    public class FileIO extends JFrame implements ActionListener
    {
    	JButton btn_open;
    	JButton btn_save;
    	JButton btn_clear;
    	JTextField file_path;
    	JTextArea stuff;
     
    	public FileIO()
    	{
    		super("FILE IO Example");
    		setSize(300, 300);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		GridBagLayout bag_layout = new GridBagLayout();
    		setLayout(bag_layout);
     
    		GridBagConstraints c = new GridBagConstraints();
    		//TextField file_path
    		file_path = new JTextField(40);
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.gridx = 0;
    		c.gridwidth = 4;
    		c.gridy = 0;
    		c.gridheight = 1;
    		c.weightx = 1.0;
    		add(file_path, c);
    		//TextArea stuff and JScrollPane scroll
    		stuff = new JTextArea(40, 40);
    		JScrollPane scroll = new JScrollPane(stuff, 
    				JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,  JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    		c.fill = GridBagConstraints.BOTH;
    		c.gridy++;
    		c.gridheight = 4;
    		c.weighty = 1.0;
    		add(scroll, c);
    		//JButton btn_open
    		btn_open = new JButton("Open File");
    		btn_open.addActionListener(this);
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.gridy = 5;
    		c.gridheight = 1;
    		c.gridwidth = 1;
    		c.weightx = 1.0;
    		c.weighty = 0.0;
    		add(btn_open, c);
    		//JButton btn_save
    		btn_save = new JButton("Save File");
    		btn_save.addActionListener(this);
    		c.gridx++;
    		add(btn_save, c);
    		//JButton btn_clear
    		btn_clear = new JButton("Clear");
    		btn_clear.addActionListener(this);
    		c.gridx++;
    		add(btn_clear, c);
     
    		setVisible(true);
    	}
    	//Methods
    	private void Read_File()
    	{
    		File input = new File(file_path.getText());
    		FileReader reader;
    		JDialog msg;
    		char[] lines = new char[80];
     
    		try {
    			reader = new FileReader(input);
    			while(reader.read(lines) != -1)
    			{
    				stuff.append(new String(lines));
    			}
    			reader.close();
     
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    			msg = new JOptionPane("FILE NOT FOUND ERROR", JOptionPane.ERROR_MESSAGE,
    					JOptionPane.DEFAULT_OPTION).createDialog("ERROR_MESSAGE");
    			msg.setVisible(true);	
     
    		} catch (IOException e) {
    			e.printStackTrace();
    			msg = new JOptionPane("FILE IO ERROR", JOptionPane.ERROR_MESSAGE,
    					JOptionPane.DEFAULT_OPTION).createDialog("ERROR_MESSAGE");
    			msg.setVisible(true);
     
    		}
     
    	}
     
    	private void Write_File()
    	{
    		File output = new File(file_path.getText());
    		FileWriter w;
    		JDialog msg;
     
    		try {
    			w = new FileWriter(output);
    			stuff.write(w);
    			w.close();
    		} catch (IOException e) {
    			e.printStackTrace();
    			msg = new JOptionPane("FILE IO ERROR", JOptionPane.ERROR_MESSAGE,
    					JOptionPane.DEFAULT_OPTION).createDialog("ERROR_MESSAGE");
    			msg.setVisible(true);
    		}
     
    	}
    	//ActionListener
    	public void actionPerformed(ActionEvent evt)
    	{
    		Object source = evt.getSource();
     
    		if(source == btn_open)
    		{
    			Read_File();
    		}
    		else if(source == btn_save)
    		{
    			Write_File();
    		}
    		else if(source == btn_clear)
    		{
    			stuff.setText("");
    		}
     
    	}
    	//Main
    	public static void main(String[] args)
    	{
    		new FileIO();
     
    	}
    }
    Last edited by helloworld922; July 3rd, 2010 at 10:21 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: JTextArea output problem

    The method you have for reading in text is appending the extraneous characters that JTextFields don't know how to render (the unicode value 0), so they draw a box. This is due to the fact that your text file's length isn't an exact multiple of 80, and since it's initialized to a default value of (char)0, you're left with a bunch of (char)0 characters at the end.

    Try reading in the file this way:

    	//Methods
    	private void Read_File()
    	{
    		File input = new File(file_path.getText());
    		StringBuilder text = new StringBuilder();
    		try
    		{
    			Scanner reader = new Scanner(input);
    			while(reader.hasNext())
    			{
    				text.append(reader.nextLine());
    				text.append("\n"); // since nextLine() strips the '\n' character
    			}
    			stuff.append(text.toString());
    		}
    		catch (IOException e)
    		{
    			// TODO: Error handling
    			JOptionPane.showMessageDialog("Something went wrong!");
    		}
     
    	}
    Last edited by helloworld922; July 3rd, 2010 at 10:23 PM.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    grimx (July 3rd, 2010)

  4. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JTextArea output problem

    The problem is you're assuming that you are reading 80 bytes. Change the code to use the number of bytes read:
            int nbrRd = 0;  // use number of bytes read vs length of input array
     
            try {
                reader = new FileReader(input);
                while((nbrRd=reader.read(lines)) != -1)
                {
                    stuff.append(new String(lines, 0 , nbrRd));
                }

  5. #4
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: JTextArea output problem


  6. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: JTextArea output problem

    Quote Originally Posted by grimx View Post
    using sun-jdk 1.6.0.20 on Gentoo 32 bit

    When i create a text file and then open it back up with the below applcation,
    i get little squares appended to the end of the text.
    The appended characters only show up in the JTextArea, but they do not show up in any text editors on my computer.

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
     
    public class FileIO extends JFrame implements ActionListener
    {
    	JButton btn_open;
    	JButton btn_save;
    	JButton btn_clear;
    	JTextField file_path;
    	JTextArea stuff;
     
    	public FileIO()
    	{
    		super("FILE IO Example");
    		setSize(300, 300);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		GridBagLayout bag_layout = new GridBagLayout();
    		setLayout(bag_layout);
     
    		GridBagConstraints c = new GridBagConstraints();
    		//TextField file_path
    		file_path = new JTextField(40);
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.gridx = 0;
    		c.gridwidth = 4;
    		c.gridy = 0;
    		c.gridheight = 1;
    		c.weightx = 1.0;
    		add(file_path, c);
    		//TextArea stuff and JScrollPane scroll
    		stuff = new JTextArea(40, 40);
    		JScrollPane scroll = new JScrollPane(stuff, 
    				JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,  JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    		c.fill = GridBagConstraints.BOTH;
    		c.gridy++;
    		c.gridheight = 4;
    		c.weighty = 1.0;
    		add(scroll, c);
    		//JButton btn_open
    		btn_open = new JButton("Open File");
    		btn_open.addActionListener(this);
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.gridy = 5;
    		c.gridheight = 1;
    		c.gridwidth = 1;
    		c.weightx = 1.0;
    		c.weighty = 0.0;
    		add(btn_open, c);
    		//JButton btn_save
    		btn_save = new JButton("Save File");
    		btn_save.addActionListener(this);
    		c.gridx++;
    		add(btn_save, c);
    		//JButton btn_clear
    		btn_clear = new JButton("Clear");
    		btn_clear.addActionListener(this);
    		c.gridx++;
    		add(btn_clear, c);
     
    		setVisible(true);
    	}
    	//Methods
    	private void Read_File()
    	{
    		File input = new File(file_path.getText());
    		FileReader reader;
    		JDialog msg;
    		char[] lines = new char[80];
     
    		try {
    			reader = new FileReader(input);
    			while(reader.read(lines) != -1)
    			{
    				stuff.append(new String(lines));
    			}
    			reader.close();
     
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    			msg = new JOptionPane("FILE NOT FOUND ERROR", JOptionPane.ERROR_MESSAGE,
    					JOptionPane.DEFAULT_OPTION).createDialog("ERROR_MESSAGE");
    			msg.setVisible(true);	
     
    		} catch (IOException e) {
    			e.printStackTrace();
    			msg = new JOptionPane("FILE IO ERROR", JOptionPane.ERROR_MESSAGE,
    					JOptionPane.DEFAULT_OPTION).createDialog("ERROR_MESSAGE");
    			msg.setVisible(true);
     
    		}
     
    	}
     
    	private void Write_File()
    	{
    		File output = new File(file_path.getText());
    		FileWriter w;
    		JDialog msg;
     
    		try {
    			w = new FileWriter(output);
    			stuff.write(w);
    			w.close();
    		} catch (IOException e) {
    			e.printStackTrace();
    			msg = new JOptionPane("FILE IO ERROR", JOptionPane.ERROR_MESSAGE,
    					JOptionPane.DEFAULT_OPTION).createDialog("ERROR_MESSAGE");
    			msg.setVisible(true);
    		}
     
    	}
    	//ActionListener
    	public void actionPerformed(ActionEvent evt)
    	{
    		Object source = evt.getSource();
     
    		if(source == btn_open)
    		{
    			Read_File();
    		}
    		else if(source == btn_save)
    		{
    			Write_File();
    		}
    		else if(source == btn_clear)
    		{
    			stuff.setText("");
    		}
     
    	}
    	//Main
    	public static void main(String[] args)
    	{
    		new FileIO();
     
    	}
    }
    What do you mean by text editors?

    Oh, what a second.....file_path.setText("Your text here");

    is that what you meant?

  7. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: JTextArea output problem

    Text editors are external programs that allow you to edit the text of files. Examples are Windows Notepad, and Gedit for Gnome.

Similar Threads

  1. How To: Add line numbers to your JTextArea
    By Freaky Chris in forum Java Swing Tutorials
    Replies: 11
    Last Post: October 15th, 2013, 10:22 PM
  2. Output problem (newbie)
    By Asido in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 8th, 2010, 12:19 PM
  3. xml output problem
    By tsili in forum Java Theory & Questions
    Replies: 4
    Last Post: May 25th, 2010, 04:56 AM
  4. [SOLVED] how to print for-loop output to JTextArea
    By voltaire in forum AWT / Java Swing
    Replies: 2
    Last Post: May 13th, 2010, 02:43 PM
  5. Display output from a file using regular expression
    By jazz2k8 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 29th, 2008, 09:33 AM