Read Content from dat file into textboxes

Discussion in 'Java' started by BISstudent!!, Mar 15, 2010.

  1. BISstudent!!

    BISstudent!! New Member

    Joined:
    Mar 15, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi i have to do a project and its due in like 2days and i cant figure out whats wrong with it!!:(
    I managed to get the project writing the info to the dat file, however it won't read it back.
    It's compiling fine, it's just for some reason the read method won't work and the next and back buttons won't work.
    I want it to read the info from the dat file to the textboxes and then be able to navigate through the files with the next and back buttons.
    I can't see a problem with the code and since it's compiling ok I don't have a clue.
    Any help would be appreciated!!

    Heres the code:
    Code:
    import java.io.*;
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import java.awt.event.*;
    import java.io.RandomAccessFile;
    
    
    //we got the code for this page from Daniel Liang's book Java:How to Program chapter on RAF-Address book example
    
    public class IS2216 extends JFrame 
    {
    private static int FNameSize = 15;
    private static int SNameSize = 20;
    private static int SNumberSize = 9;
    private static int SYearSize = 4;
    private static int SResultSize = 2;
    private static int RecordSize = (FNameSize + SNameSize + SNumberSize + SYearSize + SResultSize);
    
    
    
    private JTextField txtStudentFName = new JTextField(FNameSize);
    private JTextField txtStudentSName = new JTextField(SNameSize);
    private JTextField txtStudentNumber = new JTextField(SNumberSize);
    private JTextField txtStudentYear = new JTextField(SYearSize);
    private JTextField txtStudentResult = new JTextField(SResultSize);
    
    
    public static void main(String args[])
    {
    IS2216 IS2216 = new IS2216();
    
    
    }
    
    
    private JButton btnEnter = new JButton("Enter Result");
    private JButton btnNext = new JButton("Next>");
    private JButton btnBack = new JButton("<Back");
    private JButton btnAttendance = new JButton("Attendance");
    
    private RandomAccessFile raf;
    private String FixedLengthStringIO;
    
    
    
    
    public IS2216()
    { 
    super("IS2216");
    
    try
    {
    raf = new RandomAccessFile("Student.dat", "rw");
    
    }
    catch(IOException ioException)
    {
    System.err.println("File does not exist");
    }
    
    
    
    
    JPanel p1 = new JPanel();
    p1.setLayout(new GridLayout(3, 1));
    p1.add(new JLabel("First Name:"));
    p1.add(new JLabel("Surname:"));
    p1.add(new JLabel("Student Number:"));
    
    JPanel jpYear = new JPanel();
    jpYear.setLayout(new BorderLayout());
    jpYear.add(new JLabel("Year:"),BorderLayout.WEST);
    jpYear.add(txtStudentYear,BorderLayout.CENTER);
    
    
    JPanel jpResult = new JPanel();
    jpResult.setLayout(new BorderLayout());
    jpResult.add(new JLabel("Result:"),BorderLayout.WEST);
    jpResult.add(txtStudentResult,BorderLayout.CENTER);
    
    JPanel p2 = new JPanel();
    p2.setLayout(new BorderLayout());
    p2.add(jpYear,BorderLayout.WEST);
    p2.add(jpResult,BorderLayout.CENTER);
    
    JPanel p3 = new JPanel();
    p3.setLayout(new BorderLayout());
    p3.add(txtStudentNumber,BorderLayout.CENTER);
    p3.add(p2,BorderLayout.EAST);
    
    JPanel p4 = new JPanel();
    p4.setLayout(new GridLayout(3, 1));
    p4.add(txtStudentFName);
    p4.add(txtStudentSName);
    p4.add(p3);
    
    JPanel jpIS2216 = new JPanel(new BorderLayout());
    jpIS2216.add(p1,BorderLayout.WEST);
    jpIS2216.add(p4,BorderLayout.CENTER);
    
    jpIS2216.setBorder(new BevelBorder(BevelBorder.RAISED));
    
    JPanel jpButton = new JPanel();
    jpButton.add(btnEnter);
    jpButton.add(btnBack);
    jpButton.add(btnNext);
    jpButton.add(btnAttendance);
    
    add(jpIS2216,BorderLayout.CENTER);
    add(jpButton,BorderLayout.SOUTH);
    
    setSize(450,175);
    setVisible(true);
    setLocation(500,250);
    setResizable(false);
    
    addWindowListener(
    new WindowAdapter()
    {
    public void windowClosing( WindowEvent event)
    {
    System.exit(0);
    }
    }
    
    );
    
    JMenuBar menu = new JMenuBar();
    
    JMenu fileMenu = new JMenu("File");
    menu.add(fileMenu);
    
    JMenuItem NewStudent = new JMenuItem("Add Student");
    NewStudent.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e)
    {
    txtStudentFName.setText("");
    txtStudentSName.setText("");
    txtStudentNumber.setText("");
    txtStudentYear.setText("");
    txtStudentResult.setText("");
    
    }
    });
    fileMenu.add(NewStudent);
    
    fileMenu.addSeparator();
    
    JMenuItem MainMenu = new JMenuItem("MainMenu");
    MainMenu.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e)
    {
    
    setVisible(false);
    welcomeP welcomeP = new welcomeP();
    welcomeP.setVisible(true);
    }
    });
    fileMenu.add(MainMenu);
    
    fileMenu.addSeparator();
    
    
    JMenuItem logout = new JMenuItem("Logout");
    logout.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e)
    {
    JOptionPane.showMessageDialog(null,"You have been successfully logged out","Logout",JOptionPane.INFORMATION_MESSAGE);
    setVisible(false);
    Login Login = new Login();
    Login.setVisible(true);
    }
    });
    fileMenu.add(logout);
    
    fileMenu.addSeparator();
    
    JMenuItem exit = new JMenuItem("Exit");
    exit.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e)
    {
    System.exit(0);
    }
    
    });
    
    fileMenu.add(exit);
    
    this.setJMenuBar( menu);
    
    
    
    btnEnter.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e){
    writeStudent();
    
    txtStudentFName.setText("");
    txtStudentSName.setText("");
    txtStudentNumber.setText("");
    txtStudentYear.setText("");
    txtStudentResult.setText("");
    
    
    }
    });
    
    
    
    btnNext.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e){
    if(e.getSource()==btnNext){
    try{
    long currentPosition = raf.getFilePointer();
    if(currentPosition < raf.length())
    readStudent(currentPosition);
    }
    catch(IOException ex){
    ex.printStackTrace();
    }
    }
    }
    });
    
    
    
    
    btnBack.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e){
    if(e.getSource()==btnBack){
    try{
    long currentPosition = raf.getFilePointer();
    if(currentPosition - 2 * RecordSize > 0)
    readStudent(currentPosition - 2 * 2 * RecordSize);
    else
    readStudent(0);
    }
    catch(IOException ex)
    {ex.printStackTrace();
    }
    }
    }
    });
    
    try{
    if(raf.length()>0)readStudent(0);
    }
    catch(IOException ex){
    ex.printStackTrace();
    }
    
    
    } 
    
    
    //write record to file
    public void writeStudent(){
    FixedLengthStringIO IO = new FixedLengthStringIO();
    
    try{
    raf.seek(raf.length());
    IO.writeFixedLengthString(txtStudentFName.getText(), 50, raf); 
    IO.writeFixedLengthString(txtStudentSName.getText(),50, raf);
    IO.writeFixedLengthString(txtStudentNumber.getText(),50, raf);
    IO.writeFixedLengthString(txtStudentYear.getText(), 50, raf);
    IO.writeFixedLengthString(txtStudentResult.getText(), 50, raf);
    }
    catch(IOException ex){
    ex.printStackTrace();
    } 
    }
    
    //read record at specified position
    public void readStudent(long position) throws IOException{
    raf.seek(position);
    Student s = new Student();
    
    
    FixedLengthStringIO IO = new FixedLengthStringIO();
    IO.readFixedLengthString(50,raf);
    IO.readFixedLengthString(50,raf);
    IO.readFixedLengthString(50,raf);
    IO.readFixedLengthString(50,raf);
    IO.readFixedLengthString(50,raf);
    
    txtStudentFName.setText(s.getFirstName());
    txtStudentSName.setText(s.getSurname());
    txtStudentNumber.setText(Long.toString(s.getStudentNumber()));
    txtStudentYear.setText(s.getYear());
    txtStudentResult.setText(Integer.toString(s.getResult()));
    }
    
    
    }
    
     
    Last edited by a moderator: Mar 16, 2010
  2. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    send all classes (welcomeP,Login,.....
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice