Results 1 to 5 of 5
- 12-14-2010, 03:19 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 3
- Rep Power
- 0
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,
- 12-14-2010, 03:51 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Simple thing.
Use the TableRowSorter!
And add a DocumentListener to your textfield - in insertUpdate and removeUpdate you call sorter.setRowFilter(RowFilter.regexFilter(yourtext field.getText(),indeces));Java Code:final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
- 12-14-2010, 03:59 PM #3
Member
- Join Date
- Dec 2010
- Posts
- 3
- Rep Power
- 0
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
- 12-14-2010, 04:16 PM #4
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
yeah, the insertUpdate method belongs to the listener :)
so you have to add a documentlistener to your textfield !!
here is a simple example
the important rows are marked. these rows(similar) you have to put into your code!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(); } }); } }
- 12-14-2010, 04:28 PM #5
Member
- Join Date
- Dec 2010
- Posts
- 3
- Rep Power
- 0
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
-
can't get JTextField to update
By rippon in forum AWT / SwingReplies: 4Last Post: 11-29-2010, 12:34 PM -
GUI does not update table when variables are set to datamodel using JButton
By Kenjitsuka in forum New To JavaReplies: 13Last Post: 10-30-2010, 04:01 PM -
update a change in a Jtable directly to the mysql table
By Muffel in forum JDBCReplies: 0Last Post: 02-21-2010, 11:51 AM -
JTextField won't update from subclass
By Edward in forum New To JavaReplies: 9Last Post: 06-29-2009, 05:45 AM -
Jsf, Filtering Data In A Table
By Freddie in forum JavaServer Faces (JSF)Replies: 2Last Post: 05-11-2007, 12:59 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks