|
"Arda " <ardaaksu@hotmail.com> wrote in message <j2grnj$go7$1@newscl01ah.mathworks.com>...
> "matt dash" wrote in message <j2e9la$5lb$1@newscl01ah.mathworks.com>...
> > Maybe the best solution that doesnt involve writing java is just to recreate the model each time with only the rows you want to display.
> >
>
> That solution has serious disadvantages. First of all column widths return to default values every time when you set a data vector to the table model. And table data can not be insert with specified widths as far as i know. All widths must be arranged one by one after setting the data. Yet, if the columns are rearrangeable the order of columns must be tracked as well as the widths. Even though things become more complicated and not eye-candy (and more slow too probably!) this may be an option for sure, but i have to find a way to implement the row filter. I am just obsessed with it.
I agree that recreating the model is far from ideal.
To clarify on the issue of RowFilter being abstract, it means that you can't really create a RowFilter, it's more like a template that shows you how to code your own row filter that will easily plug into the tablemodel.
If you're feeling adventurous, here's a table model I made that has filtering built in. You will need to install the JDK in order to compile it. (Note, I also know very little java and I'm sure this could be improved. Also it leaves out many methods you'd probably want to include, but it should get you started).
The strategy here is basically to overload the DefaultTableModels "getValueAt" method, so that it only gets values for the unfiltered rows. It also overloads "getRowCount" to only return the number of visible rows. That's all it takes to trick the JTable into only showing a subset of the rows in the model. You can probably use a similar strategy with your TableRowSorter model.
Here comes java code. Put this in a file called "TableFilterModel.java" (it must be called that)
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;
import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.*;
public class TableFilterModel extends DefaultTableModel {
protected DefaultTableModel tableModel;
public Boolean[] isRowVisible;
public int visibleRowCount;
public int[] visibleRows;
public TableFilterModel(DefaultTableModel tableModel) {
this.tableModel=tableModel;
Boolean[] isRowVisible = new Boolean[tableModel.getRowCount()];
for (int i = 0; i < tableModel.getRowCount(); i++)
isRowVisible[i] = true;
this.isRowVisible=isRowVisible;
setIsRowVisible(isRowVisible);
}
public void setTableModel(DefaultTableModel tableModel){
this.tableModel=tableModel;
}
public TableModel getTableModel() {
return tableModel;
}
public void setIsRowVisible(Boolean[] newRowVisible) {
int visibleRowCount = 0;
for (int i = 0; i < newRowVisible.length; i++){
if (newRowVisible[i]){
visibleRowCount++;
}
}
int j=0;
int[] visibleRows = new int[visibleRowCount];
for (int i = 0; i < newRowVisible.length; i++){
if (newRowVisible[i]){
visibleRows[j]=i;
j++;
}
}
this.visibleRowCount=visibleRowCount;
this.visibleRows=visibleRows;
}
public Object getValueAt(int rowIndex, int columnIndex){
int visibleRowIndex = this.visibleRows[rowIndex];
return this.tableModel.getValueAt(visibleRowIndex,columnIndex);
}
public int getRowCount(){
return this.visibleRowCount;
}
public int getColumnCount(){
return tableModel.getColumnCount();
}
}
Here comes matlab code. Put this in a new file. The lines you need to run to compile the java code above are in the "compilejava" function at the end. Run those first, then you can run the TableFilterTest function to demonstrate the filtering.
The demo shows two tables, one using the DefaultTableModel, and one using TableFilterModel. You can use the list to select which rows are visible.
function TableFilterTest
f=figure('position',[100 100 600 600]);
data=rand(10);
data=num2cell(data);
dmod=javax.swing.table.DefaultTableModel(data,{'a','b','c','d','e','f','g','h','i','j'});
fmod=TableFilterModel(dmod);
jtable=javax.swing.JTable(dmod);
jtable.setAutoResizeMode(jtable.AUTO_RESIZE_OFF);
jtable.putClientProperty('terminateEditOnFocusLost',true);
jscroll=javax.swing.JScrollPane(jtable);
javacomponent(jscroll,[20 20 300 200],gcf);
jtable2=javax.swing.JTable(fmod);
jtable2.setAutoResizeMode(jtable2.AUTO_RESIZE_OFF);
jtable2.putClientProperty('terminateEditOnFocusLost',true);
jscroll2=javax.swing.JScrollPane(jtable2);
javacomponent(jscroll2,[20 320 300 200],gcf);
uicontrol('position',[400 50 100 200],'string',{'1';'2';'3';'4';'5';'6';'7';'8';'9';'10'},...
'max',10,'style','listbox','callback',{@dofilter,jtable2})
function dofilter(src,ev,jtable2)
vals=get(src,'value');
isvis=false(10,1);
isvis(vals)=true;
%make vector of visible rows:
visarray=javaArray('java.lang.Boolean',10);
for c=1:10
visarray(c)=java.lang.Boolean(isvis(c));
end
fmod=jtable2.getModel;
fmod.setIsRowVisible(visarray)
jtable2.repaint;
function complilejava
%replace the dots with your actual paths. It should be the same path for
%each one.
!javac "C:\.....\MATLAB\TableFilterModel.java"
!cd "C:\......\MATLAB\" & jar cf TableFilterModel.jar *.class *.java
javaaddpath('C:\......\MATLAB\TableFilterModel.jar');
|