Jay
Jay

Reputation: 11

How do I get data from a text file into a jtable?

How can I display the contents of my text file into a jTable?

String fileName = "/Users/JJ/Desktop/SSD Text.txt";

// This will reference one line at a time
String line = null;

try {
    // FileReader reads text files in the default encoding.
   FileReader fileReader = new FileReader(fileName);

    // Wraps FileReader in BufferedReader.
    BufferedReader bufferedReader = 
        new BufferedReader(fileReader);
    int i = 0;
    int x= 0;
    Object[][] myarray = new String [4][100];
    while((line = bufferedReader.readLine()) != null) {
      x=0;
        for (String retval: line.split (",")) {
        myarray[x][i]=retval; 
        }

Upvotes: 1

Views: 1754

Answers (1)

Niklas Rosencrantz
Niklas Rosencrantz

Reputation: 26671

The following code reads data from a file and puts it in column 1 of a JTable. I hope this can help you.

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

public class Main {

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            public void run() {
                new Main().createUI();
            }
        };

        EventQueue.invokeLater(r);
    }

    private void createUI() {

        try {
            JFrame frame = new JFrame();
            frame.setLayout(new BorderLayout());
            JTable table = new JTable();

            String readLine = null;

            StudentTableModel tableModel = new StudentTableModel();
            File file = new File("/home/developer/ffmpeg.txt"/*Give your File Path here*/);

            FileReader reader = new FileReader(file);
            BufferedReader bufReader = new BufferedReader(reader);

            List<Line> studentList = new ArrayList<Line>();
            while((readLine = bufReader.readLine()) != null) {
                String[] splitData = readLine.split(";");

                Line line = new Line();
                line.setName(splitData[0]);
                studentList.add(line);
            }

            tableModel.setList(studentList);
            table.setModel(tableModel);

            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new JScrollPane(table));
            frame.setTitle("File to JTable");
            frame.pack();
            frame.setVisible(true);

        } catch(IOException ex) {}
    }

    class Line {

        private String name;
        private String number;

        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    }

    class StudentTableModel extends AbstractTableModel {

        private List<Line> list = new ArrayList<Line>();
        private String[] columnNames = {"column1", "column2"};

        public void setList(List<Line> list) {
            this.list = list;
            fireTableDataChanged();
        }

        @Override
        public String getColumnName(int column) {
            return columnNames[column];
        }

        public int getRowCount() {
            return list.size();
        }

        public int getColumnCount() {
            return columnNames.length;
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            switch (columnIndex) {
                case 0:
                    return list.get(rowIndex).getName();
                default:
                    return null;
            }
        }
    }
}

Upvotes: 1

Related Questions