1

Here is a class which deals with the process of Table filtering

//Class DbUtils

public class DbUtils {
    public static TableModel resultSetToTableModel(ResultSet rs) {

        try {
        ResultSetMetaData metaData = rs.getMetaData();
        int numberOfColumns = metaData.getColumnCount();
        Vector columnNames = new Vector();

        for (int column=0; column < numberOfColumns; column++) {
            columnNames.addElement(metaData.getColumnLabel(column + 1));              
        }
        Vector rows = new Vector(); 
        while(rs.next()) {
        Vector newRow = new Vector(); 
        for (int i =1; i <= numberOfColumns; i++) {
        newRow.addElement(rs.getObject(i));
        }
        rows.addElement(newRow);
        } 
        return new DefaultTableModel(rows,columnNames);

       }  catch (Exception e) {
          e.printStackTrace();      
         return null; 
    }



}

}

I have a method which updates a Job Table in class Job_GUI, arguments passed in are sql statements from which table_job is populated. what sql statement is passed in depends on which JTabbedPane is selected taking index value into account. and this is where my problems lies.

//Class Job_GUI

 public void UpdateJobTable(String sql) {
    try {

    pst = conn.prepareStatement(sql); 
    rs = pst.executeQuery();

    table_job.setModel(DbUtils.resultSetToTableModel(rs));
    table_job.getColumnModel().getColumn(0).setPreferredWidth(50);
        table_job.getColumnModel().getColumn(1).setPreferredWidth(140);
        table_job.getColumnModel().getColumn(2).setPreferredWidth(170);
        table_job.getColumnModel().getColumn(3).setPreferredWidth(80);
        table_job.getColumnModel().getColumn(4).setPreferredWidth(120);

}
    catch (Exception e ) {

        JOptionPane.showMessageDialog(null, e);
    } 

    finally {

         try {

          pst.close();
          rs.close();
        } catch (Exception e) {

        }
    }
} 



private void JobTabbedPaneStateChanged(javax.swing.event.ChangeEvent evt) {                                           
    JTabbedPane sourceTabbedPane = (JTabbedPane) evt.getSource();
    int index = sourceTabbedPane.getSelectedIndex();
    if (index == 0) {

       jobTableInit(); 

    }
    else if (index == 1) {
        fillCombo();

    }

    else if(index==2) {
        try {
            sql = "SELECT Job.jobID as 'Job ID', Employer.name as'Company', Job.title as 'Role', Job.description as 'Description', Job.type as 'Type', Job.benefits as 'Benefits', Job.closing as 'Closing Date' FROM Job INNER JOIN Employer ON Job.employerID=Employer.employerID ORDER BY Employer.name";
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            TableModel model = DbUtils.resultSetToTableModel(rs);
            table_job.setModel(model);
            final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
            table_job.setRowSorter(sorter);
            JScrollPane pane = new JScrollPane(table_job);
            searchJob.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                String text = keyword.getText(); 
                if (text.length() == 0) {
                sorter.setRowFilter(null);
                 } else {
                  sorter.setRowFilter(RowFilter.regexFilter(text));


                  }
                 }             
             });

        }

        catch (Exception e) {
           e.printStackTrace();                
        }


    }

When I am in index 2 mode, the table_job content is wiped away, i.e. going back to index 0, table becomes empty, whereas intially it was full of data. When I am in index 2, I don't want table_job to be empty, rather still continue to show data, unless a keyword is entered in JTextField, adjust table_job to search matches. but table_job is not showing anything. what have I done wrong in the code snippet in if block for index 2, perhaps I am wrong elsewhere?

Hoody
  • 2,942
  • 5
  • 28
  • 32

1 Answers1

3

For reference, this complete example illustrates using setModel() to change a TableModel en bloc. Updates are automatic. Also consider using the methods of DefaultTableModel to alter the model.

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • while removing the `JScrollPane pane = new JScrollPane(table_job);` made it work, your method was also working :). I had duplicate scrolls on the table thats why. – Hoody Jan 13 '13 at 09:22