简体   繁体   English

JTable在删除行时刷新

[英]Jtable refresh when delete a row

I have aa table that show all book list from a txt file correctly. 我有一个表格,可以正确显示txt文件中的所有图书清单。 I add a new button to frame that when get id number an its textfield and if found, delete it record from file, but if i want to delete from table too, i shoud redisplay the table frame. 我在框架上添加了一个新按钮,当获取ID号为其文本字段时,如果找到它,则从文件中删除记录,但是如果我也想从表格中删除,则应重新显示表格框架。 I want to, when a record deleted from file, my table automatically refresh itself and no need to redisplay table frame. 我想将记录从文件中删除后,表自动刷新,而无需重新显示表框架。 my code is this: 我的代码是这样的:

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();
}
  }

Thank You. 谢谢。

addNotify() method JTable object may be useful, as it reconfigure enclosing scrollpane. JTable对象的addNotify()方法可能很有用,因为它重新配置了封闭的滚动窗格。

Just add tableName.addNotify() after you delete row in table. 删除表中的行后,只需添加tableName.addNotify()

given that you can determine which row is to be removed, you can use the following, 鉴于您可以确定要删除的行,可以使用以下代码,

AbstractTableModel#fireTableRowsDeleted

and then, if needed, issue a repaint request. 然后,如果需要,发出重新绘制请求。

If I understand you correctly you want to delete a row from a table? 如果我理解正确,您想从表中删除行吗?

If so, you should just delete the row from your TableModel and fire the appropriate table changed event to let the table know that you've deleted the row 如果是这样,您应该从TableModel删除该行,并触发适当的表更改事件,以使该表知道您已删除该行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM