Tonno22
Tonno22

Reputation: 165

How can I set certain text to be read into a textfield in GUI?

I'm working on an assignment using Binary IO and Event Handling. It looks like this, I think I have most of the basic code down, and now its down to the hard stuff. How can I read text from my .bin file into the seperate text fields? enter image description here

My fields currently are blank, but when read in they should look something like that and I should be able to parse through seperate sets of information with my two buttons on the bottom.

Here is what I have so far. I have a RecordViewerUI JFrame form, a RecordViewerPanel JFrame panel, and a SalesAgent java class which holds all of my getters and setters/ constructor for my four variables.

Here is my panel. Sorry for the generated code. Thanks in advance for any help anyone can offer.

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

public class RecordViewerPanel extends javax.swing.JPanel {

String filename = "sales.bin";
ArrayList<SalesAgent> customer = new ArrayList<>();
int curIndex = 0;

/**
 * Creates new form RecordViewerPanel
 */
public RecordViewerPanel() throws IOException, ClassNotFoundException {
    initComponents();
    try {
        ObjectInputStream in = new ObjectInputStream(
                new BufferedInputStream(
                new FileInputStream(filename)));
        ArrayList<SalesAgent> salesForce = new ArrayList<SalesAgent>();
        String first = in.readUTF();
        String last = in.readUTF();
        double sales = in.readDouble();
        Date date = (Date) (in.readObject());
    } catch (FileNotFoundException ex) {
        Logger.getLogger(RecordViewerPanel.class.getName()).log(Level.SEVERE, null, ex);
    }

}

private void refreshUI() {
    SalesAgent sale = (customer.get(curIndex));
    firstTextField.setText(sale.getFirst());
    lastTextField.setText(sale.getLast());
    salesTextField.setText("$" + String.valueOf(sale.getSales()));
    dateTextField.setText(String.valueOf(sale.getDate()));
    recordPanel.repaint();
}

/**
 * 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() {

    filePanel = new javax.swing.JPanel();
    fileLabel = new javax.swing.JLabel();
    fileLabel1 = new javax.swing.JLabel();
    recordPanel = new javax.swing.JPanel();
    firstTextField = new javax.swing.JTextField();
    lastTextField = new javax.swing.JTextField();
    salesTextField = new javax.swing.JTextField();
    dateTextField = new javax.swing.JTextField();
    jLabel1 = new javax.swing.JLabel();
    previousButton = new javax.swing.JButton();
    nextButton = new javax.swing.JButton();

    filePanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

    fileLabel.setBackground(new java.awt.Color(204, 204, 204));
    fileLabel.setText("sales.bin");

    fileLabel1.setBackground(new java.awt.Color(204, 204, 204));
    fileLabel1.setText("Current file:");

    javax.swing.GroupLayout filePanelLayout = new javax.swing.GroupLayout(filePanel);
    filePanel.setLayout(filePanelLayout);
    filePanelLayout.setHorizontalGroup(
        filePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(filePanelLayout.createSequentialGroup()
            .addGap(94, 94, 94)
            .addComponent(fileLabel)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        .addGroup(filePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(filePanelLayout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(fileLabel1)
                .addContainerGap(562, Short.MAX_VALUE)))
    );
    filePanelLayout.setVerticalGroup(
        filePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(filePanelLayout.createSequentialGroup()
            .addContainerGap()
            .addComponent(fileLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE)
            .addContainerGap())
        .addGroup(filePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(filePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(fileLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE)
                .addContainerGap()))
    );

    recordPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

    firstTextField.setText("jTextField1");

    lastTextField.setText("jTextField1");

    salesTextField.setText("jTextField1");

    dateTextField.setText("jTextField1");

    jLabel1.setText("First                             Last                             Sales                            Date");

    previousButton.setText("<<");
    previousButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            previousButtonActionPerformed(evt);
        }
    });

    nextButton.setText(">>");
    nextButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            nextButtonActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout recordPanelLayout = new javax.swing.GroupLayout(recordPanel);
    recordPanel.setLayout(recordPanelLayout);
    recordPanelLayout.setHorizontalGroup(
        recordPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(recordPanelLayout.createSequentialGroup()
            .addContainerGap()
            .addGroup(recordPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(recordPanelLayout.createSequentialGroup()
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 415, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 203, Short.MAX_VALUE))
                .addGroup(recordPanelLayout.createSequentialGroup()
                    .addComponent(firstTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addComponent(lastTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addComponent(salesTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addComponent(dateTextField)))
            .addContainerGap())
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, recordPanelLayout.createSequentialGroup()
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(previousButton, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(nextButton, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(31, 31, 31))
    );
    recordPanelLayout.setVerticalGroup(
        recordPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(recordPanelLayout.createSequentialGroup()
            .addGap(29, 29, 29)
            .addComponent(jLabel1)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(recordPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(firstTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(lastTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(salesTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(dateTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
            .addGroup(recordPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(previousButton)
                .addComponent(nextButton))
            .addGap(27, 27, 27))
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(filePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .addComponent(recordPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(filePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(recordPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addContainerGap())
    );
}// </editor-fold>                        

private void previousButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
    if (curIndex > 0) {
        curIndex--;
    } else if (curIndex == 0) {
        curIndex = 4;
    }
    customer.get(curIndex);
}                                              

private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
    if (curIndex < 4) {
        curIndex++;
    } else if (curIndex == 4) {
        curIndex = 0;
    }
    customer.get(curIndex);
}                                          
// Variables declaration - do not modify                     
private javax.swing.JTextField dateTextField;
private javax.swing.JLabel fileLabel;
private javax.swing.JLabel fileLabel1;
private javax.swing.JPanel filePanel;
private javax.swing.JTextField firstTextField;
private javax.swing.JLabel jLabel1;
private javax.swing.JTextField lastTextField;
private javax.swing.JButton nextButton;
private javax.swing.JButton previousButton;
private javax.swing.JPanel recordPanel;
private javax.swing.JTextField salesTextField;
// End of variables declaration                   
}

Upvotes: 0

Views: 131

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347334

This is rather difficult to answer, as I have no understanding of the file format or the Customer class, but basically, you need to create a new Customer from the values you've read from the file, add it to the customer List and call refreshUI

try {
    ObjectInputStream in = new ObjectInputStream(
            new BufferedInputStream(
            new FileInputStream(filename)));
    String first = in.readUTF();
    String last = in.readUTF();
    double sales = in.readDouble();
    Date date = (Date) (in.readObject());
    Customer cust = ...;
    customer.add(cust);
} catch (FileNotFoundException ex) {
    Logger.getLogger(RecordViewerPanel.class.getName()).log(Level.SEVERE, null, ex);
}
refreshUI();

Upvotes: 1

Related Questions