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.