qndo salvo um txt no salvar como… o txt gerado salva com quadradinhos na hora que vc passa pra proxima linha, como resolvo isso? P.S.: ta pegando só o campo relatorio.
segue ai o meu codigo:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.swing.*;
public class Relatorio extends JFrame {
private Container container;
private JLabel labelNome, labelData, labelRelatorio;
private JTextField fieldNome, fieldData;
private JTextArea areaRelatorio;
private JButton buttonSalvar, buttonCancelar;
private JScrollPane scroll;
private File fFile;
public Relatorio() {
super("Gerador de Relatório");
container = getContentPane();
container.setLayout(null);
labelNome = new JLabel("Nome");
labelNome.setBounds(10, 10, 50, 25);
fieldNome = new JTextField();
fieldNome.setBounds(70, 10, 280, 25);
container.add(labelNome);
container.add(fieldNome);
labelData = new JLabel("Data");
labelData.setBounds(10, 40, 200, 25);
fieldData = new JTextField();
fieldData.setBounds(70, 40, 280, 25);
container.add(labelData);
container.add(fieldData);
labelRelatorio = new JLabel("Relatório");
labelRelatorio.setBounds(10, 70, 290, 25);
areaRelatorio = new JTextArea(12, 22);
areaRelatorio.setBounds(70, 70, 280, 400);
areaRelatorio.setLineWrap(true);
scroll = new JScrollPane();
scroll.setBounds(70, 70, 280, 400);
scroll.setViewportView(areaRelatorio);
scroll.setHorizontalScrollBarPolicy
(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scroll.setVerticalScrollBarPolicy
(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
this.getContentPane().add(scroll, null);
container.add(labelRelatorio);
buttonSalvar = new JButton("Salvar Como...");
buttonSalvar.setBounds(90, 500, 120, 30);
buttonSalvar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonSalvarActionPerformed(evt);
}
});
buttonCancelar = new JButton("Cancelar");
buttonCancelar.setBounds(220, 500, 120, 30);
buttonCancelar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonCancelarActionPerformed(evt);
}
});
container.add(buttonSalvar);
container.add(buttonCancelar);
setSize(420, 600);
setVisible(true);
}
private void buttonCancelarActionPerformed(ActionEvent evt) {
this.setVisible(false);
this.dispose();
}
private void buttonSalvarActionPerformed(ActionEvent evt) {
boolean status=false;
status = saveFile ();
}
boolean saveFile () {
File file = null;
JFileChooser fc = new JFileChooser ();
// Começar no diretório atual
fc.setCurrentDirectory (new File ("."));
// Abrir caixa de escolha
int result = fc.showSaveDialog (this);
if (result == JFileChooser.CANCEL_OPTION) {
return true;
} else if (result == JFileChooser.APPROVE_OPTION) {
fFile = fc.getSelectedFile ();
if (fFile.exists ()) {
int response = JOptionPane.showConfirmDialog (null,
"Substituir o arquivo existente?","Confirmar substituição",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE);
if (response == JOptionPane.CANCEL_OPTION) return false;
}
return writeFile (fFile, areaRelatorio.getText());
} else {
return false;
}
} // saveFile
public static boolean writeFile (File file, String dataString) {
try {
PrintWriter out =
new PrintWriter (new BufferedWriter (new FileWriter (file)));
out.print (dataString);
out.flush ();
out.close ();
}
catch (IOException e) {
return false;
}
return true;
} // writeFile
}
Main:
class RelatorioMain{
public static void main(String args[]){
Relatorio relatorio;
relatorio = new Relatorio();
}
}