-2

I have got a table and four combo-boxes namely "Asset Categories","Department:","Branches:" and "Asset State".

problem: Now i want to be able to filter my table in such a way that i can select a category of an asset "LAND & BUILDING" and will want to select the branch of the asset.

so when i select "LAND & BUILDING" it shows in my table and when i select a branch, it also appears in the table. so the output in the table becomes

 ASSET CATEGORY   |   BRANCHES
------------------|-------------
LAND & BUILDING   |   BRANCH 1

and so on for the other combo-boxes. I would really appreciate the help thanks

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Sarkwa
  • 1
  • 1
  • 5
  • 1
    You would do this the exact same way in Netbeans 7 as in Netbeans 6, Eclipse or a Java file written using a text editor. In other words, Netbeans has nothing to do with it and is an irrelevant tag. – Andrew Thompson Nov 27 '13 at 10:30
  • 2
    *"I would really appreciate the help"* Do you have a question? Have you tried anything? For example, Google for 'JTable filter tutorial' shows top link as [How to Use Tables](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html) which seems to cover the exact requirement. – Andrew Thompson Nov 27 '13 at 10:32
  • 1
    Take a look at [How to use tables](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html) and pay attention to the [Sorting and Filtering](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#sorting) section – MadProgrammer Nov 27 '13 at 10:33
  • @Sarkwa sorry don't understand, I miss question, direction, sence – mKorbel Nov 27 '13 at 11:23

1 Answers1

2

Table sorting and filtering is managed by a sorter object. The easiest way to provide a sorter object is to set autoCreateRowSorter bound property to true:

JTable table = new JTable();
table.setAutoCreateRowSorter(true);

And then, you can use RowFilter.regexFilter(String regex, int... indices) with the sorter.
Pleas look into the section Sorting and Filter section of the official tutorial page which contains example. You can use ItemListener with the JComboBox to create new filter to work with using the regexFiler function on item selection event.

jComboBox2.addItemListener(new ItemListener() {

   @Override
   public void itemStateChanged(ItemEvent e) {

     JComboBox comb = (JComboBox)e.getSource();
     String selText = (String) comb.getSelectedItem();
     RowFilter<DefaultTableModel, Object> rf = null;

     try {
         rf = RowFilter.regexFilter(selText,
                                 table.getColumnModel().getColumnIndex("ASSET CATEGORY"));
      } catch (PatternSyntaxException ex) {
                     ex.printStackTrace();
      }
      ((TableRowSorter)table.getRowSorter()).setRowFilter(rf);

     }
});
Sage
  • 15,290
  • 3
  • 33
  • 38
  • [it should be little bit complicated](http://stackoverflow.com/a/8187799/714968), [but](http://stackoverflow.com/questions/17854854/jtable-rowfilter-and-rowfilter-entry) +1 for direction.... – mKorbel Nov 27 '13 at 11:21
  • @mKorbel, i see. Yes identity less header will have issue :) +1 to your linked work. – Sage Nov 27 '13 at 19:10