JTable:  Howto: Row color depending on value

Hi all
I would like to write a cell renderer that changes the background color of a row (or indivicual cells) depending on the value in one of the table columns. For example, if the value in column 'recursive' of a given row is 'true' then make the row (cell) backgroundd grey. if 'false' make white.
I ve been able to write a simple cell renderer:
public class colorTableCellRenderer extends DefaultTableCellRenderer
java.awt.Color recursiveBG = new java.awt.Color(240, 240,240);
public colorTableCellRenderer()
super();
public void setValue(Object value)
String text = (String)value;
this.setBackground(recursiveBG);
this.setText(text);
But i am lost on how to actually check the value of the other column so I can determine which color to choose.
Help is greately appreciated.
Cheers,
michael

Hi Laszlo
Ok, I think this needs an explanation. Here is what I want to do:
1. Depending on the value of column 0 compared to an external value (referenced), I want to mark a row by setting the background of the row to recursiveBG. All others should have a white background.
        if(selectedPerson.equals(sourceID)) // row is not recursive
          this.setBackground(Color.white);
        else // row IS recursive
          this.setBackground(recursiveBG);
          }This should just set the background color of the cell. If I select the row, the 'normal' behaviour should mark the row blue (or whatever is standard). Your counting is right, these states should be accomodated for.
Current full code is below:
public class colorTableCellRenderer extends DefaultTableCellRenderer
  java.awt.Color recursiveBG = new java.awt.Color(240, 240,240);
  int recursiveColumn = 4;
  String recursiveID = null;
  Connect connectRef = null;
  public colorTableCellRenderer(Connect connectRef)
    super();
    this.connectRef = connectRef;
  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
        java.awt.Color recursiveColor = new java.awt.Color(240,240,240);
        String myVal = (String)value;
        this.setText(myVal);
        recursiveID = (String)table.getValueAt(row, recursiveColumn);
        String selectedPerson = connectRef.getSelectedPersonPID();
        String sourceID = (String)table.getValueAt(row, 6);
        if(selectedPerson.equals(sourceID)) // row is not recursive
          this.setBackground(Color.white);
        else // row IS recursive
          this.setBackground(recursiveBG);
          return this;
[/CODE]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

