faisal abdulai
faisal abdulai

Reputation: 3819

replacing the content of a jtable

I have a desktop application in swing with NetBeans IDE the application has a JTable that displays data from a lucene search operation.

Any time a new search is made , the table appends the new search results to the previous search result. What I want is for the table to replace any exiting search results with the new search results. In order words for the table to refresh and display the new search results.

Any suggestions available

this is the code snippet for the datamodel

public class MyTableModel extends AbstractTableModel {

    private Vector<Vector<String>> dataList = new Vector<>();
     private String[] header = { "ID","SUBJECT","LETTTER FROM","LETTER DATE","DATE RECEIED",
                                  "REMARKS","DATE DISPATCHED","DESTINATION OFFICE"};



    public Vector<Vector<String>> getDataList() {
        return dataList;
    }

    public void setDataList(Vector<Vector<String>> dataList) {       
        this.dataList = dataList;
        fireTableDataChanged(); 
    }

    public void setHeader(String[] header) {
        this.header = header;
    }

    public String[] getHeader() {
        return header;
    }

    @Override
    public int getRowCount() {
        return dataList.size();
    }

    @Override
    public int getColumnCount() {
        return header.length;
    }

    @Override
   public String getColumnName(int col) {
    return header[col];
   }

    @Override
    public Object getValueAt(int row, int col) { 
    return dataList.get(row).get(col);
    }

}

this code passes the search result to the data model class

private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
    try {

        searchField = searchTextField.getText();
        if(!searchField.isEmpty())
        {                
       matrix = dbs.searchDatabase(searchField + "*");
       myModel.setDataList(matrix);

        }

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

Upvotes: 1

Views: 3366

Answers (1)

Amarnath
Amarnath

Reputation: 8855

If your table model is in this way,

class AllTableModel extends AbstractTableModel {

    // Suppose this is the data list table is using,
    List<TableData> tableData = new ArrayList<TableData>();

    // Override methods goes here.

    public void setTableData(List<TableData> tableData) {
        this.tableData = tableData;
        fireTableDataChanged();
}  
}

Now, set the new data to the list using the table model instance.

 allTableModel.setTableData(/* Set new search results to the list.*/);

Upvotes: 5

Related Questions