Results 1 to 5 of 5
  1. #1
    johny2011 is offline Member
    Join Date
    Apr 2011
    Posts
    28
    Rep Power
    0

    Default How to filter a JTable ? confused,

    Hi,

    I just want to filter my JTable, which consists of rows from a database.

    Many of you can say,"That is not advanced, very easy" but not et al.

    I found an approach for filtering, and works fine but the problem is while sorter objects uses model, I have no model in my code. The code I have found is not making table from a database, but from objects[]

    Here is the working code,
    Object rows[][] = {
    {"AMZN", "Amazon", 41.28},
    {"EBAY", "eBay", 41.57},
    };
    Object columns[] = {"Symbol", "Name", "Price"};

    TableModel model =
    new DefaultTableModel(rows, columns) {
    public Class getColumnClass(int column) {
    Class returnValue;
    if ((column >= 0) && (column < getColumnCount())) {
    returnValue = getValueAt(0, column).getClass();
    } else {
    returnValue = Object.class;
    }
    return returnValue;
    }
    };

    JTable table = new JTable(model);


    and sorter objects uses that model for row sorter

    final TableRowSorter<TableModel> sorter =
    new TableRowSorter<TableModel>(model);

    --------------------------------------------

    And here is the my code for making te table from a resultset

    try {

    //columns
    ResultSetMetaData rsmd = rs.getMetaData();

    for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
    columnHeads.addElement( rsmd.getColumnName( i ) );

    //rows
    do {
    rows.addElement( getNextRow( rs, rsmd ) );
    } while ( rs.next() );

    // make Table
    // Jtable table = new JTable( rows, columnHeads );

    By the way, I think it is interesting to use TableModel in this way, because on the offical java TableModel Interface, the example given like this

    TableModel myData = new MyTableModel();
    JTable table = new JTable(myData);

    Any idea or approach, how can handle this , and filter my JTable which I make from an access database ?

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    Read the JTable API and follow the link to the Swing tutorial on "How to Use Tables" for a working example of filtering.

  3. #3
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Please use code tags when posting codes.

    I found an approach for filtering, and works fine but the problem is while sorter objects uses model, I have no model in my code. The code I have found is not making table from a database, but from objects[]
    I repost your code, notice the bold code
    Java Code:
    Here is the working code,
    Object rows[][] = {
    {"AMZN", "Amazon", 41.28},
    {"EBAY", "eBay", 41.57},
    };
    Object columns[] = {"Symbol", "Name", "Price"};
    [b]
    //this is the model of your JTable
    TableModel model =
    new DefaultTableModel(rows, columns) {
    public Class getColumnClass(int column) {
    Class returnValue;
    if ((column >= 0) && (column < getColumnCount())) {
    returnValue = getValueAt(0, column).getClass();
    } else {
    returnValue = Object.class;
    }
    return returnValue;
    }
    };[/b]
    
    JTable table = new JTable(model);
    and sorter objects uses that model for row sorter

    final TableRowSorter<TableModel> sorter =
    new TableRowSorter<TableModel>(model);
    And you have already created model for your JTable.
    Last edited by mine0926; 05-17-2011 at 03:13 AM.

  4. #4
    johny2011 is offline Member
    Join Date
    Apr 2011
    Posts
    28
    Rep Power
    0

    Default

    I think something misudnerstood here. That one (first code block) is the example for filtering JTable from jaav site, and works fine on that JTable.

    The second code block is my code fro JTable, and my JTable constructs from resultset and has not model.

    I need a model in order to make filtering , but I do not know how to setup my Jtable with a model, that uses resultset for data , instead of objects..

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    The second code block is my code fro JTable, and my JTable constructs from resultset and has not model.
    Of course it has a model. A JTable MUST have a model. The model is created automatically for you when you pass the "rows' Nd "columnHead" to the JTable constructor. You can access the TableModel at any time by using:

    Java Code:
    TableModel model = table.getModel();

Similar Threads

  1. Filter JTable using andFilter and orFilter
    By mine0926 in forum New To Java
    Replies: 3
    Last Post: 02-08-2011, 06:48 AM
  2. update jtable with filter
    By simo_mon in forum AWT / Swing
    Replies: 7
    Last Post: 06-15-2010, 04:53 AM
  3. JTable with "Filter" like EXCEL
    By atom86 in forum AWT / Swing
    Replies: 1
    Last Post: 10-14-2009, 10:18 AM
  4. Sort and filter in Jtable (java ver 1.5 )
    By itaipee in forum AWT / Swing
    Replies: 3
    Last Post: 04-16-2009, 12:03 PM
  5. web content filter or internet filter
    By sundarjothi in forum Advanced Java
    Replies: 3
    Last Post: 05-15-2008, 11:36 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
  •