Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Java table

Subject: Java table

From: Arda

Date: 15 Aug, 2011 20:10:26

Message: 1 of 10

Hi,
I have a java gui with a table inside. Without removing any row, i want to make some rows invicible. There is a Java example here;

http://www.java2s.com/Tutorial/Java/0240__Swing/FiltertablebythetextinaTextField.htm


The code i have implemented to Matlab is as follows;

clc; clear all; close all;
frame=javax.swing.JFrame('table');
model=javax.swing.table.DefaultTableModel({1,'a';2,'b'},{'int','str'});
table=javax.swing.JTable(model);
sorter=javax.swing.table.TableRowSorter(model);
table.setRowSorter(sorter);
pane=javax.swing.JScrollPane(table);
frame.add(pane,java.awt.BorderLayout.CENTER);
frame.setSize(300,250);
frame.setVisible(true);
%
sorter.setRowFilter(javax.swing.RowFilter); %??

Matlab fails to create "javax.swing.RowFilter" class. I couldnt able to filter the table. I would happy to see your suggestions.

Thanks,
Arda

Subject: Java table

From: matt dash

Date: 15 Aug, 2011 20:42:27

Message: 2 of 10

"Arda " <ardaaksu@hotmail.com> wrote in message <j2bufi$hth$1@newscl01ah.mathworks.com>...
> Hi,
> I have a java gui with a table inside. Without removing any row, i want to make some rows invicible. There is a Java example here;
>
> http://www.java2s.com/Tutorial/Java/0240__Swing/FiltertablebythetextinaTextField.htm
>
>
> The code i have implemented to Matlab is as follows;
>
> clc; clear all; close all;
> frame=javax.swing.JFrame('table');
> model=javax.swing.table.DefaultTableModel({1,'a';2,'b'},{'int','str'});
> table=javax.swing.JTable(model);
> sorter=javax.swing.table.TableRowSorter(model);
> table.setRowSorter(sorter);
> pane=javax.swing.JScrollPane(table);
> frame.add(pane,java.awt.BorderLayout.CENTER);
> frame.setSize(300,250);
> frame.setVisible(true);
> %
> sorter.setRowFilter(javax.swing.RowFilter); %??
>
> Matlab fails to create "javax.swing.RowFilter" class. I couldnt able to filter the table. I would happy to see your suggestions.
>
> Thanks,
> Arda

RowFilter is an abstract class.

Could you just setRowHeight(r,0) for the rows you want to hide?

Subject: Java table

From: Arda

Date: 16 Aug, 2011 06:35:15

Message: 3 of 10

It appears that, row heights can notbe set less than 1.. Even if it can be, it is not desirable since rows actually stay there, so you will lost in the table if you try to change columns with arrow keys.
By the way, does the fact that rowfilter is an abstract class means we cannot use it in matlab?

Arda

Subject: Java table

From: matt dash

Date: 16 Aug, 2011 12:07:12

Message: 4 of 10


> By the way, does the fact that rowfilter is an abstract class means we cannot use it in matlab?
>
> Arda

No, you'll just need to write a short java class that extends it before you can use it.

Subject: Java table

From: Arda

Date: 16 Aug, 2011 13:16:27

Message: 5 of 10

"matt dash" wrote in message <j2dmhg$rbv$1@newscl01ah.mathworks.com>...
>
>
> No, you'll just need to write a short java class that extends it before you can use it.

How? :)
I dont know Java unfortunately and I would be very much appreciated your help..

Subject: Java table

From: matt dash

Date: 16 Aug, 2011 17:33:30

Message: 6 of 10

"Arda " <ardaaksu@hotmail.com> wrote in message <j2dqjb$bt7$1@newscl01ah.mathworks.com>...
> "matt dash" wrote in message <j2dmhg$rbv$1@newscl01ah.mathworks.com>...
> >
> >
> > No, you'll just need to write a short java class that extends it before you can use it.
>
> How? :)
> I dont know Java unfortunately and I would be very much appreciated your help..


I'm not entirely sure how to implement RowFilter either. If I were going to try I think i'd just make my own tablemodel that has a true/false vector of which rows to display. I was surprised to hear that you can't set row heights to zero. I use a similar strategy to hide columns and it works fine.

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.

Subject: Java table

From: Arda

Date: 17 Aug, 2011 16:54:11

Message: 7 of 10

"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.

Subject: Java table

From: matt dash

Date: 17 Aug, 2011 20:40:26

Message: 8 of 10

"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');

Subject: Java table

From: Yair Altman

Date: 17 Aug, 2011 21:12:10

Message: 9 of 10

Just as an aside, if you use the built-in class com.mathworks.hg.peer.ui.table.UISortableTable (or the com.jidesoft.grid.SortableTable class on which it is based), you will have immediate access to the jtable.setRowHeight(rowIndex,rowHeight) method, in addition to all the other goodies available in the JIDE table. Both of these classes ultimately extend the standard JTable (via lots of intermediate derived classes that add some functionality), so it's basically plug-and-play compatible with your JTable source code.

FYI, this is the underlying Java object used by Matlab's built-in uitable function.

Yair Altman
http://UndocumentedM

Subject: Java table

From: Arda

Date: 19 Aug, 2011 18:47:08

Message: 10 of 10

@matt, thank you very much for your kind help. I have found the solution :) Following code works;

sorter.setRowFilter(javax.swing.RowFilter.regexFilter('a',1)); %searches string 'a' in second column (0 for first column)
sorter.setRowFilter([]); %to clear the filter

Now i need to find a way to extend this filter so that i can decide myself which rows to be displayed and tell the table. This is important for me because i dont keep the whole data in my table. So when i filter it i want to do it with the outside data, find the desired rows then make the table to display only those..

@Yair, thank you for pointing the right direction. Jide table turns out to be wonderful. What i see after a few try is that sorting is much more stable and faster! But, unfortunately, it doesnt have a sorter class inside so in order to implement a filter i need to create a sorter from table model and disable sorting one by one for each column since all columns are sortable and it becomes unreliable if sorting is enabled twice, (by the way is there a way to make it for all columns at the same time once for all?) then set it to the table so that a filter can be used.. Appropriate code is as follows;

table.setAutoCreateRowSorter(1);
sorter=table.getRowSorter;
sorter.setSortable(0,false);
sorter.setSortable(1,false);

Another possible way i could find is to extend the table model with a filterabletablemodel and adding a filter to it. However i couldnt succeed to add a filter :( Table model can be changed with a filterable one as follows;
 
model=com.jidesoft.grid.FilterableTableModel(model);

Note that original one can be achieved from getDefaultModel (i might be writing it wrong, but something like that..) and still can be used after changing it..

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us