Hey Guys,

i was looking at the post linked below and been trying to solve the issue, however, i have been unable to solve it.

Click Here

The post says i "MUST create a new Vector for each row" in order for it to work. as mentioned above, i have tried doing this but failed.

Would really appreciate some help.

The code i have is displayed below:

package game;

import java.awt.BorderLayout;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class HighScores {
  public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Vector<String> v = new Vector<String>();
    Vector<Vector<String>> myVector = new Vector<Vector<String>>();
    try{
           FileReader fReader = new FileReader("playerscores.txt");
           BufferedReader inFile = new BufferedReader(fReader);
           String input;
       String[] temp;
        while((input=inFile.readLine())!=null)
        {
                    temp = input.split(",",6);
            for(int i=0;i<temp.length;i++){
                v.add(temp[i]);
                System.out.println(temp[i]+" added");
            }
            System.out.println("V is "+v);
            myVector.add(v);
            System.out.println("---------End of Line");
                Vector<String> columnNames = new Vector<String>();
                columnNames.addElement("Player Name:");
                    columnNames.addElement("Score:");
                JTable table = new JTable(myVector,columnNames);
            JScrollPane scrollPane = new JScrollPane(table);
            frame.add(scrollPane, BorderLayout.CENTER);
            frame.setSize(600, 150);
            frame.setVisible(true);
        }
    }catch(Exception e){
        System.out.println("ERROR");
    }
  }
}

Dani AI

Generated

Common cause and a compact fix

The behavior seen in the original code comes from reusing the same Vector instance for every row. Adding the same Vector reference into the outer Vector multiple times means every entry points to the same object, so later edits overwrite all “rows.” The simplest, correct pattern is: create a new Vector (row) inside the read loop, add that row to the outer Vector, and create the JTable only after the file has been read. Also keep Swing work on the Event Dispatch Thread.

Minimal example (illustrates the key changes — new row per line, table created after reading, use of try-with-resources and EDT):

Vector<Vector<String>> rows = new Vector<>();
Vector<String> cols = new Vector<>();
cols.add("Player Name:");
cols.add("Score:");

try (BufferedReader br = new BufferedReader(new FileReader("playerscores.txt"))) {
  String line;
  while ((line = br.readLine()) != null) {
    String[] parts = line.split(",", -1);
    Vector<String> row = new Vector<>();
    row.add(parts.length > 0 ? parts[0].trim() : "");
    row.add(parts.length > 1 ? parts[1].trim() : "");
    rows.add(row);
  }
}

SwingUtilities.invokeLater(() -> {
  JTable table = new JTable(rows, cols);
  JFrame f = new JFrame("High Scores");
  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  f.add(new JScrollPane(table));
  f.pack();
  f.setLocationRelativeTo(null);
  f.setVisible(true);
});

Extra notes

  • Prefer DefaultTableModel and addRow(...) for dynamic updates.
  • Use split(",", -1) to preserve trailing empty fields; trim() strings before adding.
  • Read the file off the EDT; construct/show the GUI on the EDT.
  • Use Files.newBufferedReader(path, StandardCharsets.UTF_8) for predictable encoding in modern code.

Thread context: noted the issue was resolved; offered help. The above explains why the bug happens and gives a concise, robust fix.

Recommended Answers

All 6 Replies

Hey M/, Yh i did it is already implemented within the code.

okay, when i get home (i am right now out of the city, will be back in a few hours) i will try my best to fix up your code for you (i am sorry if i forget).

In the mean time, someone else might be able to help you, so expect someone like James, Tins, ddanbe, etc. to help out.

Thank you!

I sorted this issue, but thank you for your help!

oh, how did you solve. do you mind posting your code so others can learn from your mistakes. (i am 10 minutes away from doing my event (100 meter) :D).

Also mark the question solved if you are satisfied and congrats on, technically, self solving your question.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.