Hi all, I'm trying to get the information from a text file to show up in the JTextArea I have in a GUI. I'm using the GUI editor in NetBeans, because I'm a Java newb. I've been reading a lot of things on the web and can't seem to figure this one out. If anyone could provide some help I'd greatly appreciate it. Thanks in advance.

This is the code I have so far:

package parsecsvfileexample;
import java.io.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.StringTokenizer;

/**
 *
 * @author RJ
 */
public class Browse extends javax.swing.JFrame {

    /** Creates new form Browse */
    public Browse() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        backButton2 = new javax.swing.JButton();
        removeButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        library = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        backButton2.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
        backButton2.setText("Back");
        backButton2.setToolTipText("Back to the main menu");
        backButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backButton2ActionPerformed(evt);
            }
        });

        removeButton.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
        removeButton.setMnemonic('R');
        removeButton.setText("Remove");
        removeButton.setToolTipText("Remove a DVD from the library");
        removeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeButtonActionPerformed(evt);
            }
        });

        library.setColumns(20);
        library.setRows(5);
        jScrollPane1.setViewportView(library);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(238, Short.MAX_VALUE)
                .addComponent(removeButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(backButton2)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 361, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(19, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(backButton2)
                    .addComponent(removeButton))
                .addGap(19, 19, 19))
        );

        pack();
    }// </editor-fold>                        

    private void backButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        DVDorganizer one = new DVDorganizer();
        //one.setBounds(300,300,300,300);
        one.setVisible(true);
        this.dispose();
    }                                           

    private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
        // TODO add your handling code here:
    }                                            

   public void setShowText(java.io.BufferedReader strFile){

       try{

     String dvdLibrary = new String("dvds.txt");
       for (int sub = 0; sub < strFile.read(); sub++)
            dvdLibrary += strFile.toString() + "\n";

        library.setText(dvdLibrary);

        } catch (Exception e) {
            System.out.println("Exception while reading file: " + e);
        }


  }

   








    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Browse().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton backButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea library;
    private javax.swing.JButton removeButton;
    // End of variables declaration                   

}

Your code a bit jumbled, but I'll just write out what's necessary for what you need.

File file = new File(filename);
BufferedReader reader = null;
try {
	reader = new BufferedReader(new FileReader(file));
	String text = null;
	String savetext=null;
		            		
	while ((text = reader.readLine()) != null) {
		savetext += text
	}
	textarea.setText(savetext);
}
catch (FileNotFound etc etc

whereabouts do I put that piece of code? Because right now I've added it under my buttons and I'm getting quite a few errors.

you add it under the code at line 103.

and if at the top of the code you type

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

then you don't need to write out a bunch of extra code every single time you use a JFrame, JPanel, JLabel, JButton, etc.

Edited 5 Years Ago by sirlink99: n/a

What do you mean by extra code, like if I want to have the output from the text file show up elsewhere? Because I only want to display the contents of the text file in the JTextArea box I have in my GUI.

by extra code I mean instead of writing this

backButton2 = new javax.swing.JButton();

you could just write this:

backButton2 = new JButton();

that will save you time and make your code neater. another example

backButton2.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N

would be

backButton2.setFont(new Font("Tahoma", 3, 11)); // NOI18N
This article has been dead for over six months. Start a new discussion instead.