-3

This is my need :

String condition=null;
condition="row==2&&column==2||row==6&&column==1||row==1&&column==2 
|| row==4 && column==1";
table.setDefaultRenderer(Object.class, new CellColorChanger(condition));

in CellColorChanger class i want to use,

 if (condition)
        {
            setBackground(Color.GREEN);
            setForeground(Color.RED);
        }

i know this is not possible. but this is what my requirement. if anyone knows correct way of approach or alternate solution, kindly reply me soon.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
Abdul Azeez
  • 35
  • 1
  • 6
  • Please format your question, right now queston formatting seems to be the first problem – Juned Ahsan Aug 19 '13 at 06:13
  • 2
    Why are you passing it as String and not boolean? – Rohit Jain Aug 19 '13 at 06:14
  • http://stackoverflow.com/a/13780267/279982 – atoMerz Aug 19 '13 at 06:18
  • [You could go with enum-bitwise way](http://stackoverflow.com/q/4960167/593709) – Adil Soomro Aug 19 '13 at 06:23
  • 2
    Functional programming in Java is not a basic topic at all. I don't understand the downvotes, specially if asked by a very new member who we should be welcoming. – Mario Rossi Aug 19 '13 at 06:40
  • @Abdul Azeez Can you please clarify where are the values for `row` and `column` generated? Are they already present in the context where you define `condition`? Or are they present inside `CellColorChanger`? I'm sure this is what you want to do because of the way you use things in `table.setDefaultRenderer()`, but can you please answer these questions? Can you post the code of `CellColorChanger`? – Mario Rossi Aug 19 '13 at 07:01
  • @MarioRossi public static class CellColorChanger extends DefaultTableCellRenderer { private String condition; CellColorChanger(String Condition) { this.check = Condition; } public Component getTableCellRendererComponent(//) { super.getTableCellRendererComponent(//); if (condition) //here what the problem resides. the condition must checked here. since it is string the logic fails. { // } return this; } – Abdul Azeez Aug 19 '13 at 07:37
  • @Abdul Azeez OK. I think I was in the right track. BTW, it's much better if you add that information to the question itelf. And a critical point: where are the actual values of `row` and `column` coming from? I expected to see them in `CellColorChanger`. Are them fields (or otherwise attributes) of DefaultTableCellRenderer? Or, assuming that they are accessible somehow, do you expect the values at the moment the condition is defined/declared, or the at the moment `getTableCellRendererComponent` evaluates it? – Mario Rossi Aug 19 '13 at 08:26
  • @MarioRossi it will come from main program. actually my requirement is to paint background of multiple cells in jtable. I browsed all over, all i got is renderer. If i use renderer for huge data my app's performance will decrease in a large scale. So, i what i thought is to decrease the multiple rendering to just one. this will be like, checking my condition in main if succeed ill concate it to 'condition' variable. at last ill get a huge set of condition statement. then i put that statement in CellColorChanger class. ill put my main function coding in next comment. – Abdul Azeez Aug 19 '13 at 08:50
  • //condition will be generated at run time. here im just checking whether it works condition = "((row==2&&column==2)||(row==6&&column==1)||(row==1&&column==2) || (row==4 && column==1))"; table.setDefaultRenderer(Object.class, new CellColorChanger(condition)); – Abdul Azeez Aug 19 '13 at 08:52
  • 1) I don't think so, as renderers are called with the rows to be displayed *only*, not all of them. 2) I still think my interpretation is correct, but it's hard to confirm it in the abstract (especially when you have an intuition of what you want, but not experience on it). I'd recommend you to produce a complete, working application in which the condition in `CellColorChanger` is fixed (written manually). Once this is done, try to apply to it the most convincing answers and/or come back with a new question. – Mario Rossi Aug 19 '13 at 09:15
  • @MarioRossi i agree ur points. But what i meant to say is to know whether is it possible or not. String="something";if(something){} ill get exactly what i expect if this logic works. please tell the correct way of approach if u know. – Abdul Azeez Aug 19 '13 at 10:50
  • @Abdul Azeez Yes, it is possible, but not with `something` being a String. `something` needs to be an object as I explained in my first post. – Mario Rossi Aug 19 '13 at 18:47

4 Answers4

1

How about this?

  public static void main(String[] args) {
        boolean condition=false;
        int row=0;
        int column=0;
        condition=row==2&&column==2||row==6&&column==1||row==1&&column==2
                || row==4 && column==1;
        setParam(condition);
    }

    public static void setParam(boolean condition){
        if (condition)
        {
            setBackground(Color.GREEN);
            setForeground(Color.RED);
        }
    }

But here you can define condition as boolean not String.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
  • Thanks. but im not getting what i expect. -- "condition" will be generated dynamic. it may be more than 10 lakh records. so i cannot generate the 'condition' as static. im just looking whether is it possible or not. what i will do is., ill simply concate my condition in main program and pass it to another function 'CellColorChanger'. Since jtable doesn't provide single cell color change property without renderer, i want to use this logic(just one time renderer for more than 10 lakh records). thanks for ur suggestions. – Abdul Azeez Aug 19 '13 at 06:42
  • IMHO, Abdul's idea is to implement a very generic CellColorChanger, able to receive different criteria in different circumstances. `row` and `column` are present in CellColorChanger, not the place where the expression is defined. – Mario Rossi Aug 19 '13 at 06:43
  • @ruchira thanks for ur suggestion. i don't want the condition to set true or false. if i use this logic the condition checked in the main program itself. but it has to be checked in my another class. then only it can return what i expect. – Abdul Azeez Aug 19 '13 at 07:21
0

You can create a method, and place it in CellColorChanger class:

private boolean checkCondition(){
  return /* whatever condition like: */ (row == 2 && column == 2) || (row == 6 && column == 1) || (row == 1 && column == 2) || (row == 4 && column == 1);
}

Call this function on passed CellColorChanger object, whenever you want the condition to be re-evaluated.

S.D.
  • 29,290
  • 3
  • 79
  • 130
  • Thanks for your reply. String condition will be generated dynamic. it may be more than 10 lakh records. So far im just looking whether is it possible to do that. – Abdul Azeez Aug 19 '13 at 06:21
  • Anyhow you should have rows and column count separately to check that condition is true – someone Aug 19 '13 at 06:26
  • Again, where do `row` and `column` come from? This is critical to the question. – Mario Rossi Aug 19 '13 at 06:44
  • @MarioRossi i ll generate row and column from 'for iteration'in main program. this is just to compare two table and to show differences with highlighting contents – Abdul Azeez Aug 19 '13 at 06:47
  • @sura@Mario It can be whatever OP wants to put in his condition. This example just shows how to use a methods as a boolean evaluator. – S.D. Aug 19 '13 at 06:47
0

You have to do it like this

new CellColorChanger(row,column)

in your CellColorChanger class

     if(row==2&&column==2||row==6&&column==1||row==1&&column==2 || row==4 && column==1){

        setBackground(Color.GREEN);
        setForeground(Color.RED);
 }
someone
  • 6,577
  • 7
  • 37
  • 60
  • Thanks for your reply. "condition" will be generated dynamic. it may be more than 10 lakh records. so in above "if" condition i cannot format as static as u did. im just looking whether is it possible or not. what i will do is., ill simply concate my condition in main program and pass it to another function 'CellColorChanger'. Since jtable doesn't provide single cell color change property without renderer, i want to use this logic(just one time renderer for more than 10 lakh records). thanks for ur suggestions. – Abdul Azeez Aug 19 '13 at 06:40
  • IMHO, Abdul's idea is to implement a very generic CellColorChanger, able to receive different criteria in different circumstances. – Mario Rossi Aug 19 '13 at 06:42
0

If what you want is to do some form of functional programming (which is what looks more likely to me), then you can implement it as:

interface Condition {
    boolean isTrueFor(Map parameters);
}
public void CellColorChanger(Condition condition) {
    Map<String,String> arguments= new HashMap<String,String>() ;
    //  Populate arguments
    arguments.set("row",String.valueOf(specificRow));
    arguments.set("column",String.valueOf(specificColumn));
    if( condition.isTrueFor(arguments) ) {
        //  Whatever
    }
}
...
Condition myFirstCondition= new Condition() {
    boolean isTrueFor(Map parameters) {
        int row= Integer.paseInt( parameters.get("row") ) ;
        int column= Integer.paseInt( parameters.get("column") ) ;
        return row==2 && column==2 || row==6 && column==1 || row==1 && column==2 || row==4 && column==1
    }
};

That would work if you want to do something really generic. However, my preferred alternative corresponds to code much simpler, clearer, and manageable:

interface Row_Column_Condition {
    boolean isTrueFor(int row,int column);
}
public void CellColorChanger(Condition condition) {
    if( condition.isTrueFor(specificRow,specificColumn) ) {
        //  Whatever
    }
}
...
Row_Column_Condition mySecondCondition= new Row_Column_Condition() {
    boolean isTrueFor(int row,int column) {
        return row==2 && column==2 || row==6 && column==1 || row==1 && column==2 || row==4 && column==1
    }
};
Mario Rossi
  • 7,651
  • 27
  • 37