coderazzi / tablefilter-swing

TableFilter is a set of Swing components to support user-driven filtering on table.
https://coderazzi.net/tablefilter
MIT License
7 stars 2 forks source link

Allow filtering based on TableCellRenderer values #47

Closed skmohit05 closed 2 years ago

skmohit05 commented 4 years ago

Given that the table model contains column values 0.07, 1.15, 0.28, and table cell renderer is formatting the model values and displaying them in percent format as 7%, 115%, and 28%. Now if you filter the values in the displayed table, it will filter based on the table model values 0.07, 1.15, 0.28, and not based on values 7, 115, and 28. Here the requirement is filtering should be done on the displayed value in UI and not based on the table model values. Table Model Values: 0.07, 1.15, 0.28 Values displayed in UI: 7%, 115%, 28% Filter input: >20 Expected Result: Row values 115% and 28% Actual Result: No row data

Attached zipped file for the above problem. example.zip

coderazzi commented 4 years ago

Hi, Mohit, what you need is to apply the renderer you created (ScoreRenderer) to the header itself:

You do: table.setDefaultRenderer(Float.class, new ScoreRenderer());

And you need to do, as well: int scoreColumn = 1; header.getFilterEditor(scoreColumn).setRenderer(new ScoreRenderer());

The ScoreRenderer itself needs to change, to implement the ChoiceRenderer interface, which mostly mimics the TableCellRenderer interface. Following code shows how to do it:

import net.coderazzi.filters.gui.ChoiceRenderer; import net.coderazzi.filters.gui.IFilterEditor;

import java.awt.Component;

import javax.swing.JLabel; import javax.swing.JTable; import javax.swing.table.TableCellRenderer;

public class ScoreRenderer extends JLabel implements TableCellRenderer, ChoiceRenderer {

public ScoreRenderer() { super.setOpaque(true); }

@Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { Float score = (Float) value; score = score * 100; this.setText(score.toString() + "%");

  return this;

}

@Override public Component getRendererComponent(IFilterEditor editor, Object value, boolean isSelected) { Float score = (Float) value; score = score * 100; this.setText(score.toString() + "%");

  editor.getLook()
          .setupComponent(this, isSelected, editor.getFilter().isEnabled());

  return this;

} }

As you see, it is mostly the same as your implementation, with a new method, which is a copy of the getTableCellRenderer method, plus a line: editor.getLook().setupComponent(this, isSelected, editor.getFilter().isEnabled());

You can have a loog to the FlagRenderer.java file, in the examples download, to see a better implementation.

Best regards,

L.

On Mon, May 18, 2020 at 5:59 PM Mohit Kumar Meena notifications@github.com wrote:

Given that the table model contains column values 0.07, 1.15, 0.28, and table cell renderer is formatting the model values and displaying them in percent format as 7%, 115%, and 28%. Now if you filter the values in the displayed table, it will filter based on the table model values 0.07, 1.15, 0.28, and not based on values 7, 115, and 28. Here the requirement is filtering should be done on the displayed value in UI and not based on the table model values. Table Model Values: 0.07, 1.15, 0.28 Values displayed in UI: 7%, 115%, 28% Filter input: >20 Expected Result: Row values 115% and 28% Actual Result: No row data

Attached zipped file for the above problem. example.zip https://github.com/coderazzi/tablefilter-swing/files/4645231/example.zip

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/coderazzi/tablefilter-swing/issues/47, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAH54IMFYFITDN2S6OFUBY3RSFLMNANCNFSM4NEGSJFQ .

skmohit05 commented 4 years ago

Hi L, Thank you for the quick reply. I followed your approach. I'm unable to type anything in the filter of the score column. Could you please shed some light on this? I've enabled the auto choice functionality for the filter header in my example and now I'm seeing the values 7%, 115%, and 28% in the choice dropdown. So I think it is working now but the only issue is that I'm unable to type anything in the filter.

coderazzi commented 4 years ago

Hi, Mohit,

Indeed, if you apply a renderer there is no way to apply a filter text entered by the user, you can only choose among the options displayed. There are, anyway, several ways to manage your requirements. On one side, you could set a Comparator on the header, to compare the values you enter in the header against those found in the column. But if your idea is to treat the numbers as %, and >20 means >20%, then the best option would be setting in the column, not the current numbers (0.07, 0.15, etc), but these numbers multiplies by 100 (7, 15, etc). You can still apply your rendered in the table column, to show the added '%' symbol, and the header would work without problems without further changes (but the % symbol would not appear on the header).

Best,

L.

On Wed, May 20, 2020 at 2:17 PM Mohit Kumar Meena notifications@github.com wrote:

Hi L, Thank you for the quick reply. I followed your approach. I'm unable to type anything in the filter of the score column. Could you please shed some light on this? I've enabled the auto choice functionality for the filter header in my example and now I'm seeing the values 7%, 115%, and 28% in the choice dropdown. So I think it is working now but the only issue is that I'm unable to type anything in the filter.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/coderazzi/tablefilter-swing/issues/47#issuecomment-631437225, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAH54ILIL6CUDKB4LVPNVSTRSPC5FANCNFSM4NEGSJFQ .