Sorry, I forgot to include my Account Record file with the first post.
hi, i am creating a sequential file with a gui. i can get the gui to come up, enter the info, but having difficulty when i try and enter the info to a saved file and clearing the text fields. any insight would help
thank you in advance
CODE
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CreateSequentialFile extends JFrame {
private ObjectOutputStream output;
private BankUI userInterface;
private JButton enterButton, openButton;
public CreateSequentialFile()
{
super( "Creating a Sequential File of Objects" );
userInterface = new BankUI( 5 );
getContentPane().add(
userInterface, BorderLayout.CENTER );
openButton = userInterface.getDoTask1Button();
openButton.setText( "Save into File ..." );
openButton.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event )
{
openFile();
}
}
);
enterButton = userInterface.getDoTask2Button();
enterButton.setText( "Enter" );
enterButton.setEnabled( false );
enterButton.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event )
{
addRecord();
}
}
);
addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent event )
{
if ( output != null )
addRecord();
closeFile();
}
}
);
setSize( 300, 200 );
show();
}
private void openFile()
{
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(
JFileChooser.FILES_ONLY );
int result = fileChooser.showSaveDialog( this );
if ( result == JFileChooser.CANCEL_OPTION )
return;
File fileName = fileChooser.getSelectedFile();
if ( fileName == null ||
fileName.getName().equals( "" ) )
JOptionPane.showMessageDialog( this,
"Invalid File Name", "Invalid File Name",
JOptionPane.ERROR_MESSAGE );
else {
try {
output = new ObjectOutputStream(
new FileOutputStream( fileName ) );
openButton.setEnabled( false );
enterButton.setEnabled( true );
}
catch ( IOException ioException ) {
JOptionPane.showMessageDialog( this,
"Error Opening File", "Error",
JOptionPane.ERROR_MESSAGE );
}
}
}
CODE
import java.awt.*;
import javax.swing.*;
public class BankUI extends JPanel {
protected final static String names[] = { "Account number",
"First name", "Last name", "Balance", "gpa",
"Transaction Amount" };
protected JLabel labels[];
protected JTextField fields[];
protected JButton doTask1, doTask2;
protected JPanel innerPanelCenter, innerPanelSouth;
protected int size;
public static final int ACCOUNT = 0, FIRSTNAME = 1,
LASTNAME = 2, BALANCE = 3, TRANSACTION = 4, gpa = 5;
public BankUI( int mySize )
{
size = mySize;
labels = new JLabel[ size ];
fields = new JTextField[ size ];
for ( int count = 0; count < labels.length; count++ )
labels[ count ] = new JLabel( names[ count ] );
for ( int count = 0; count < fields.length; count++ )
fields[ count ] = new JTextField();
innerPanelCenter = new JPanel();
innerPanelCenter.setLayout( new GridLayout( size, 2 ) );
for ( int count = 0; count < size; count++ ) {
innerPanelCenter.add( labels[ count ] );
innerPanelCenter.add( fields[ count ] );
}
doTask1 = new JButton();
doTask2 = new JButton();
innerPanelSouth = new JPanel();
innerPanelSouth.add( doTask1 );
innerPanelSouth.add( doTask2 );
setLayout( new BorderLayout() );
add( innerPanelCenter, BorderLayout.CENTER );
add( innerPanelSouth, BorderLayout.SOUTH );
validate();
}
public JButton getDoTask1Button()
{
return doTask1;
}
public JButton getDoTask2Button()
{
return doTask2;
}
public JTextField[] getFields()
{
return fields;
}
public void clearFields()
{
for ( int count = 0; count < size; count++ )
fields[ count ].setText( "" );
}
public void setFieldValues( String strings[] )
throws IllegalArgumentException
{
if ( strings.length != size )
throw new IllegalArgumentException( "There must be " +
size + " Strings in the array" );
for ( int count = 0; count < size; count++ )
fields[ count ].setText( strings[ count ] );
}
public String[] getFieldValues()
{
String values[] = new String[ size ];
for ( int count = 0; count < size; count++ )
values[ count ] = fields[ count ].getText();
return values;
}
}
CODE
import java.io.Serializable;
public class AccountRecord implements Serializable {
private int account;
private String firstName;
private String lastName;
private double balance;
private double gpa;
public AccountRecord()
{
this( 0, "", "", 0.0, 0.0);
}
public AccountRecord( int acct, String first,
String last, double bal, double gpa )
{
setAccount( acct );
setFirstName( first );
setLastName( last );
setBalance( bal );
setgpa( gpa );
}
public void setAccount( int account )
{
this.account = account;
}
public int getAccount()
{
return account;
}
public void setFirstName( String first )
{
firstName = first;
}
public String getFirstName()
{
return firstName;
}
public void setLastName( String last )
{
lastName = last;
}
public String getLastName()
{
return lastName;
}
public void setBalance( double bal )
{
balance = bal;
}
public double getBalance()
{
return balance;
}
public void setgpa( double gpa )
{
gpa = gpa;
}
public double getgpa()
{
return gpa;
}
}