1
here is the code
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
public class Layout extends JFrame {
JTextArea ta = new JTextArea();
JMenuBar menu = new JMenuBar();
JMenu arq = new JMenu("Arquivo");
JMenuItem abrir = new JMenuItem("Abir");
public Layout(){
setSize(500,500);
setLayout( new GridLayout(1,1));
setJMenuBar(menu);
add(ta);
menu.add(arq);
arq.add(abrir);
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
abrir.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"txt", "txt");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
String caminho = chooser.getSelectedFile().getPath();
try(BufferedReader br = new BufferedReader(new FileReader(caminho))){
String linha = br.readLine();
while (linha!=null){
ta.setText(linha);
linha = br.readLine();
}
}catch (Exception exception){
System.out.println(exception.getMessage());
}
}
}
});
}
}
Thank you so much for your help, it worked.
– Diana