title: JTable show JTextFiled search result
body: <p>I have a JTextFiled and JTable that read data from text file.</p>
  
  <p>I want to add a keyListener to my JTextFiled that when enter a number ' program should search my textfile and show lines that start with that number on my JTable.</p>
  
  <p>My Text File:</p>
  
  <pre><code>26     thired     62     Yes
  29     sixth     92     No
  35     vff     53     No
  33     oll     36     No
  38     koole     86     No
  </code></pre>
  
  <p>For example, When i write "3" in textfield, my JTable should display this three lines on own:</p>
  
  <pre><code>35     vff     53     No
  33     oll     36     No
  38     koole     86     No
  </code></pre>
  
  <p>My Code:</p>
  
  <pre><code>public class BookPage extends JFrame implements KeyListener{
  
  private AllBooks bookModel;
  private JTable bTabel;
  JTextField tf1;
  
  
  public BookPage(){
      bookModel=new AllBooks();
      bTabel=new JTable(bookModel);
  
      tf1=new JTextField(20);
      tf1.addKeyListener(this);
  
      JPanel panel= new JPanel();
      JScrollPane scroolpane=new JScrollPane(bTabel);
      panel.add(tf1);
      panel.add(scroolpane);
      this.setContentPane(panel);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.setBounds(300, 60, 800, 600);
      this.setVisible(true);
  }
  
  
  public static void main(String[] args){
      new BookPage().setVisible(true);
  }
  
  @Override
  public void keyTyped(KeyEvent e) {
      String line=tf1.getText().trim();
      SearchBook(line);
  
  }
  
  @Override
  public void keyPressed(KeyEvent e) {
      throw new UnsupportedOperationException("Not supported yet.");
  }
  
  @Override
  public void keyReleased(KeyEvent e) {
      throw new UnsupportedOperationException("Not supported yet.");
  }
  
  public void SearchBook(String Bid){
      File f=new File("AllBookRecords.txt");
      try{
          FileReader Bfr=new FileReader(f);
          BufferedReader Bbr=new BufferedReader(Bfr);
          String bs;
          while( (bs=Bbr.readLine()) != null ){
              String[] Ust=bs.split("     ");
              String id=Ust[0];
              String Bname=Ust[1];
              String Bdate=Ust[2];
              String borrowS=Ust[3];
              if(id.equals(Bid.trim())){
                 bTabel.setValueAt(Bname, 10, 1);
              }
          }
      }
      catch (IOException ex) {
  
      }
  }
  }
  </code></pre>
  
  <p>Second Class:</p>
  
  <pre><code>public class AllBooks extends AbstractTableModel{
  BookInformation Binfos1=new BookInformation();
  
  String[] Bcol=new String[]{"Id","Name","Date","Borrow Status"};
  ArrayList&lt;BookInformation&gt; Bdata=new ArrayList&lt;BookInformation&gt;();
  
  public AllBooks(){
      try{
          FileReader fr=new FileReader("AllBookRecords.txt");
          BufferedReader br=new BufferedReader(fr);
          String line;
  
          while( (line=br.readLine()) != null){
              Bdata.add(initializeBookInfos(line));
          }
          br.close();
      }
      catch(IOException ioe){
  
      }
  }
  
  public static BookInformation initializeBookInfos(String myLine){
      BookInformation Binit=new BookInformation();
      String[] bookCellArray=myLine.split("     ");
      Binit.setBookID(bookCellArray[0]);
      Binit.setBookName(bookCellArray[1]);
      Binit.setBookDate(bookCellArray[2]);
      Binit.setBorrowStatus(bookCellArray[3]);
      return Binit;
  }
  public void RemoveMyRow(int row){
      if(RemoveBookFromFile(row)){
         Bdata.remove(row);
        fireTableRowsDeleted(row, row);
      }
  }
  
  public boolean RemoveBookFromFile(int index){
  
      File Mf=new File("AllBookRecords.txt");
      File Tf=new File("Boutput.txt");
      try{
          BufferedReader Ubr=new BufferedReader(new FileReader(Mf));
          PrintWriter Bpw=new PrintWriter(new FileWriter(Tf));
          String line;
          while( (line=Ubr.readLine()) != null ){
              if(line.trim().length() == 0){
                  continue;
              }
              if(!line.startsWith(String.valueOf(getValueAt(index, 0)))){
                  Bpw.println(line);
              }
          }
          Bpw.close();
          Ubr.close();
          Mf.delete();
          Tf.renameTo(Mf);
          return true;
      } catch(FileNotFoundException e1){
          return false;
      }
      catch(IOException ioe){
            return false;
      }
  }
  
  public void AddRow(BookInformation bookinfo){
      if(WriteBooktofile(bookinfo.toString())){
          Bdata.add(bookinfo);
          fireTableRowsInserted(Bdata.size()-1, Bdata.size()-1);
      }
      else{
          JOptionPane.showMessageDialog(null, "Unable Add To File"+bookinfo.getBookName());
      }
  }
  
  public boolean WriteBooktofile(String bookc){
      try{
          File f=new File("AllBookRecords.txt");
  
              FileWriter fw=new FileWriter(f.getAbsoluteFile(), true);
              BufferedWriter bw=new BufferedWriter(fw);
              bw.write(bookc);
              bw.close();
              return true;
      }
      catch(Exception e){
          return false;
      }
  }
  
  @Override
  public String getColumnName(int col){
      return Bcol[col];
  }
  
  @Override
  public int getRowCount() {
      if(Bdata !=null){
      return Bdata.size();
      }
      else{
          return 0;
      }
  }
  
  @Override
  public int getColumnCount() {
      return Bcol.length;
  }
  
  @Override
  public Object getValueAt(int rowIndex, int columnIndex) {
      BookInformation binfo=Bdata.get(rowIndex);
      Object value;
  
      switch(columnIndex){
  
          case 0:
              value=binfo.getBookID();
              break;
          case 1:
              value=binfo.getBookName();
              break;
          case 2:
              value=binfo.getBookDate();
              break;
          case 3:
              value=binfo.getBorrowStatus();
              break;
          default :
              value="...";  
      }
      return value;
  }
  }
  </code></pre>
  
  <p>Second Class:</p>
  
  <pre><code>public class BookInformation {
  
  private String BookName;
  private String BookDate;
  private String BookID;
  private String BorrowStatus;
  
  public String getBookName() {
      return BookName;
  }
  
  public void setBookName(String book_name) {
      this.BookName = book_name;
  }
  
  public String getBookDate() {
      return BookDate;
  }
  
  
  public void setBookDate(String book_date) {
      this.BookDate = book_date;
  }
  
  
  public String getBookID() {
      return BookID;
  }
  
  public void setBookID(String Book_id) {
      this.BookID = Book_id;
  }
  
  @Override
  public String toString(){
      return BookID + "     " + BookName+ "     "
              + BookDate +"     "+ BorrowStatus + "\n";
  }
  
  public String getBorrowStatus() {
      return BorrowStatus;
  }
  
  
  public void setBorrowStatus(String borrowStat) {
      BorrowStatus = borrowStat;
  }
  
  }
  </code></pre>
  
  <p>thanks for help.</p>