gpt4 book ai didi

java - 将 txt 或 xls 加载到 JTable

转载 作者:行者123 更新时间:2023-12-01 12:57:25 25 4
gpt4 key购买 nike

我正在尝试加载一个 txt 文件,其中包含我想要加载到 JTable 的所有信息,但我不知道该怎么做。我寻找一种解决方案,但找不到真正的解决方案。请帮忙!这是我的代码:

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.FileWriter;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;


public class Table2 extends JFrame implements ActionListener, TableModelListener{


//String[] column = {"Name","Age","Gender"};
Object[][] data = {{"John","20","Male"}};
JTable table = new JTable(new DefaultTableModel(data, new Object[]{"Name", "Age","Gender"})){

public Component prepareRenderer(TableCellRenderer r, int data, int col){ //gives a pattern to the table cells.
Component c = super.prepareRenderer(r, data, col);

if(data % 2 == 0){
c.setBackground(Color.PINK);
}
else{
c.setBackground(Color.LIGHT_GRAY);
}
return c;
}

};

//JTable table = new JTable(data,column);

public Table2(){

table.setPreferredScrollableViewportSize(new Dimension(300,200));
table.setFillsViewportHeight(true);

JScrollPane scrollpane = new JScrollPane(table);
add(scrollpane);

table.getModel().addTableModelListener(this); //Adds the model listener to the table. (checks if any changes are made and activated on table changed method.)
}

public static void main(String[] args) {

Table2 table = new Table2();
table.setLayout(new FlowLayout());
table.setVisible(true);
table.setSize(400,300);
table.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
table.setResizable(false);
table.setTitle("Table Prictice on JFrame");
//table.pack();

JButton button = new JButton("Add");
table.add(button);
button.addActionListener(table);
}

public void actionPerformed(ActionEvent e) {

DefaultTableModel model = (DefaultTableModel) table.getModel();
model.addRow(new Object[]{"", "", ""});
System.out.println("Action Performed!");
}

@Override
public void tableChanged(TableModelEvent e) {

System.out.println("Table change detected by tableChange method!");
int row = e.getFirstRow();
int column = e.getColumn();
TableModel model = (TableModel)e.getSource();
String columnName = model.getColumnName(column);
System.out.println("---TEST LINE---");

try {

saveTable();
}

catch (Exception e2) {

e2.printStackTrace();
}
}

public void saveTable()throws Exception
{
BufferedWriter bfw = new BufferedWriter(new FileWriter("Table Data.txt")); //use Excel extension once finished. (txt for now)
for(int i = 0 ; i < table.getColumnCount() ; i++)
{
bfw.write(table.getColumnName(i));
bfw.write("\t");
}

for (int i = 0 ; i < table.getRowCount(); i++)
{
bfw.newLine();
for(int j = 0 ; j < table.getColumnCount();j++)
{
bfw.write((String)(table.getValueAt(i,j)));
bfw.write("\t");;
}
}
bfw.close();
}

}

基本上我想在每次重新启动程序时加载txt文件,以便存储所有信息。

最佳答案

加载数据的代码与写入数据的代码相反:

  1. 从文件中读取一行数据
  2. 使用String.split(...)为每列创建一个值数组
  3. 使用 DefaultTableModel.addRow(...) 方法将数组添加到 TableModel
  4. 重复步骤 1-3,直到读取所有数据,然后使用 JTable.setModel(...) 方法将 TableModel 添加到 JTable。

关于java - 将 txt 或 xls 加载到 JTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23767976/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com