4
I’m tired of searching and can’t find how to display a.txt file in a Jtextarea automatically. I made a program that opens a window and then wanted it to display the contents of that file without having to load it in some button.
4
I’m tired of searching and can’t find how to display a.txt file in a Jtextarea automatically. I made a program that opens a window and then wanted it to display the contents of that file without having to load it in some button.
5
You must create the JTextArea
inside the builder!
Follow an example:
Demojtextarea.java
public class DemoJTextArea {
public static void main(String[] args) {
Janela jn = new Janela();
jn.setVisible(true);
}
}
Java window.
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JTextArea;
class Janela extends JFrame {
public Janela() {
super("Display txt on JTextArea!");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(250, 250);
File file = new File("/home/anderson/arquivo.txt");
FileInputStream fis = null;
String texto = "";
try {
fis = new FileInputStream(file);
int content;
while ((content = fis.read()) != -1) {
texto += (char) content;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
JTextArea textArea = new JTextArea(texto);
textArea.setLineWrap(true); //quebra de linha automática
add(textArea);
}
}
Running:
Reference: How To Read File In Java - Fileinputstream
0
I managed to do it in a different way but it gives the same result.
I had the visual screen builder call the procedure Reader(); passing the file address, the reader reads what is in the file and writes everything to Arraylist contentTxt that is public Static.
Returning to the constructor after calling the reader we will pass all the contents of the Arraylist for a String calling for line with the help of a for, then we give an Areatexto.setText(line); and ready we have a program that loads the information from the.txt file into an Areatext.
The low codes are missing some things like Imports and instantiate the program to run correctly. I only put what is in evidence for the solution.
public class JanelaArquivosTxt extends javax.swing.JFrame {
ArquivosTxt tela1 = new ArquivosTxt();
public JanelaArquivosTxt() {
initComponents();
leitor("C:\\Users\\Paulo Brito\\Desktop\\paulo.txt");
String linha = "";
for (int i = 0; i < conteudoTxt.size(); i++) {
linha += conteudoTxt.get(i) + "\n";
}
AreaTexto.setText(linha);
}
private javax.swing.JTextArea AreaTexto;
}
public class ArquivosTxt {
public static ArrayList < String > conteudoTxt = new ArrayList < String > ();
private static File arquivo;
public static void leitor(String path) {
try {
BufferedReader buffRead = new BufferedReader(new FileReader(path));
String linha = "";
if (!arquivo.exists()) {
arquivo.createNewFile();
}
ArrayList < String > linhas = new ArrayList < String > ();
while (true) {
linha = buffRead.readLine();
if (linha != null) {
linhas.add(linha);
} else {
break;
}
}
buffRead.close();
conteudoTxt = linhas;
} catch (IOException ex) {
Logger.getLogger(JanelaArquivosTxt.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Browser other questions tagged java gui
You are not signed in. Login or sign up in order to post.
Friend how can I make that after a certain number of characters or spaces change the line?
– MD4tw
Yes: textarea.setLineWrap(true); If the answer was helpful to you, upvote! Vlw!
– AndersonBS
I think I already did, but if you don’t teach me that I do
– MD4tw
You have marked the answer as accepted. But you can still contribute the questions and answers by clicking the arrow up if they were useful. Dispose :)
– AndersonBS
I still don’t have enough reputation points -.-
– MD4tw
It needs 15 reputation to be able to vote in favor, it’s a privilege to be achieved.
– Math