E
E
evgenyt20002020-03-06 19:45:37
Java
evgenyt2000, 2020-03-06 19:45:37

How to display content in JTextArea from file?

Good afternoon!
I need to display the contents of the file in JTextArea , but something doesn’t work for me. Thanks in
advance GUI (JPAN)

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GUI  {
    public static void gui() {
        JFrame frame = new JFrame("Laboratory work");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JFrame.setDefaultLookAndFeelDecorated(true);
        frame.setSize(1440, 900);
        frame.setLocation(0, 0);
        frame.setIconImage(new ImageIcon("D:\\Projects\\guilaba\\src\\main\\resources\\icon3.jpg").getImage());
        //exemplari
        JPanel panel01 = new JPanel();
        JPanel panel02 = new JPanel();
        panel01.setBounds(0, 0, 1440, 50);
        panel01.setBackground(Color.lightGray);
        panel02.setBounds(0, 50, 1440, 850);
        panel02.setBackground(Color.yellow);
        JLabel label01 = new JLabel("Server Idle Time: ");
        JLabel label02 = new JLabel("Average residence time in queue: ");
        JTextField text01 = new JTextField(15);
        text01.setText("Not counted yet");
        JTextField text02 = new JTextField(15);
        text02.setText("Not counted yet");
        JButton calculate = new JButton("Calculate");
        //LISTENER -ButtonActionListener
        calculate.addActionListener(new Listener());
        //JPANE
        JTextArea textp = new JTextArea();
        JScrollPane pane = new JScrollPane(textp);
        pane.setPreferredSize(new Dimension(1440, 860));
        //add
        frame.add(panel01);
        frame.add(panel02);
        panel01.add(label01);
        panel01.add(text01);
        panel01.add(label02);
        panel01.add(text02);
        panel01.add(calculate);
        panel02.add(pane);
        //setvisible
        frame.setVisible(true);
    }

}

Ending Listener where tried to do it...
FileReader reader = new FileReader( "table.txt" );
            BufferedReader br = new BufferedReader(reader);
            JTextArea textp = new JTextArea();
            char buffer[] = new char[4096];
            int len;
            while ((len = reader.read(buffer)) != -1){
                String s = new String (buffer, 0, len);
                textp.append(s); }
            br.close();
            textp.requestFocus();
        }catch(IOException ex){System.out.println("Error");}}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
koperagen, 2020-03-06
@evgenyt2000

Could you describe in more detail what exactly does not work. It looks like they didn't even try to solve the problem. Line not being read? Text not showing up?
Judging by the code, you are creating a JTextArea textp , which is not added anywhere. It is necessary by analogy with

panel01.add(label02);
panel01.add(text02);

do

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question