简体   繁体   English

将数据显示到jtable有点不同的方式

[英]show data into jtable a bit different way

I have a code that read file and store in arraylist and then convert to array(To use for table model) 我有一个代码读取文件并存储在arraylist然后转换为数组(用于表模型)

My class extends abstracttablemodel correctly. 我的类正确地扩展了abstracttablemodel。

My All Code is: 我的全部代码是:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;

public class ReadFileToList extends AbstractTableModel{
String[] col={"Fname","Lname","Number"};
List<String> data=new ArrayList<String>();
String[][] Arraydata;

public ReadFileToList(){
try{
FileReader fr=new FileReader("D:\\AllUserRecords.txt");
BufferedReader br=new BufferedReader(fr);
String line;
while((line=br.readLine()) !=null){
    data.add(line);
}
br.close();
Arraydata=(String[][]) data.toArray();
}
catch(IOException ioe){
    ioe.printStackTrace();
}
}

@Override
public String getColumnName(int colu){
return col[colu];
}

public int getRowCount() {
return Arraydata.length;
}

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

public Object getValueAt(int rowIndex, int columnIndex) {
return Arraydata[rowIndex][columnIndex];
}
}

My main Class is ReadFileToListM: 我的主要类是ReadFileToListM:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class ReadFileToListM  {
ReadFileToList rftl=new ReadFileToList();
public ReadFileToListM(){
    JFrame frame=new JFrame();
    JTable table=new JTable(rftl);
    JPanel panel=new JPanel();
    JScrollPane sp=new JScrollPane(table);
    panel.add(sp);
    frame.add(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(470,470);
    frame.setVisible(true);
}

public static void main(String[] args){
    new ReadFileToListM();
}
}

but it has Exception! 但它有例外!

this is my Exceptions: 这是我的例外情况:

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [[Ljava.lang.String;
    at javaapplication1.ReadFileToList.<init>(ReadFileToList.java:37)
    at javaapplication1.ReadFileToListM.<init>(ReadFileToListM.java:8)
    at javaapplication1.ReadFileToListM.main(ReadFileToListM.java:22)
Java Result: 1

My txt File: 我的txt文件:

 FName Lname Number
 second secondsecond 22
 thired thithird 33
 fourth fourfourr 44
 fifth fiffif 55

Please help me, Thanks. 请帮帮我,谢谢。

Contructor of the ReadFileToList class is empty, you should change: ReadFileToList类的构造函数为空,您应该更改:

public void readtolist() throws IOException{

to

public ReadFileToList() throws IOException{

because if dont do that initialization of your model(code below) doesnt execute your method which put data in your collections but only execute contructor from extended class. 因为如果不做模型的初始化(下面的代码)不会执行将数据放入集合但只从扩展类执行构造函数的方法。

ReadFileToList rftl=new ReadFileToList();

2nd 第2

You can't cast List which is one-dimential dynamic sized array to two-dimential array. 你不能将一维动态大小数组的List转换为二维数组。 Other methods from your model looks good. 您模型中的其他方法看起来不错。

Also you should somehow seperate data from the File. 你也应该以某种方式从文件中分离数据。 At this moment it's only splitted to to rows. 此时它只分裂成行。

It will be really helpful if you show us few lines from the file. 如果你向我们展示文件中的几行,那将非常有用。

EDIT: 06-01-2013 After posting file content Change Countructor in model to this one 编辑:06-01-2013发布文件内容后 ,将模型中的指示符更改为此

public ReadFileToList() throws IOException{
    FileReader fr=new FileReader("D:/AllUserRecords.txt"); //one slash instead of two backslash
    BufferedReader br=new BufferedReader(fr);
    String line = br.readLine();
    while((line=br.readLine()) !=null){
        data.add(line.trim());  //trim() delete spaces before and after line
        System.out.println(line);
    }
    br.close();
    Arraydata = new String[data.size()][];
    for (int i=0;i<data.size();i++){
        Arraydata[i]=data.get(i).split(" "); //split text to array by space
    }

}

main should look like this 主要应该是这样的

public class ReadFileToListM  {

public ReadFileToListM() throws IOException{
    JFrame frame=new JFrame();
    ReadFileToList rftl=new ReadFileToList();
    JTable table=new JTable(rftl);
    JPanel panel=new JPanel();
    JScrollPane sp=new JScrollPane(table);
    panel.add(sp);
    frame.add(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(470,470);
    frame.setVisible(true);
}

public static void main(String[] args) throws IOException{
    new ReadFileToListM();
}
}

Result: 结果: 结果

It seems that there is a NullPointerException, because Arraydata in ReadFileToList is null. 似乎存在NullPointerException,因为ReadFileToList中的Arraydata为null。

Where do you fill it with data? 你在哪里填写数据? It looks that method ReadFileToList.readtolist is never called, it is not part of the AbstractTableModel and you do not call it manually. 它看起来从不调用方法ReadFileToList.readtolist,它不是AbstractTableModel的一部分,你不会手动调用它。

You should call readtolist or change it to constructor. 您应该调用readtolist或将其更改为构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM