Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将文本文件中的数据获取到jtable中?_Java - Fatal编程技术网

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

Java 如何将文本文件中的数据获取到jtable中?,java,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(fileN

如何将文本文件的内容显示到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.awt.BorderLayout;
导入java.awt.EventQueue;
导入java.io.BufferedReader;
导入java.io.File;
导入java.io.FileReader;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.List;
导入javax.swing.JFrame;
导入javax.swing.JScrollPane;
导入javax.swing.JTable;
导入javax.swing.table.AbstractTableModel;
公共班机{
公共静态void main(字符串[]args){
Runnable r=新的Runnable(){
公开募捐{
新建Main().createUI();
}
};
调用器(r);
}
私有void createUI(){
试一试{
JFrame=新JFrame();
frame.setLayout(新的BorderLayout());
JTable table=新的JTable();
字符串readLine=null;
StudentTableModel tableModel=新的StudentTableModel();
File File=新文件(“/home/developer/ffmpeg.txt”/*在此处给出文件路径*/);
FileReader=新的FileReader(文件);
BufferedReader bufReader=新的BufferedReader(读卡器);
List studentList=new ArrayList();
而((readLine=bufReader.readLine())!=null){
字符串[]splitData=readLine.split(“;”);
行=新行();
line.setName(splitData[0]);
学生列表。添加(行);
}
tableModel.setList(学生列表);
table.setModel(tableModel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(新的JScrollPane(表));
frame.setTitle(“文件到JTable”);
frame.pack();
frame.setVisible(true);
}catch(IOException ex){}
}
班级线{
私有字符串名称;
私有字符串编号;
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
}
类StudentTableModel扩展了AbstractTableModel{
私有列表=新的ArrayList();
私有字符串[]columnNames={“column1”,“column2”};
公共无效集合列表(列表){
this.list=列表;
fireTableDataChanged();
}
@凌驾
公共字符串getColumnName(int列){
返回columnNames[列];
}
public int getRowCount(){
返回list.size();
}
public int getColumnCount(){
返回columnNames.length;
}
公共对象getValueAt(int行索引、int列索引){
开关(列索引){
案例0:
返回list.get(rowIndex.getName();
违约:
返回null;
}
}
}
}

对象[][]myarray
为什么是对象?如果你有4行以上的代码,又会发生什么呢?你似乎知道如何读取一个文件,网上有很多关于如何构建JTable的例子。这里有一个: