我试图让以下文本文件进入这个'Jtable'..我很难让所有的数据/列出现..我知道这很简单我只是新的到java,所以任何帮助将不胜感激。
我的文字档案:
1;2;3;4;5;6
1b;2b;3b;4b;5b;6b
1C;2C;3C;4C;5C;6C
到目前为止我的代码:
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.Arrays;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
public class JtableTest {
public static void main(String[] args) throws Exception{
Runnable r = new Runnable() {
public void run() {
new JtableTest ().createUI();
}
};
EventQueue.invokeLater(r);
}
private void createUI() {
try {
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
JTable table = new JTable();
String readLine = null;
// DatabaseTableModel tableModel = new DatabaseTableModel();
File file = new File ("JtableTest.txt");
FileReader reader = new FileReader(file);
BufferedReader bufReader = new BufferedReader(reader);//Need to close this
List<Dentry> studentList = new ArrayList<Dentry>();
while ((readLine = bufReader.readLine()) != null) {
String[] splitData = readLine.split(";");
Dentry dentry = new Dentry();
dentry.setName(splitData[0]);
dentry.setNumbers(Arrays.copyOfRange(splitData, 1, splitData.length));
studentList.add(dentry);
}
DatabaseTableModel tableModel = new DatabaseTableModel();
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 Dentry {
private String name;
private String[] number;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber(int index) {
String value = null;
if (index >= 0 && index < number.length) {
value = number[index];
}
return value;
}
public void setNumbers(String... number) {
this.number = number;
class DatabaseTableModel extends AbstractTableModel {
private List<Dentry> list = new ArrayList<Dentry>();
private String[] columnNames = {"1", "2", "3", "4", "5", "6"};
public void setList(List<Dentry> 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();
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
return list.get(rowIndex).getNumber(columnIndex);
default:
return null;
}
}
}
}
}
}
答案 0 :(得分:0)
您的数据包含6列,但您的数据仅允许2(名称和数字)。
首先更改您的pojo以支持多列
class Dentry {
private String name;
private String[] number;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber(int index) {
String value = null;
if (index >= 0 && index < number.length) {
value = number[index];
}
return value;
}
public void setNumbers(String... number) {
this.number = number;
}
}
我在这里完成的所有操作都允许您应用可变长度的数字并提供一种方便的方法来访问它们。
接下来,您需要更改加载数据的方式,以便将文件中的所有其他元素提供给pojo
List<Dentry> studentList = new ArrayList<Dentry>();
while ((readLine = bufReader.readLine()) != null) {
String[] splitData = readLine.split(";");
Dentry dentry = new Dentry();
dentry.setName(splitData[0]);
dentry.setNumbers(Arrays.copyOfRange(splitData, 1, splitData.length));
studentList.add(dentry);
}
这只是从第二个元素(索引splitData
)创建1
的副本到最后一个元素。
我还更改了getVaueAt
方法,以便更容易获取指定列的数字...
public Object getValueAt(int rowIndex, int columnIndex) {
switch (columnIndex) {
case 0:
return list.get(rowIndex).getName();
case 1:
case 2:
case 3:
case 4:
case 5:
return list.get(rowIndex).getNumber(columnIndex - 1);
default:
return null;
}
}
你可以使用default
,因为pojo会检查索引的范围,但这很好地证明了switch
语句的性质下降。