从文件中读取数据并将数据放入 JTable
Reading from file and putting data into JTable
我有一个如下所示的文本文件
---
*Jack
*James
Tim
*Hannah
Kim
我希望我的程序读取文件的每一行并获取不带 * 的名称并将其显示在 JTable
中。我想出了以下代码,但它只显示 table 中的姓氏 "Kim"。这是问题所在的代码的一部分。
public class GUI extends JPanel{
public JFileChooser fileChooser;
public JButton openbtn;
public BufferedReader br;
public File file;
public GUI(){
fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
if (fileChooser.showOpenDialog(openbtn)==JFileChooser.APPROVE_OPTION){
String fName = null;
file = fileChooser.getSelectedFile();
try{
br = new BufferedReader(new FileReader(file));
String nextLine;
while ((nextLine = br.readLine()) != null)
{
if (nextLine.startsWith("---"))
{
String[] f = nextLine.split("*");
fName = f[1];
}
}
String[] columns = {"FirstName"};
Object[][] data = {{fName}};
JTable tableVIEW = new JTable(data, columns);
tableVIEW.setPreferredScrollableViewportSize(new Dimension(250, 35));
tableVIEW.setFillsViewportHeight(true);
br.close();
}
catch (IOException e) {
System.out.println("Error - INVALID FILE. PLEASE TRY AGAIN");
}
}
}
}
所以我尝试将 fName
放入数组并使用计数器,但是当我浏览文件
时出现此错误
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
和代码:
if (fileChooser.showOpenDialog(openbtn)==JFileChooser.APPROVE_OPTION){
String[] fName = null;
int counter = 0;
file = fileChooser.getSelectedFile();
try{
br = new BufferedReader(new FileReader(file));
String nextLine;
while ((nextLine = br.readLine()) != null)
{
if (nextLine.startsWith("---"))
{
String[] f = nextLine.split("*");
fName[counter] = f[1];
counter++;
}
}
如何获取 table 中的所有条目(不仅仅是最后一个条目)?
it only displays the last name "Kim" in the Jtable
Object[][] data = {{fName}};
您只需向数据数组添加一条信息。
相反,您需要将每个唯一的数据添加到 TableModel。因此,首先使用如下代码创建 DefaultTableModel(在从文件读取数据之前):
String[] columns = {"FirstName"};
DefaultTableModel model = new DefaultTableModel(columns, 0);
然后在循环中每当您从文件中读取数据时将数据添加到模型中:
Vector row = new Vector(1);
row.add( f[1] );
model.addRow( row );
然后在循环完成后创建 table:
JTable tableVIEW = new JTable(model);
我有一个如下所示的文本文件
---
*Jack
*James
Tim
*Hannah
Kim
我希望我的程序读取文件的每一行并获取不带 * 的名称并将其显示在 JTable
中。我想出了以下代码,但它只显示 table 中的姓氏 "Kim"。这是问题所在的代码的一部分。
public class GUI extends JPanel{
public JFileChooser fileChooser;
public JButton openbtn;
public BufferedReader br;
public File file;
public GUI(){
fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
if (fileChooser.showOpenDialog(openbtn)==JFileChooser.APPROVE_OPTION){
String fName = null;
file = fileChooser.getSelectedFile();
try{
br = new BufferedReader(new FileReader(file));
String nextLine;
while ((nextLine = br.readLine()) != null)
{
if (nextLine.startsWith("---"))
{
String[] f = nextLine.split("*");
fName = f[1];
}
}
String[] columns = {"FirstName"};
Object[][] data = {{fName}};
JTable tableVIEW = new JTable(data, columns);
tableVIEW.setPreferredScrollableViewportSize(new Dimension(250, 35));
tableVIEW.setFillsViewportHeight(true);
br.close();
}
catch (IOException e) {
System.out.println("Error - INVALID FILE. PLEASE TRY AGAIN");
}
}
}
}
所以我尝试将 fName
放入数组并使用计数器,但是当我浏览文件
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
和代码:
if (fileChooser.showOpenDialog(openbtn)==JFileChooser.APPROVE_OPTION){
String[] fName = null;
int counter = 0;
file = fileChooser.getSelectedFile();
try{
br = new BufferedReader(new FileReader(file));
String nextLine;
while ((nextLine = br.readLine()) != null)
{
if (nextLine.startsWith("---"))
{
String[] f = nextLine.split("*");
fName[counter] = f[1];
counter++;
}
}
如何获取 table 中的所有条目(不仅仅是最后一个条目)?
it only displays the last name "Kim" in the Jtable
Object[][] data = {{fName}};
您只需向数据数组添加一条信息。
相反,您需要将每个唯一的数据添加到 TableModel。因此,首先使用如下代码创建 DefaultTableModel(在从文件读取数据之前):
String[] columns = {"FirstName"};
DefaultTableModel model = new DefaultTableModel(columns, 0);
然后在循环中每当您从文件中读取数据时将数据添加到模型中:
Vector row = new Vector(1);
row.add( f[1] );
model.addRow( row );
然后在循环完成后创建 table:
JTable tableVIEW = new JTable(model);