java - 如何将文本文件中的数据获取到 jtable 中?

标签 java

如何将文本文件的内容显示到 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; 
        }

最佳答案

以下代码从文件中读取数据并将其放入 JTable 的第 1 列中。我希望这可以帮助你。

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;
            }
        }
    }
}

关于java - 如何将文本文件中的数据获取到 jtable 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44361511/

相关文章:

java - 回文循环始终打印 True

java - 我可以强制显式使用@Override 吗?

java - C++ JNI 在执行任何 IO/Streams 时崩溃/挂起

java - GWT - UiBinder 中的停靠布局面板

Java - 括号问题

java - 将数组中的数组中的选定字符串打印到 JTextField 数组上

java - 为 JDO 实体选择某个 "column"

java - 即使文件夹存在, file.listFiles() 返回 null

java - 守护线程和低优先级线程的区别

java - 为什么子类中的静态 block 没有被执行?