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:
Code Java:
//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!");
}
}
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:
Code :
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));
}
Re: JTextArea output problem
Re: JTextArea output problem
Quote:
Originally Posted by
grimx
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.
Code Java:
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?
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.