Results 1 to 5 of 5
  1. #1
    sungirl is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default how can i update my table with Jtextfield filtering?

    Hi all
    I have a big problem :( and i accept any help .
    I have a Jtable that show the all customers list that there are in database.
    On this table there is textfield for filtering. When a user type a character in the textfield , table must be update and show all customers that their names started by that character same time. For example when i type 's' in textfield ,table would synchronize show all customer list that start with s like 'sara' without pressing any key . I write this code , but it don't work and hass DBexeption.I want use of typedkey event .Please help



    // below code is in myFrame.java
    private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) {
    String st=evt.getKeyText(2);
    newCall(st);




    }


    // Variables declaration - do not modify
    private javax.swing.JTable customerTable;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration


    private void newCall(String st){
    newFill();
    try {
    List<Customer> customerList = CustomerDAO.getInstance().listCustomersWithName(st );
    Vector customerData = new Vector();

    for (Customer c : customerList) {
    Vector v = c.getCustomerData();
    customerData.add(v);
    }

    Vector column = new Vector();

    column.add("pass");
    column.add("phone");
    column.add("assress");
    column.add("name");
    column.add("id");


    DefaultTableModel model = new DefaultTableModel(customerData, column);

    customerTable.setModel(model);

    } catch (DBException ex) {
    JOptionPane.showMessageDialog(null, "database exception .");
    }

    }



    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    private void newFill(){
    Vector customerData = new Vector();
    customerData=null;
    Vector column = new Vector();
    column=null;
    DefaultTableModel model = new DefaultTableModel(customerData, column);
    customerTable.setModel(model);
    }

    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~
    private void fillCustomerList() {
    try {
    List<Customer> customerList = CustomerDAO.getInstance().listCustomers();

    Vector customerData = new Vector();

    for (Customer c : customerList) {
    Vector v = c.getCustomerData();
    customerData.add(v);
    }

    Vector column = new Vector();
    column.add("pass");
    column.add("phone");
    column.add("assress");
    column.add("name");
    column.add("id");

    DefaultTableModel model = new DefaultTableModel(customerData, column);

    customerTable.setModel(model);

    } catch (DBException ex) {
    JOptionPane.showMessageDialog(null, "database exception .");
    }
    }


    Regars,

  2. #2
    eRaaaa is online now Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    Simple thing.
    Use the TableRowSorter!
    Java Code:
    final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
    And add a DocumentListener to your textfield - in insertUpdate and removeUpdate you call sorter.setRowFilter(RowFilter.regexFilter(yourtext field.getText(),indeces));

  3. #3
    sungirl is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default reply

    Thanks for your reply , but I don't have removeupdate and insertupdate in my code. What is your mean exactly ?please explain more . tanx a lot

  4. #4
    eRaaaa is online now Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    yeah, the insertUpdate method belongs to the listener :)
    so you have to add a documentlistener to your textfield !!

    here is a simple example
    Java Code:
    import java.awt.BorderLayout;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.JTextField;
    import javax.swing.RowFilter;
    import javax.swing.SwingUtilities;
    import javax.swing.event.DocumentEvent;
    import javax.swing.event.DocumentListener;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableModel;
    import javax.swing.table.TableRowSorter;
    
    public class FilterSnippet {
    	
    	private final TableRowSorter<TableModel> sorter;
    	private JTextField field;
    
    	public FilterSnippet(){
    		JFrame frame = new JFrame();
    		JPanel panel = new JPanel(new BorderLayout());
    		field = new JTextField(10);
    		field.getDocument().addDocumentListener(new DocumentListener() {  // <---
    			@Override
    			public void removeUpdate(DocumentEvent e) {
    				filter();
    			}
    			
    			@Override
    			public void insertUpdate(DocumentEvent e) {
    				filter();
    			}
    			
    			@Override
    			public void changedUpdate(DocumentEvent e) {}
    		});
    		DefaultTableModel model = new DefaultTableModel(new String[][]{{"Sarah","foo"}, {"Abc","bar"}, {"def","foobar"}}, new String[]{"Name","Nickname"});
    		JTable table = new JTable(model);
    		sorter = new TableRowSorter<TableModel>(model);  // <-- 
    		table.setRowSorter(sorter);  // <-- 
    		panel.add(field, BorderLayout.PAGE_START);
    		panel.add(new JScrollPane(table),BorderLayout.CENTER);
    		frame.add(panel);
    		frame.pack();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setLocationRelativeTo(null);
    		frame.setVisible(true);
    	}
    
    	protected void filter() {
    		sorter.setRowFilter(RowFilter.regexFilter("^(?i)"+field.getText(),0));  // <--
    	}
    
    	public static void main(String[] args) {
    		SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				new FilterSnippet();
    			}
    		});
    	}
    }
    the important rows are marked. these rows(similar) you have to put into your code!

  5. #5
    sungirl is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default

    Oh yes , you r right :)
    but i want connect to the database and from there the table's data be update.
    For example when i type A the program must be connect to database and get all customer with name that started A and then show them in table. yor code is right but how can i connect to this database?:(

    can i email to you all my code ?

Similar Threads

  1. can't get JTextField to update
    By rippon in forum AWT / Swing
    Replies: 4
    Last Post: 11-29-2010, 12:34 PM
  2. Replies: 13
    Last Post: 10-30-2010, 04:01 PM
  3. Replies: 0
    Last Post: 02-21-2010, 11:51 AM
  4. JTextField won't update from subclass
    By Edward in forum New To Java
    Replies: 9
    Last Post: 06-29-2009, 05:45 AM
  5. Jsf, Filtering Data In A Table
    By Freddie in forum JavaServer Faces (JSF)
    Replies: 2
    Last Post: 05-11-2007, 12:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •