删除行时Jtable刷新

Saj*_*jad 1 java swing jtable tablemodel

我有一个表格,正确显示txt文件中的所有图书清单.我添加了一个新的按钮框架,当获取id号为其文本字段时,如果找到,从文件中删除它的记录,但如果我也要从表中删除,我应该重新显示表格框架.我想,当从文件中删除一条记录时,我的表会自动刷新并且不需要重新显示表格框架.我的代码是这样的:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

public class ReadBookFileToListM {

public ReadBookFileToListM(){
   final ReadBookFileToList rbftl=new ReadBookFileToList();
   final JFrame Bframe=new JFrame("All Book List");
  final JTextField tf1=new JTextField("             ");
  final JLabel foundlable=new JLabel();
    JButton button1=new JButton("Back To Main Page");
    JButton button2=new JButton("Exit");
    JButton button3=new JButton("Delete Book");
    button1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Bframe.setVisible(false);
            new MainFrame().setVisible(true);
        }
    });
    button2.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });


    JTable Btable=new JTable(rbftl);

    button3.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            boolean find=false;
    String Bookid=tf1.getText();
    File Mf=new File("D:\\AllBookRecords.txt");
    File Tf=new File("D:\\Boutput.txt");
    try{
        FileReader Bfr=new FileReader(Mf);
        BufferedReader Bbr=new BufferedReader(Bfr);
        PrintWriter Bpw=new PrintWriter(new FileWriter(Tf));
        String Bs;
        while( (Bs=Bbr.readLine()) != null ){
                String[] Ust=Bs.split("   ");
                String Bname=Ust[0];
                String Bdate=Ust[1];
                String id=Ust[2];
            if(id.equals(Bookid.trim())){
                find=true;
                foundlable.setText("Book Found,    "+ Bname + "  " + Bdate);
            }
            if(!id.equals(Bookid.trim())){
                Bpw.println(Bs);
            }
        }
        Bpw.close();
        Bbr.close();
        Mf.delete();
        Tf.renameTo(Mf);

    } catch(FileNotFoundException fnfe){
        foundlable.setText("File Not Found");
    } catch(IOException ioe){
        foundlable.setText("IO Error");
        ioe.printStackTrace();
    }
    finally{
        rbftl.fireTableDataChanged();
        if(find)
        foundlable.setText("Book Deleted");
        else
            foundlable.setText("Book Not Found!");
            tf1.setText("     ");
    }
        }
    });

    JPanel panel=new JPanel();
    JScrollPane sp=new JScrollPane(Btable);
    button1.setToolTipText("To Go Main Page, Click here");
    button2.setToolTipText("Terminate Program");
    panel.add(sp);
    panel.add(button1);
    panel.add(button2);
    panel.add(button3);
    panel.add(tf1);
    panel.add(foundlable);
    Bframe.add(panel);
    Btable.setAutoCreateRowSorter(true);
    Bframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Bframe.setBounds(300, 60, 550, 550);
    Bframe.setVisible(true);
}

public static void main(String[] args){
    new ReadBookFileToListM();
}
  }
Run Code Online (Sandbox Code Playgroud)

谢谢.

nul*_*ptr 5

addNotify()方法JTable对象可能很有用,因为它重新配置封闭的scrollpane.

tableName.addNotify()删除表中的行后再添加.