| |

VerySource

 Forgot password?
 Register
Search
View: 836|Reply: 3

Filtering of JTable specified columns

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-10 20:00:01
| Show all posts |Read mode
I want to implement filtering in the column specified by JTable, such as filtering only in the name column, I don't know how to do it
Reply

Use magic Report

1

Threads

51

Posts

32.00

Credits

Newbie

Rank: 1

Credits
32.00

 China

Post time: 2020-1-16 11:36:01
| Show all posts
Just rewrite the JTREE method
class ResultSetTableModel extends AbstractTableModel
{
   / **
      Constructs the table model.
      @param aResultSet the result set to display.
   * /
   public ResultSetTableModel (ResultSet aResultSet)
   {
      rs = aResultSet;
      try
      {
         rsmd = rs.getMetaData ();
      }
      catch (SQLException e)
      {
         e.printStackTrace ();
      }
   }

   public String getColumnName (int c)
   {
      try
      {
         return rsmd.getColumnName (c + 1);
      }
      catch (SQLException e)
      {
         e.printStackTrace ();
         return "";
      }
   }

   public int getColumnCount ()
   {
      try
      {
         return rsmd.getColumnCount ();
      }
      catch (SQLException e)
      {
         e.printStackTrace ();
         return 0;
      }
   }

   public Object getValueAt (int r, int c)
   {
      try
      {
         rs.absolute (r + 1);
         return rs.getObject (c + 1);
      }
      catch (SQLException e)
      {
         e.printStackTrace ();
         return null;
      }
   }

   public int getRowCount ()
   {
      try
      {
         rs.last ();
         return rs.getRow ();
      }
      catch (SQLException e)
      {
         e.printStackTrace ();
         return 0;
      }
   }

   private ResultSet rs;
   private ResultSetMetaData rsmd;
}
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-4 13:00:01
| Show all posts
[Quote=Quote the reply ofseakeyon the 1st floor:]

Just rewrite the JTREE method
class ResultSetTableModel extends AbstractTableModel
{
        /**
                 Constructs the table model.
                 @param aResultSet the result ……
[/Quote]

lj
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-4 13:45:01
| Show all posts
The following content comes from the jdk documentation
[Quote]
javax.swing.RowFilter<M,I>

RowFilter is used to filter items from the model so that these items will not be displayed in the view. For example, a RowFilter associated with a JTable may only allow those rows that contain columns with specified strings. The meaning of the entry depends on the component type. For example, when a filter is associated with JTable, an entry corresponds to a row; when a filter is associated with JTree, an entry corresponds to a node.
The subclass must override the include method to indicate whether the item should be displayed in the view. The Entry parameter can be used to get the value of each column in the entry. The following example shows an include method that only allows entries with one or more values ​​beginning with the string "a".
RowFilter<Object,Object> startsWithAFilter = new RowFilter<Object,Object>() {
public boolean include(Entry<? extends Object,? extends Object> entry) {
for (int i = entry.getValueCount()-1; i >= 0; i--) {
if (entry.getStringValue(i).startsWith("a")) {
// The value starts with "a", include it
return true;
       }
     }
// None of the columns start with "a"; return false so that this
// entry is not shown
return false;
   }
};

RowFilter has two formal type parameters that can be used to create RowFilters for specific models. For example, the following code assumes a specific model that wraps objects of type Person. Only show persons older than 20:
RowFilter<PersonModel,Integer> ageFilter = new RowFilter<PersonModel,Integer>() {
public boolean include(Entry<? extends PersonModel,? extends Integer> entry) {
PersonModel personModel = entry.getModel();
Person person = personModel.getPerson(entry.getIdentifier());
if (person.getAge()> 20) {
// Returning true indicates this row should be shown.
return true;
     }
// Age is <= 20, don't show it.
return false;
   }
};
PersonModel model = createPersonModel();
TableRowSorter<PersonModel> sorter = new TableRowSorter<PersonModel>(model);
sorter.setRowFilter(ageFilter);

From the following version:
1.6[/Quote]
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list