将文本文件读取到jtable

时间:2013-03-01 16:38:24

标签: java swing file-io jtable defaulttablemodel

我想创建一个JTable,其中使用2d数组逐行读取文本文件,仅用于行循环。如何循环DefaultTableModel中的行。

这是我的代码:

try{
    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("Database.txt"))));
    String line = "";
    int width = 0, height = 0;

    while((line = br.readLine())!=null){

    if(width == 0){

    String data[] = line.split("-");
    width = data.length;

        }
    height++;

    String matrix[][] = new String[height][width];
    model = new DefaultTableModel(matrix, col);
    table = new JTable(model){
    public boolean isCellEditable(int row, int column){
    return false;
    }};
    }
}catch (IOException ex){
         ex.printStackTrace();
}

这是整个代码:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.table.*;
import java.io.*;
import java.util.*;

public class Administrator extends JFrame {

        private String col []= {"Account No.","PinCode","First Name",
        "Last Name", "MI","BirthDate","Sex","E-mail","Address"};
        private JScrollPane scroll;
        private JTable table;
        private DefaultTableModel model;

     public Administrator(){
        //ImageIcon bcr = new ImageIcon(getClass().getResource("/images/bcr.jpg"));
        final JButton back = new JButton("back");
        back.setLocation(20,480);
        back.setSize(70,30);
        back.setPreferredSize(new Dimension(90,30));
        back.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            dispose();
            }
        }
        );
        try{

            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("Database.txt"))));

            String line = "";

            int width = 0, height = 0;

            while((line = br.readLine())!=null){

            if(width == 0){

            String data[] = line.split("-");
            width = data.length;

            }
            height++;

            String matrix[][] = new String[height][width];
            model = new DefaultTableModel(matrix, col);
            table = new JTable(model){
            public boolean isCellEditable(int row, int column){
            return false;
                }
            };
        }
        }
        catch (IOException ex){
        ex.printStackTrace();
        }

        DefaultTableCellRenderer cent = new DefaultTableCellRenderer();
        cent.setHorizontalAlignment( JLabel.CENTER );
        table.getColumnModel().getColumn(0).setCellRenderer(cent);
        table.getColumnModel().getColumn(1).setCellRenderer(cent);
        table.getColumnModel().getColumn(2).setCellRenderer(cent);
        table.getColumnModel().getColumn(3).setCellRenderer(cent);
        table.getColumnModel().getColumn(4).setCellRenderer(cent);
        table.getColumnModel().getColumn(5).setCellRenderer(cent);
        table.getColumnModel().getColumn(6).setCellRenderer(cent);
        table.getColumnModel().getColumn(7).setCellRenderer(cent);
        table.getColumnModel().getColumn(0).setPreferredWidth(100);
        table.getColumnModel().getColumn(1).setPreferredWidth(100);
        table.getColumnModel().getColumn(2).setPreferredWidth(100);
        table.getColumnModel().getColumn(3).setPreferredWidth(100);
        table.getColumnModel().getColumn(4).setPreferredWidth(50);
        table.getColumnModel().getColumn(5).setPreferredWidth(100);
        table.getColumnModel().getColumn(6).setPreferredWidth(50);
        table.getColumnModel().getColumn(7).setPreferredWidth(120);

        scroll = new JScrollPane(table);
        scroll.setLocation(20, 320);
        scroll.setSize(750,150);

        Container pane = getContentPane();
        pane.setLayout(null);
        pane.add(back);
        pane.add(scroll);

        /*setIconImage(new ImageIcon("/images/icon.jpg").getImage());
        setLayout(new BorderLayout());
        JLabel www = new JLabel(new ImageIcon(getClass().getResource("/images/admin.jpg")));
        add(www);*/
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setTitle("Account Profile");
        setSize(800,550);
        setResizable(false);
        setVisible(true);
        setLocation(110, 30);
     }
     public static void main(String[]args){
        new Administrator();
     }
}

1 个答案:

答案 0 :(得分:1)

1)创建一个没有数据且只有列名的DefaultTableModel:

DefaultTableModel model = new DefaultTableModel(columnName, 0);

2)在循环中,当您从文件中读取每行文本时,您将执行以下操作:

String[] data = line.split("-");
model.addRow( data );

3)当你读完所有文本后,你就创建了表格:

JTable table = new JTable( model );