Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

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

  • 12-04-2014 04:04PM
    #1
    Registered Users, Registered Users 2 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