Similar Messages

  • Alternating row color ordered by value

    Hello,
    I created a report ordered by the column COUNTRY. Then I added a hidden column called COLOR with a value switching between '#FFFFFF' and '#F0F0F0' every time we have the next country in the report.
    I now want the background-color of each row to have the value of den column COLOR.
    How can I achieve this? Do I need a dynamic action?
    Regards
    Felix

    Hello Scott,
    sorry that I couldn't get it running until now, thanks for every hint anyway.
    I did all your changes. Unfortunately it still not works. I give you some more information, perhaps you can see the error.
    The region has the template Reports Region and the Layout and Pagination is Standard with color definition. This template is a copy of the Standard Template where I added den background color in the Column Template 1.
    The table the SQL-Statment returns looks like this:
    <font face="courier">
    ID    COMPANY_NAME  COUNTRY  BG_COLOUR
    1356  Company 1     AE       #BFBFBF
    1359  Company 2     AE       #BFBFBF
    1357  Company 4     AE       #BFBFBF
    1200  Company 5     AL       #FFFFFF
    1272  Company 6     AT       #BFBFBF
    1263  Company 7     AT       #BFBFBF
    1264  Company 8     AT       #BFBFBF
    1265  Company 9     AT       #BFBFBF
    1318  Company 14    BA       #FFFFFF
    1319  Company 15    BA       #FFFFFF
    1383  Company 16    BE       #BFBFBF
    </font>
    For the testing I made the column BG_COLOUR unhidden.
    I copied and changed template Standard. In Column Template 1 the new code is
    <td#ALIGNMENT# headers="#COLUMN_HEADER_NAME#" class="t1data" style="background-color:#BG_COLOUR#">#COLUMN_VALUE#</td> It has been enhanced with
    style="background-color:#BG_COLOUR#" Then I have a dynamic action for this report with a javascript code:
    $("td[headers='BG_COLOUR']").each(function()
       $(this).parent().children("td[headers='ID']").css({"background-color":$(this).text});
       alert($(this).text);
    ); The line
    alert($(this).text); I added to see if the dynamic action is fired. Unfortunately it isn't fired.
    The dynamic action is a true action with the little checkbox Fire on Page Load selected.
    Does this information help?
    Thanks Scott for all the time you spent.
    Regards
    Felix

  • Change JTable's row color on row selection

    Hello!
    Everywhere I've looked they're only explaining how to draw rows in different colors WHEN THE TABLE IS BEING CREATED, via prepareRenderer.
    But that is not what I am looking for. What I want is to actually change the row's color after the table has been drawn/rendered/whatsoever. For instance, when the user clicks on that row. The default behaviour is for the row to become blueish, but, when the user selects another row, the first selected row turns back to white. I would like to prevent that from happening. I want the row to become FIRE RED when the user clicks on it, and I want the row to remain like that for good, even when the user selects a different row. Do I make any sense here? :-)
    Thank you for your time and future suggestions.

    Everywhere I've lookedApparently you didn't look in the Swing forum, because thats where Swing related questions should be posted.
    but how do I assign a cell renderer to an entire row/table ?The easiest way is to use the prepareRenderer(...) method you have already found. My "TablePrepareRenderer" class (which you can find by searching the Swing forum) shows you how to do this.
    You will note that the color of the row is determined by data in the table. So all you need to do is add another column in the TableModel that contains the information you need to determine the row color. That column can be removed from view in the table by removing the TableColumn from the TableColumnModel.

  • Setting JTable Background Row Color based on cell Value

    I am new to Java and JDeveloper (so bear with me). I have managed to create a Swing form using BC4J (Jdev 10g preview) which contains a scrollpane with a view object represented as a table. I would like to be able to color the rows that are displayed within the table differently depending on the value of a cell/column within the row. Its not obvious to me from the property inspector how to do this - I assume it is possible as all the other bells and whistles appear to be in place.
    Thanks in advance
    Simon

    Simon,
    in Swing this is not done through properties but a custom cell renderer. You can check the Swing tutorial on http://java.sun.com for how to modify the default behavior of a JTable component. A very good book to learn Swing is "Java Swing" from O'Reilly (ISBN 0-596-00408-7).
    ADF JClient uses Swing components and therefore everything you can do in Swing you can do with JClient.
    Frank

  • Display measurment data and change color depending on value

    Hi folks.  I have a 1D array of double precision measurments.  There are 20 elements in the array.  I need to display them on the front panel as well as change the backround color of each element programatically if it is out of range.  There must be an easy way to do this?
    Thanks in advance!

    Like the man says, you can't change the background color of just one element in an array. HOWEVER, there are a variety of other display options that could work, like listboxes.
    Mike...

  • Color table cells depending on value of entry in row (UI5 & JSON)

    Hello all,
    I've stuck here a bit. There have been some discussions about how to color the cells of a table that has been bound to a JSON data model. I found some answers how to color individual cells (http://http://scn.sap.com/thread/3383698), but I have a hard time to fulfill the following requirement (rephrased for simplicity):
    Given value "a" in the first cell of a table row, color the row cells 1 - 4 in blue, and 5-7 in red.
    Given value "b" in the first cell of a table row, color the row cells 1 - 4 in yellow and 5-7 in green.
    This wouldn't be a problem with normal HTML as I could easily assign style classes to every cell, however I can't find the solution for the UI5 table.
    Any ideas?
    Thanks,
    Christian

    Hi Nishant,
    not sure if I undestand you correctly.
    When I scroll the table, the data scrolls with the table correctly, but in the "Last Name" column the formatting gets messed up.
    To solve this, you need to remove the previously added classes:
      // Approach 2
        template = new sap.ui.commons.TextView().bindProperty("text", {
                parts: [
                    {path: "***" },
                    {path: "lname" }
                formatter: function(***, lname){
                   // Remove previously added classes
                  this.removeStyleClass("cyan").removeStyleClass("yellow");
                  if(*** == "m" ){
                    <!-- approach 2 step B : Add Css style class to element -->
                    this.addStyleClass("yellow");
                  }  else  if(*** == "f" )
                       this.addStyleClass("cyan");
                  return lname ;
        oTable.addColumn(new sap.ui.table.Column({ label: "Last Name",
                template: template,
    Same for the first approach using where you have to remove the custom data instead if the class.
    This should solve the problem. (By the way this is already mentioned in this thread in my post from Mar 10, 2014 11:40 AM)
    Greeets,
    ben

  • Changing Row Color In JTable !

    Hi friends
    I have two JTables. The first one has 7 columns and 10 rows (with data) but the second one is empty.
    The user can choose one row from first table and by pressing the ((copy)) button , copy that row to the second table. Is it possible to change the color of those rows(in the first table) that been copied to the second table. I mean if the user choose a row and copy then to the second table, the color of this row wold be changed.
    Can anybody help me with that??
    Thanks

    I think you are heading in the wrong direction here... you want to create a brand new DefaultTableCellRenderer and override the getTableCellRendererComponent method...
    public class MyRenderer extends DefaultTableCellRenderer {
      public Component getTableCellRendererComponent(JTable table, Object value,
                              boolean isSelected, boolean hasFocus, int row, int column) {
                  JLabel lbl = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
                               row, column);
                  //now I can change the background color depending on the row...
                  if (row == xxx) lbl.setBackground(Color.red);
    }Then use your new class on your table...
    <table.setDefaultRenderer(Object.class, new MyRenderer()); //the class must match what you TableModel returns for getColumnClass();Hope this helps,
    Josh Castagno
    http://www.jdc-software.com
    Hope this helps

  • AnyGantt: How to set different bar color depending on data value.

    Need to have different colors for bars in a gantt resource chart.
    The color depends on certain data value not included in the visible part of the chart, so it must be provided 'extra' for the chart in some way without beeing visible.
    Have studied some examples but cannot figure out the mechanism for this, and the procedure to implement it.
    Any ideas?
    Apex 4.2.2
    Regards
    Per.

    Hi Per,
    You mention "+Among the samples I can see that it has been implemented, but not how.+" - in the Information region below the chart that Tony referred to (https://apex.oracle.com/pls/apex/f?p=36648:50) that I've stated the following:
    "The elements of the Timeline region have been customized via the Chart Attributes page, using the following settings: *Custom Colors*: #000000,#00FF00,#0033FF"
    So that's how I applied custom colours to that particular chart. However, that declarative option won't meet your requirements, where the colours you wish to apply are dependent on data not included in the chart series query. You mention "+Now, if I add another column to the statement I get a yellow error message telling that the statement is not valid.+". It is not possible to simply add an additional column to the chart series query. Each supported chart type expects the associated chart series query to use specific syntax - see About Creating SQL Queries for Charts in Chapter 10 of the APEX User's Guide, http://docs.oracle.com/cd/E37097_01/doc/doc.42/e35125/bldapp_chrt.htm#BCEIJJID.
    So to answer your question "+How to add the new value if not providing another column to the graph?+", if you wish to customise your chart to that extent, then you will need to use custom XML. Depending on what exactly you're trying to achieve, this may require the generation of custom data for your chart. If you edit your chart, and take a look at the Custom XML region, you'll notice a substitution string, #DATA#, towards the end of the XML. When a chart is rendered, we use the chart series query to generate the necessary XML to represent the data on the chart, replacing the string #DATA# with the actual XML. The format of the data XML generated for gantt charts uses a bar style called "defaultStyle", and this handles the default appearance of the bars in the timeline region e.g.
    <period resource_id="1" start="2009.03.21 00.00.00" end="2009.09.21 00.00.00" style="defaultStyle"/>You'll see that the 'style="defaultStyle" ' attribute of the data corresponds to a segment of XML outlining what that style is e.g.
    <period_style name="defaultStyle">Now take a look at the Bar Style in Resource Project example in the AnyChart online documentation http://www.anychart.com/products/anygantt/docs/users-guide/bar-style.html#bar-style-application-resource-project. In that particular example, a number of different bar styles are used i.e.
    <periods>
      <period resource_id="server_1" start="2008.07.07" end="2008.07.8" style="Working" />
      <period resource_id="server_1" start="2008.07.8" end="2008.07.12" style="Maintance" />If you need to add a different bar style to particular rows of data on your chart, then you'll need to add new bar style XML for each colour you wish to use in the Custom XML on the Chart Attributes page, and then you'll also need to generate the data XML yourself, applying the necessary bar style to the relevant row of data. I've got an example of how custom data can be generated for a Resource Gantt here e.g. https://apex.oracle.com/pls/apex/f?p=36648:73 but keep in mind that particular example doesn't demo adding different styles to the data...but it can certainly give you an idea of what's entailed in the generation of custom data for your chart.
    I hope this helps.
    Regards,
    Hilary

  • Setting row color in JTable

    I am trying to display some rows in a JTable in a different color regardless of selection status. The row color criteria would be from data from the object in the row, i.e., given a certain status of a record, I would want to show it as red instead of black.

    A few optimizations:
    class StatusColorRenderer
    extends DefaultTableCellRenderer
        public StatusColorRenderer()
            // super(); not needed, call by default
            setOpaque(true);
            // DefaultTableCellRenderer returns itself as a CellRenderer Component, it extends JLabel so you can call setOpaque on it
    // since this method is called at every paint for every cell it should be HIGHLY optimized
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
        Component c = super.getTableCellRendererComponent( table, value, isSelected, hasFocus, row, column);
        MyObjectClass myObject  = ( MyObjectClass ) myTableModel.getMyVector().elementAt(row);
        <SomeClass> myStatus = myObject.getTheStatus();
        if ( myStatus.compareTo(Status.BAD_STAT) == 0)
            c.setForeground(Color.red);
        else if (myStatus.compareTo(Status.IN_PROGRESS) == 0)
            c.setForeground(Color.green);
        else if ( myObject.getOtherFactor().compareTo(OtherFactors.NOT_DEFAULT) ==  0)
            c.setForeground(Color.blue);
        else
            c.setForeground(Color.black);
        // c.repaint(); NEVER DO THAT
        return c;
    StatusColorRenderer myRenderer = new StatusColorRenderer();
    // only one instance of the renderer
    for ( int i = myTableModel.getColumnCount() - 1 ; i >= 0 ; i-- )
       //column = myTable.getColumnModel().getColumn( i );
       myTable.setDefaultRenderer( myTable.getColumnClass( i ),
                                                myRenderer );
    }Comment:
    AbstractTableModel and DefaultTableModel.getColumnClass method returns Object.class, it is used by the JTable to setup the editor and the renderer of a column
    so if you're using the DefaultTableModel and JTable without overriden getColumnClass, you can set a generic renderer for your JTable by using:
    myTable.setDefaultRenderer( Object.class, myRenderer );

  • Color report rows based on a value in that row

    I would like to Color report rows based on a value in that row.
    For examplle with the "EMP" table:
    I would like job=MANAGER to be red and job=CLERK to be green etc etc
    The other example I found only had the option of one color either the highlight or the default color.
    I am looking for a way to do multiple colors.

    Hi,
    In the row template, you can use #1#, #2#, etc to indicate where a field in the report needs to be output. This does not have to be plain text - ie, you can use it within style tags if you like.
    So, take a query like:
    SELECT EMPNO,
    ENAME,
    DEPTNO,
    DECODE(DEPTNO, 10, 'green', 20, 'red', 30, 'cyan', 'white') BG_COLOUR
    FROM EMPYou get columns 1=empno, 2=ename, 3=deptno and 4=bg_colour. In the row template, you can then do:
    Before Rows setting (sets up the table):
    &lt;table&gt;
    &lt;tr&gt;&lt;td&gt;ID&lt;/td&gt;&lt;td&gt;Name&lt;/td&gt;&lt;td&gt;Dept&lt;/td&gt;&lt;/tr&gt;After Rows setting (closes the table):
    &lt;/table&gt;Row Template 1 (used for ALL rows):
    &lt;tr style="background-color:#4#;"&gt;&lt;td&gt;#1#&lt;/td&gt;&lt;td&gt;#2#&lt;/td&gt;&lt;td&gt;#3#&lt;/td&gt;&lt;/tr&gt;Then, for every row, the colour that has been calculated using the DECODE function will be used in the style tag to colour the background for the entire row.
    How you determine the colours is up to you. I've used DECODE here, but you could use a field on the DEPT table to hold this and use this in your SQL statement.
    Andy

  • How to set color of our specified JTable's row

    i need to set color of our specified JTable's row.so send the coddings of that part.

    i need to set color of our specified JTable's row.so
    send the coddings of that part.I think you misunderstand how this forum works...
    If you have a problem, you post your problem here with example code which demonstrates said problem...
    The people here then attempt to help you if they want to.
    You do not post demands for code examples

  • Coloring table on row/column dependent mode

    hi all forumers.
    I ve got a table wich is filled line per line
    Each new line is inserted on top of table, and colored according to values of each cells.
    When a new line is inserted, all previous lines are pushed down.
    I wish colors could also be push down automaticaly, to be associated with the same cells.
    Anyone know if it is possible?
    (i m using LV6.1)
    Thanks and congratulations to this very active forum, that is often very usefull.

    I have been hesitating to answer because answers that start with "NO" are not very usefull. Well here goes.
    No, the colors can not be slid down automatically.
    If you are inserting the new lines at the top because you want to have the new entry visable, this can be done by inserting at the bottom.
    The "index values" property will let you scroll the table to keep the last row visable after each row is added to the table.
    If this acceptable, you will only have to update the colors for the cells in the row you are adding, but you would have to do this for adding at the top so no difference there.
    Ben
    Ben Rayner
    I am currently active on.. MainStream Preppers
    Rayner's Ridge is under construction

  • Setting color in 3D chart series depending on values

    Hi,
    I have a problem with a flash chart.
    Is it possible to set element color depending on element value?
    I mean, I have a 3D column chart with a serie ranging from 0 to 100 possible values.
    I would like to have blue color element (column) when element value is under 70 and red color element when element value is between 70 and 100.
    Anyone has any suggestion?
    Thank you very much
    matte

    Mateo -
    In that example that I sent you, which I did not write by the way, you will notice that there is some custom XML generated by the On Demand Process. Near the beginning of that xml is a line defining the series
    <series name="Salary Thresholds">The charting engine uses this name for the legend. You could either remove the name element or set the name="". I believe that would do the trick. You would also have to set the default color of the series to transparent.
    I have not actually attempted this so I am suggesting this as a theory. I sincerely do hope it works.
    Austin

  • Changing JTable Row Color OnMouseOver

    Hello everybody,
    is it possible to change JTable Row Color when Mouse passes over it ??
    THanks!

    Yes...
    You have to override the table cell renderer and set the color of the rows based on flag.
    The flag can be turned on/off in a mouse motion listener of the table.

  • Change the row colors based on column values in MOSS 2007.

    Hi Team,
    I am using MOSS 2007 environment. In that I am having one SharePoint list. In that list based on column values rows colors need to change.
    Kindly help me anyone on this.
    Thanks,
    Ashok

    Hi Ashok,
    Please follow the below link:
    http://www.contentmaster.com/sharepoint-2010/conditional-formatting-of-list-views-for-sharepoint-2010-changing-the-font-colour/
    http://sharepoint.stackexchange.com/questions/7478/highlight-row-color-based-on-field-values-in-sharepoint-2010-list-view
    Best Regards,
    Brij K

Maybe you are looking for

  • Unable to open any form in R12 after getting FRM 92050 error

    Hi gurus, i have some problem opening form i can switch between responsibilites but one trying to open it doesnt show any change ex: system adminstrator -->concurrent request no change earlier i was getting FRM-92050 error for which i followed this d

  • Metadata in Bridge

    In Adobe Bridge CS6 I downloaded a series of photos from a Canon 5D Mark 2.  First I downloaded them to Bridge on a PC running Windows 7.  There were no issues.  Next, I downloaded them to Bridge on an iMac running OSX Maverick.  No metadata appears

  • RMI and EventListeners

    i am creating the desktop sharing gadget via rmi,, every thing is fine apart from it does not respond to key events..i don't understand where i am lacking.. here's server code. public class ServerInterface extends UnicastRemoteObject implements Syste

  • Archived Data - Sales Order Header Text

    We have archived the object SD_VBAK and I notice that some of the sales order header text also been archived. Now, the question is from where I retrieve back those header text. I am building the info structure but I need to know the object name for t

  • Can place a swf with wmode transparent on top html without effect html link?

    i using DIV to put my swf (Param=wmode value=transparent) on top of the html, after that i can see that the swf background is transparent. but one thing happen , the html link has been overlap by the swf. i cannot click even highlight the link of the