我试图加载一个包含我想要加载到JTable的所有信息的txt文件,但我不知道该怎么做。 我找了一个解决方案但是找不到一个。请帮忙! 这是我的代码:
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文件,以便存储所有信息。
答案 0 :(得分:1)
加载数据的代码与编写数据的代码相反:
String.split(...)
为每个列创建值数组DefaultTableModel.addRow(...)
方法将数组添加到TableModel JTable.setModel(...)
方法将TableModel添加到JTable。