Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

File Handling: Reading a file and displaying it in a JTable

Options
  • 12-04-2014 4:04pm
    #1
    Registered Users Posts: 2,369 ✭✭✭


    Hey there I'm struggling on a college project assignment and basically all I need to do is read from a file and display it in a row in a JTable.

    What I'm trying to do is read score and name from a quiz I implemented and display in a seperate GUI with a JTable leaderboard. I think I need to go inside the JTable itself in NetBeans and code it within that?

    This is the code I done so far and I'm using NetBeans with Java.

    This is the JTable Source code for reading from the file
    import java.io.File;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.FileInputStream;
    import java.util.ArrayList;
    import javax.swing.JOptionPane;

    public class FootballGameLeaders extends javax.swing.JFrame {

    private String name;
    private int pts;
    private File inFile;

    /**
    * Creates new form FootballGameLeaders
    */
    public FootballGameLeaders() {
    initComponents();
    name = new String();
    pts = 0;
    inFile = new File("footballscore.data");

    }

    private void FootballQuiz() {
    File inFile;
    FileInputStream fStream;
    ObjectInputStream oStream;

    try {
    inFile = new File("footballscore.data");
    fStream = new FileInputStream(inFile);
    oStream = new ObjectInputStream(fStream);
    oStream.readObject();
    oStream.close();

    } catch (IOException error) {
    System.out.println(error);
    } catch (ClassNotFoundException e) {
    System.out.print(e);
    }
    }

    Code for writing score and name to the file from the Football Quiz
    private void sumbitBtnActionPerformed(java.awt.event.ActionEvent evt) {

    FootballQuiz quiz = new FootballQuiz();

    //get text
    name = nameTf.getText();

    //set name
    quiz.setName(name);

    File outFile;
    FileOutputStream fStream;
    ObjectOutputStream oStream;

    try{
    outFile = new File("footballscore.data");
    fStream = new FileOutputStream(outFile);
    oStream = new ObjectOutputStream(fStream);

    oStream.writeObject(pts);
    oStream.writeObject(name);

    if(nameTf.getText().equals("")){
    JOptionPane.showMessageDialog(null,"Please enter your name!");
    }
    else{
    JOptionPane.showMessageDialog(null, "Sumbitted successfully!");
    }

    oStream.close();
    }
    catch(IOException e){
    System.out.println(e);
    }


    }


Advertisement