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
}