title: How to change the jtable data by clicking the search button?
body: <p>I have here a not so short working code. I need help in my search button. If I type in the <code>textfield</code> and click the search button the table data should be changed to what is written in the <code>textfield</code>. If it doesn't searched anything then the table should be blank.  What I can't make it work here is the showing of the new data into the table. Should I remove the first data and then <code>repaint()</code>? Or is there a better solution for that?</p>
  
  <pre><code>import java.awt.*;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.io.IOException;
  import java.sql.SQLException;
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  
  import javax.swing.BorderFactory;
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JPanel;
  import javax.swing.JScrollPane;
  import javax.swing.JTabbedPane;
  import javax.swing.JTable;
  import javax.swing.JTextField;
  import javax.swing.border.Border;
  import javax.swing.border.EmptyBorder;
  import javax.swing.table.DefaultTableModel;
  import javax.swing.table.JTableHeader;
  
  public class TableSearch {
  
      JFrame Card = new JFrame();
      static JTable table;
      JTabbedPane card_tab = new JTabbedPane(); // FOR TAB
      JPanel buttonpanel = new JPanel(); // FOR BUTTON BELOW
      FlowLayout flow = new FlowLayout(FlowLayout.LEFT);
      Border etch = BorderFactory.createEtchedBorder(Color.white, Color.gray);
      Border margin = new EmptyBorder(10, 10, 10, 10);
      JTextField text;
  
      public TableSearch() throws SQLException, IOException {
          Card.setVisible(true);
          Card.setSize(821, 421);
          Card.setTitle("File Maintenance");
          Card.setResizable(false);
  
          final Toolkit toolkit = Toolkit.getDefaultToolkit();
          Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
          int x = (int) ((dimension.getWidth() - Card.getWidth()) / 2);
          int y = (int) ((dimension.getHeight() - Card.getHeight()) / 2);
  
          Card.setLocation(x, y);
          Card.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  
          JPanel container = new JPanel();
          container.setLayout(new BorderLayout());
          container.setBackground(new Color(0, 128, 0));
          //container.setBorder(margin);
  
          JPanel labelpanel = new JPanel();
          labelpanel.setLayout(flow);
          labelpanel.setBackground(new Color(0, 128, 0));
  
          JLabel label_1 = new JLabel("Description:");
          label_1.setFont(new Font("Calibri", Font.BOLD, 20));
          label_1.setForeground(Color.YELLOW);
          labelpanel.add(label_1);
  
          text = new JTextField();
          text.setPreferredSize(new Dimension(200, 20));
          labelpanel.add(text);
  
          JButton btnsearch = new JButton("Search");
          btnsearch.setPreferredSize(new Dimension(90, 20));
          btnsearch.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent arg0) {
                  String search = text.getText();
                  System.out.println(search);
                  //What should I put here to change the table data???
              }
          });
          labelpanel.add(btnsearch);
  
          JPanel jp1 = new JPanel();
          jp1.setBackground(new Color(0, 128, 0));
          //jp1.setBorder(new CompoundBorder(etch,margin));
          jp1.setLayout(new BorderLayout());
  
          table = new JTable(new TableModel());
          JScrollPane scrollPane = new JScrollPane(table);
          table.setFillsViewportHeight(true);
  
          JTableHeader header = table.getTableHeader();
          header.setBackground(new Color(224, 223, 227));
          header.setFont(new Font("Calibri", Font.BOLD, 20));
          table.setRowHeight(25);
          jp1.add(scrollPane);
  
          Card.add(container);
          container.add(labelpanel, BorderLayout.PAGE_START);
          container.add(jp1, BorderLayout.CENTER);
      }
  
      public static class TableModel extends DefaultTableModel {
  
          public TableModel() {
              super(new Object[]{"Description", "Code", "Price"}, 0);
              for (int index = 0; index &lt; 4; index++) {
                  addRow(new Object[]{index, index, index});
              }
          }
  
          @Override
          public boolean isCellEditable(int rowIndex, int columnIndex) {
              return false;
          }
      }
  
      public static void main(String[] args) {
          //Use the event dispatch thread for Swing components
          EventQueue.invokeLater(new Runnable() {
              @Override
              public void run() {
                  try {
                      new TableSearch();
                  } catch (SQLException ex) {
                      Logger.getLogger(TableSearch.class.getName())
                            .log(Level.SEVERE, null, ex);
                  } catch (IOException ex) {
                      Logger.getLogger(TableSearch.class.getName())
                            .log(Level.SEVERE, null, ex);
                  }
              }
          });
      }
  }
  </code></pre>