1

我有一张表格,可以正确显示 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();
}
  }

谢谢你。

4

3 回答 3

6

addNotify() 方法 JTable 对象可能很有用,因为它重新配置了封闭的滚动窗格。

只需tableName.addNotify()在删除表中的行后添加。

于 2013-01-08T18:32:09.797 回答
3

鉴于您可以确定要删除哪一行,您可以使用以下内容,

AbstractTableModel#fireTableRowsDeleted

然后,如果需要,发出重绘请求。

于 2013-01-08T17:43:17.760 回答
0

如果我理解正确,您想从表中删除一行?

如果是这样,您应该从您的行中删除该行TableModel并触发相应的表更改事件,让表知道您已删除该行

于 2013-01-08T17:44:08.753 回答