Arquivo para leitura de texto

1 resposta
G

Gostaria de saber como posso inserir um JScrollPane e como faço para formatar o texto.

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

public class FileAcess extends JFrame{
  JTextArea textArea;
  JButton btn;
  JScrollPane contentsArea;

  public FileAcess() {
    super("Selecione o arquivo");
	
    Container c = getContentPane();
    FlowLayout layout = new FlowLayout(FlowLayout.LEFT);
    c.setLayout(layout);
    
    textArea = new JTextArea(10, 10);
    textArea.setLineWrap(true);
    
    btn = new JButton("Abrir Arquivo");
    btn.addActionListener(
       new ActionListener(){
          public void actionPerformed(ActionEvent e){
              JFileChooser fc = new JFileChooser();
			add( new JScrollPane( contentsArea ), BorderLayout.CENTER );
			setSize( 300, 400 );
				setVisible( true );
				
              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);
    
    setSize(350, 250);
    setVisible(true);
  }
  
  public static void main(String args[]){
    FileAcess app = new FileAcess();
    app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

Obrigado.

1 Resposta

therodrigoagostin

Bom dia,

JScrollPane:
textArea = new JTextArea(5, 30);
...
JScrollPane scrollPane = new JScrollPane(textArea);
...
setPreferredSize(new Dimension(450, 110));
...
add(scrollPane, BorderLayout.CENTER);
Para editar teste esse: http://www.java2s.com/Code/Java/Swing-JFC/SimpleEditorDemo.htm
gt3000:
Gostaria de saber como posso inserir um JScrollPane e como faço para formatar o texto.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class FileAcess extends JFrame{
  JTextArea textArea;
  JButton btn;
  JScrollPane contentsArea;

  public FileAcess() {
    super("Selecione o arquivo");
	
    Container c = getContentPane();
    FlowLayout layout = new FlowLayout(FlowLayout.LEFT);
    c.setLayout(layout);
    
    textArea = new JTextArea(10, 10);
    textArea.setLineWrap(true);
    
    btn = new JButton("Abrir Arquivo");
    btn.addActionListener(
       new ActionListener(){
          public void actionPerformed(ActionEvent e){
              JFileChooser fc = new JFileChooser();
			add( new JScrollPane( contentsArea ), BorderLayout.CENTER );
			setSize( 300, 400 );
				setVisible( true );
				
              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);
    
    setSize(350, 250);
    setVisible(true);
  }
  
  public static void main(String args[]){
    FileAcess app = new FileAcess();
    app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

Obrigado.

Criado 16 de outubro de 2012
Ultima resposta 17 de out. de 2012
Respostas 1
Participantes 2