Quebra d elinha

4 respostas
L

eu tenho uma função p abrir um arquivo texto. ele faz quebra de linha normalmente, mas eu gostaria q a quebra de linha do arquivo n seja quando o tamanho do JtextArea acabe e sim de acaordo com a formatação do texto original

ou seja pular linha se no texto original a linha pular .

como fazer isso?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class Carregar extends JFrame{
  JTextArea textArea;
  JButton btn;
  JButton btc;
  public Carregar() {
    super("Carregando um arquivo SQL");
    Container c = getContentPane();
    FlowLayout layout = new FlowLayout(FlowLayout.LEFT);
    c.setLayout(layout);
    
    textArea = new JTextArea(10, 40);
    textArea.setLineWrap(true);
   
    
     class SQLfilter extends javax.swing.filechooser.FileFilter {
     public boolean accept(File f) {
         return f.isDirectory() || f.getName().toLowerCase().endsWith(".sql");
     }
     
     public String getDescription() {
         return "arquivos SQL";
     }
 }
    
     
     btc = new JButton ("Copiar");
    btc.addActionListener(
            new ActionListener(){
        public void actionPerformed(ActionEvent e){
        textArea.copy();
        }
    });
    
    btn = new JButton("Abrir Arquivo");
    btn.addActionListener(
       new ActionListener(){
          public void actionPerformed(ActionEvent e){
              JFileChooser fc = new JFileChooser();
                   fc.setFileFilter(new SQLfilter());  
              int res = fc.showOpenDialog(null);
              
                    
              if(res == JFileChooser.APPROVE_OPTION){
                 File arquivo = fc.getSelectedFile();
                 textArea.setText("");
                 
                   
                 try {
                    BufferedReader in = new BufferedReader(new FileReader(arquivo));
                    String str, texto = "";
                    while((str = in.readLine()) != null){
                        texto += str;
                    }
                    textArea.setText(texto);
                    in.close();
                    
                 } 
               
                 
                 catch (IOException ioe){
                     
                    // possiveis erros são tratatos aqui
                 }
              }
          }
       });
       
    c.add(textArea);
    c.add(btn);
    c.add (btc);
    
    setSize(500, 500);
    setVisible(true);
  }

  public static void main(String args[]){
    Carregar app = new Carregar();
    app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

ajudem -me

4 Respostas

vhuzalo

O setLineWrap deve ser false, esta proppriedade define se irá quebrar a linha automaticamente, no teu caso, isto não deve acontecer, então false!

[]'s

bernardo.rafael

Você tem que remover esta linha do seu código:

textArea.setLineWrap(true);

e adicionae estas duas

textArea.setLineWrap(false);

o false desabilita a quebra de linhas.

:wink:

L

mas n deu certo ja fiz isso e ele continua escrevendo tudo em uma linha só! e o q ue é pior, antes ele pegava e quando chegava na borda do JtextArea ele descia p outra linha agora ele ta escrevendo direto até o texto sumir.

vhuzalo

Acho que teu problema está na leitura do arquivo, quando o “in.readLine()” lê a linha do arquivo não está pegando a quebra de linha, tente fazer:

:thumbup:

Criado 21 de agosto de 2007
Ultima resposta 22 de ago. de 2007
Respostas 4
Participantes 3