Eu quero gravar em formato de texto, ou de doc, ou até mesmo de xls.
Segue abaixo todo o código fonte:
[code]import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.PrintStream;
import java.io.Serializable;
import java.util.HashMap;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class TesteJOptionPane extends JFrame {
private JButton inserir;
private JButton fechar;
private JButton listar;
private JButton salvar;
private JButton abrir;
private JLabel labelNome;
private JLabel labelEmpresa;
private JLabel labelFone;
private JLabel labelComent;
private static JTextField textNome;
private static JTextField textEmpresa;
private static JTextField textFone;
private static JFrame frame;
// Cria um campo de texto que exibe um caracter no lugar do texto digitado
// geralmente utilizado como campo de senha
// private JPasswordField textSenha;
private JTextArea textComent;
private JScrollPane paneComent;
TextArea txtListaObjetos;
TextArea textArea;
String clientes = "C:\\java\\Clientes.txt";
int nroClientes = 0;
Vet[] clt = new Vet[100];
public TesteJOptionPane() {
super("Cadastro de Clientes");
labelNome = new JLabel("Nome");
labelEmpresa = new JLabel("Empresa");
labelFone = new JLabel("Fone");
labelComent = new JLabel("Comentário");
// Campos de Texto
textNome = new JTextField(26);
textEmpresa = new JTextField(26);
textFone = new JTextField(26);
// textFone = new JPasswordField(25);
// textSenha.setEchoChar('*');
textComent = new JTextArea(10, 25);
textComent.setLineWrap(true);
txtListaObjetos = new TextArea("Lista de Objetos\n", 5, 39);
textArea = new TextArea("Leitura de arquivos\n", 10, 39);
// Painel de rolagem
paneComent = new JScrollPane(textComent,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
inserir = new JButton("Inserir");
listar = new JButton("Listar");
salvar = new JButton("Salvar");
fechar = new JButton("Fechar");
abrir = new JButton("Abrir Arquivo");
Container pane = this.getContentPane();
pane.setLayout(new FlowLayout(FlowLayout.LEFT));
// adiciona componentes ao painel
pane.add(labelNome);
pane.add(textNome);
pane.add(labelEmpresa);
pane.add(textEmpresa);
pane.add(labelFone);
pane.add(textFone);
pane.add(labelComent);
pane.add(paneComent);
pane.add(inserir);
pane.add(listar);
pane.add(salvar);
pane.add(fechar);
pane.add(txtListaObjetos);
pane.add(abrir, BorderLayout.SOUTH);
pane.add(textArea);
// pane.setLayout(null);
// Seta na Janela (Posição X, Posição Y, Largura, Altura)
// inserir.setBounds(0, 0, 100, 22);
// fechar.setBounds(0, 30, 100, 22);
// Aqui setaremos o Tamanho da Janela e visibilidade.
this.setSize(320, 720);
this.setContentPane(pane);
this.setVisible(true);
class AcaoLeitura implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
JFileChooser fileChooser = new JFileChooser(".");
fileChooser.showOpenDialog(frame);
File arquivo = fileChooser.getSelectedFile();
if (arquivo != null) {
textArea.setText("");
String s = LeitorBuffer.ler(arquivo);
textArea.append(s);
}
}
}
abrir.addActionListener(new AcaoLeitura());
class Inserir implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
if (nroClientes < 10) {
// insere um novo objeto no array
clt[nroClientes++] = new Vet(textNome.getText(),
textEmpresa.getText(), textFone.getText());
textNome.setText("");
textEmpresa.setText("");
textFone.setText("");
}
}
}
inserir.addActionListener(new Inserir());
class Listar implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
if (nroClientes < 10) {
txtListaObjetos.setText("");
for (int i = 0; i < nroClientes; i++) {
txtListaObjetos.appendText(clt[i].getNome() + " " +
clt[i].getEmpresa() + " " + clt[i].getFone() + '\n');
}
}
}
}
listar.addActionListener(new Listar());
class Salvar implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
try {
ObjectOutputStream gravacao = new ObjectOutputStream(new FileOutputStream(clientes));
gravacao.writeObject(clt);
gravacao.close();
showStatus("Operação realizada com sucesso!");
}
catch(Exception e) { showStatus(e.getMessage());}
}
}
salvar.addActionListener(new Salvar());
fechar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonConfirmaActionPerformed(e);
}
});
// Adiciona uma ação ao clicar no botão
/*
* inserir.addActionListener(new ActionListener() { public void
* actionPerformed(ActionEvent e) { buttonMensagemActionPerformed(e); //
* chama o Método buttonMensagemActionPerformed. } });
*/
// return true;
}
/*
* private void buttonMensagemActionPerformed(ActionEvent e) {
* JOptionPane.showMessageDialog(this, "ISSO EH UM TESTE", "FUNCIONA!",
* JOptionPane.INFORMATION_MESSAGE);
*
* /* Esse método é o responsável por mostrar uma Dialog ao clicar no botão
* MENSAGEM.
*/
public void showStatus(String message) {
// TODO Auto-generated method stub
}
private void buttonConfirmaActionPerformed(ActionEvent e) {
int ret = JOptionPane.showConfirmDialog(this, "Deseja Fechar?",
"Fechar", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE);
if (ret == JOptionPane.OK_OPTION) {
System.exit(0);
}
}
class Vet implements Serializable {
public String Nome;
public String Empresa;
public String Fone;
public Vet(String aNome, String aEmpresa, String aFone) {
Nome = aNome;
Empresa = aEmpresa;
Fone = aFone;
}
public String getNome() {
return Nome;
}
public String getEmpresa() {
return Empresa;
}
public String getFone() {
return Fone;
}
}
// Não deixa alterar os campos seuTextArea.setEditable(false);
public static void main(String[] args) throws FileNotFoundException {
TesteJOptionPane p = new TesteJOptionPane();
}
}
[/code]
E a classe que necessita:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class LeitorBuffer {
public static String ler(File arquivo) {
StringBuffer sb = new StringBuffer();
try { FileReader reader = new FileReader(arquivo);
BufferedReader bufReader = new BufferedReader(reader);
String s; do {
s = bufReader.readLine();
if (s!=null) {
sb.append( s + "\r\n" );
}
} while (s!=null);
bufReader.close();
reader.close();
}
catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
}
Eu quero que funcione a gravação dos dados em um arquivo txt, doc, ou xls, e depois vou tentar formatar eles, mas não to conseguindo nem fazer gravar os dados direito. Me ajudem por favor