在java jTable中显示lucene搜索结果



我正在构建一个java桌面应用程序,使用Lucene 3.6.2搜索引擎,用于搜索Derby数据库中的数据java swing JTable和netbeans用户在testfield中输入搜索查询搜索结果显示在Jtable和defaulttablemodel中。

我已经尝试了我的手关于显示数据库的教程jTable中的信息。

但是这次我想要一个搜索字段还有一个搜索按钮。之后在搜索字段中输入字符串查找匹配项,然后将结果显示在JTable

下面是代码片段

public class GNSSJFrame extends javax.swing.JFrame {
     String searchField;
     DBSearch dbs = new DBSearch();
     Vector<String> header = new Vector<>();;
     Vector<Vector<String>> tabledata = new Vector<>();;
     TableData td = new TableData();
  public GNSSJFrame() {         
        initComponents();
    }

        searchResultTable.setModel(new javax.swing.table.DefaultTableModel(
            tabledata, header
        ));
        searchResultTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        jScrollPane1.setViewportView(searchResultTable);
        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel2.setText("        SEARCH RESULTS");

        private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
        try {
            searchField = searchTextField.getText();
            if(!searchField.isEmpty())
            {                
           tabledata = dbs.searchDatabase(searchField + "*");
             td.fillHeader();
            header = td.getHeader();
            }
        } catch (CorruptIndexException ex) {
            Logger.getLogger(GNSSJFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (LockObtainFailedException ex) {
            Logger.getLogger(GNSSJFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException | ParseException ex) {
            Logger.getLogger(GNSSJFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }                                            
    private void searchTextFieldActionPerformed(java.awt.event.ActionEvent evt) {                                                
        // TODO add your handling code here:
    }                                               
 Code for the lucene search engine
public class DBSearch {
    //Field Declarations 
    Version matchVersion = Version.LUCENE_36;
    String host  = "jdbc:derby://localhost:1527/NSS_DB";
     String uName = "staff";
     String uPass = "password";
     String qString;
     Statement stmt;
     ResultSet rs;
     Connection con;
     Document doc;
     Vector<Vector<String>> docVector;
      Document d;
    public DBSearch()
    {}
    public Vector<Vector<String>> searchDatabase( String qs) throws CorruptIndexException, LockObtainFailedException, IOException, ParseException
    {
        qString = qs;
        // create some index
        Directory index = new RAMDirectory();
        StandardAnalyzer analyzer = new StandardAnalyzer(matchVersion);
        IndexWriterConfig IWConfig = new IndexWriterConfig(Version.LUCENE_36, analyzer);
        IndexWriter iw = new IndexWriter(index,IWConfig) ;
           try {
      con = DriverManager.getConnection(host, uName, uPass);
    Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
     String sql = "SELECT * FROM APP.REGISTRY";
        rs = stmt.executeQuery(sql);
         rs.beforeFirst();
         while(rs.next()) {
         doc = new Document();
         doc.add(new Field("subject",rs.getString("SUBJECT"),Field.Store.YES,Field.Index.ANALYZED));
         doc.add(new Field("letter_from",rs.getString("LETTER_FROM"),Field.Store.YES,Field.Index.ANALYZED));
       iw.addDocument(doc);
         }
       iw.close();
       stmt.close();
       rs.close();
    }                           
         catch(SQLException err)
         {System.out.println(err.getMessage());}
     //Oen the index 
           IndexReader ir = IndexReader.open(index);
           //create the searcher object
            IndexSearcher searcher = new IndexSearcher(ir);
            QueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_36,
                    new String[]{"subject","letter_from","remarks","office_dispatched_to"}, analyzer);
            Query query = queryParser.parse(qString);
            //Displaying the search results 
            TopDocs topDocs = searcher.search(query,10);
            ScoreDoc[] hits = topDocs.scoreDocs;
             System.out.println("Total hits "+topDocs.totalHits);

        for(int q = 0; q<7 ;q++)
        {
        Vector<String>  vn = new Vector<>();
        vn.add(d.get("subject"));
        vn.add("letter_from");
        vn.add("remark");
        vn.add("office_dispatched_to"); 
        }
            return docVector;  
}

public class TableData {
    private Vector<Vector<String>> matrix = new Vector<>();
    private Vector<String> header = header = new Vector<>();;
    public TableData()
    {   }
    public Vector<Vector<String>> getMatrix() {
        return matrix;
    }
    public void setMatrix(Vector<Vector<String>> matrix) {
        this.matrix = matrix;
    }
    public Vector<String> getHeader() {
        return header;
    }
    public void setHeader(Vector<String> header) {
        this.header = header;
    }
    public void fillHeader(){
        header.add(0,"ID");
        header.add(1,"SUBJECT");
        header.add(2,"LETTER DATE");
        header.add(3,"DATE RECEIED");
        header.add(4,"REMARKS");
        header.add(5,"DATE DISPATCHED");
        header.add(6,"DESTINATION OFFICE");
    }   
}
    } 

看起来您正在使用合适的Vector构建匿名DefaultTableModel,但您随后更新了Vector而不是TableModel本身。更新模型应该会使表自己刷新。您可以从table.getModel()获得对模型的引用,并且可以使用model.setDataVector()更新它。如这里所示,扩展AbstractTableModel可能是另一种选择。

相关内容

  • 没有找到相关文章

最新更新