Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 300,513 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,945 people online right now. Registration is fast and FREE... Join Now!




sequential file

 

sequential file, unable to get the gui to enter info into a saved file

dm377

20 Aug, 2008 - 05:28 PM
Post #1

New D.I.C Head
*

Joined: 20 Jul, 2008
Posts: 5

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 );
         }      
      }
  
   }  

  
   private void closeFile()
   {
      
      try {
         output.close();

         System.exit( 0 );
      }

      
      catch( IOException ioException ) {
         JOptionPane.showMessageDialog( this,
            "Error closing file", "Error",
            JOptionPane.ERROR_MESSAGE );
         System.exit( 1 );
      }
   }

  
   public void addRecord()
   {
      int accountNumber = 0;
      AccountRecord record;
      String fieldValues[] = userInterface.getFieldValues();
    
      
      if ( ! fieldValues[ BankUI.ACCOUNT ].equals( "" ) ) {

        
         try {
            accountNumber = Integer.parseInt(
               fieldValues[ BankUI.ACCOUNT ] );

            if ( accountNumber > 0 ) {

              
               record = new AccountRecord( accountNumber,
                  fieldValues[ BankUI.FIRSTNAME ],
                  fieldValues[ BankUI.LASTNAME ],
                  Double.parseDouble(
                     fieldValues[ BankUI.BALANCE ]),
                   Double.parseDouble(    
                     fieldValues[ BankUI.gpa ]) );

              
               output.writeObject( record );
               output.flush();
            }

            
            userInterface.clearFields();
         }

        
         catch ( NumberFormatException formatException ) {
            JOptionPane.showMessageDialog( this,
               "Bad account number or balance",
               "Invalid Number Format",
               JOptionPane.ERROR_MESSAGE );
         }

        
         catch ( IOException ioException ) {
            closeFile();
         }

      }  

   }  

  
  
   public static void main( String args[] )
   {
      new CreateSequentialFile();
   }

}  


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;
   }

}  




User is offlineProfile CardPM
+Quote Post


lordms12

RE: Sequential File

20 Aug, 2008 - 11:21 PM
Post #2

D.I.C Regular
Group Icon

Joined: 16 Feb, 2008
Posts: 335



Thanked: 26 times
Dream Kudos: 250
My Contributions
I could not run your code because I do not have AccountRecord so I could figure out specifically what is your problem.

If I understood you then RandomAccessFile.seek may help.

This post has been edited by lordms12: 20 Aug, 2008 - 11:22 PM
User is offlineProfile CardPM
+Quote Post

dm377

RE: Sequential File

21 Aug, 2008 - 02:09 AM
Post #3

New D.I.C Head
*

Joined: 20 Jul, 2008
Posts: 5

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;
   }  


}  





User is offlineProfile CardPM
+Quote Post

lordms12

RE: Sequential File

21 Aug, 2008 - 06:42 AM
Post #4

D.I.C Regular
Group Icon

Joined: 16 Feb, 2008
Posts: 335



Thanked: 26 times
Dream Kudos: 250
My Contributions
Please do not duplicate post and add this code to your previous thread.

User is offlineProfile CardPM
+Quote Post

lordms12

RE: Sequential File

21 Aug, 2008 - 06:46 AM
Post #5

D.I.C Regular
Group Icon

Joined: 16 Feb, 2008
Posts: 335



Thanked: 26 times
Dream Kudos: 250
My Contributions
I guess that was not helpful so can you please explain your problem more.
User is offlineProfile CardPM
+Quote Post

gabehabe

RE: Sequential File

21 Aug, 2008 - 11:42 AM
Post #6

Sexy DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 8,830



Thanked: 175 times
Dream Kudos: 3275
Expert In: Lots of things.

My Contributions
You know, you could have just clicked "edit" on your other post.

Threads merged.
User is offlineProfile CardPM
+Quote Post

lordms12

RE: Sequential File

21 Aug, 2008 - 11:47 AM
Post #7

D.I.C Regular
Group Icon

Joined: 16 Feb, 2008
Posts: 335



Thanked: 26 times
Dream Kudos: 250
My Contributions
QUOTE(gabehabe @ 21 Aug, 2008 - 10:42 PM) *

You know, you could have just clicked "edit" on your other post.

Threads merged.

Thanks gabehabe
User is offlineProfile CardPM
+Quote Post

dm377

RE: Sequential File

21 Aug, 2008 - 03:57 PM
Post #8

New D.I.C Head
*

Joined: 20 Jul, 2008
Posts: 5

QUOTE(lordms12 @ 21 Aug, 2008 - 12:47 PM) *

QUOTE(gabehabe @ 21 Aug, 2008 - 10:42 PM) *

You know, you could have just clicked "edit" on your other post.

Threads merged.

Thanks gabehabe



sorry, new at this.

QUOTE(lordms12 @ 21 Aug, 2008 - 07:42 AM) *

Please do not duplicate post and add this code to your previous thread.



sorry, new at this.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 05:17AM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month