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

Similar Messages

  • 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

  • Making a jtable row noneditable based on a value in a certain cell.

    I have a jTable (based on a database table) and one of the columns is OWNER. Is there a way to make an entire row noneditable based on the value in the OWNER column of the currently selected row?
    Here is the listener code that I have on the jTable. I want to be able to make the entire row noneditable if the value (of the currently selected row) of the OWNER column is "SYSTEM". If it is anything other than "SYSTEM" then the user would be able to change the values on the row.
    I can't override the isCellEditable method, because I only want the rows with the value of "SYSTEM" in the OWNER column to be noneditable.
    jTable2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    ListSelectionModel rowSM = jTable2.getSelectionModel();
    rowSM.addListSelectionListener(new ListSelectionListener() {
    public void valueChanged(ListSelectionEvent e) {
    if (e.getValueIsAdjusting()) return;
    ListSelectionModel lsm = (ListSelectionModel)e.getSource();
    if (lsm.isSelectionEmpty()) {
    //no rows are selected
    } else {
    int selectedRow = lsm.getMinSelectionIndex();
    if (jTable2.getValueAt(selectedRow, 1).equals("SYSTEM"))
    System.out.println("Selected Row: " + selectedRow);
    System.out.println("Owner Value: " + jTable2.getValueAt(selectedRow, 1));
    //Make all cells in this row non-updateable???
    //Need code to make the currently selected row noneditable
    disableRetailerAddToList();
    } else
    enableRetailerAddToList();
    Any direction for this problem would be greatly appreciated.

    I've resolved it with some help by using the following:
    NOT ([<plCall_Back_Required_ITAG>]=LookupValue("OCC_CUST_LOV_SR_3", "Yes") AND [<stCall_Back_Number_ITAG>] IS NULL)

  • Change Column Header / Column Background color based on a value in a specific row in the same column

    SSRS 2012
    Dataset (40 columns) including the first 3 rows for Report layout configuration (eg: the <second> row specifies the column background color).
    Starting from the 4th row, the dataset contains data to be displayed.
    I would like to change the background color of the ColumnHeader/Column based on the value in the same column in the <second> row.
    How can I accomplish the this requirement? (this must be applied for all the columns)
    Thanks

    Hi Fasttrck2,
    Per my understanding that you want to specify the background color of all the columns/column header based on the value in one special column of the special row, right?
    I have tested on my local environment and you can add expression to condition show the background color in the columns properties or the column header properties.
    Details information below for your reference:
    Specify the background color in the Column header: you can select the entire column header row and in the properties add expression in the Background color :
    If you want to specify the background color for the entire column, you can select the entire column and add the expression, repeat to add background color for other columns.
    If you want to specify the background color based on the value in the specific columns and row, you can create an hidden parameter to get the list of values from the  specific column, specify the Available values and default values by select "Get
    values from a query", finally using the expression as below to get the specific value you want:
    Expression(Backgroud Color):
    =IIF(Parameters!Para.Value(1)="1221","red","yellow")
    If your problem still exists, please try to provide some smaple data of the report and also the snapshot of the report structure to help us more effective to provide an solution.
    Any problem, please feel free to ask.
    Regards
    Vicky Liu
    If you have any feedback on our support, please click
    here.
    Vicky Liu
    TechNet Community Support

  • ADF Table cell color based on the value in that field

    Dear All,
    I want to create a table for our new application which contains a table used to represent the data in a chart format or matrix format,
    and each cell of the table should be filled with a colour out of three colors, based on the value belongs to which range eg:(1-8 ->green, 9-20 - yellow etc),
    I dont know how to implement this. Please help...
    Ranjith

    Check this out.
    How to highlight ADF table row based on column value?
    -Arun

  • 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]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • ALV rows coloring based on condition

    Hai ,
    I want Color the rows in the list based on some condition ..
    Hope to get the helpful suggestion s on this asap
    regards,

    Hi shishupalreddy,
    1. Not only the full row color,
      we can also manipulate the color in each cell,
      based upon conditions.
    2.
    IMPORTANT THINGS
    a. Extra field in internal table
    clr TYPE slis_t_specialcol_alv,
    (this field will contain the colour codes)
    b. assign fieldname to alv layout
    alvly-coltab_fieldname = 'CLR'
    c. work area for colour
    DATA : clrwa TYPE slis_specialcol_alv.
    d. Populating the color
    Once again
    Loop at ITAB.
    *********logic
    if itab-field < 0 "---negative
    clrwa-fieldname = 'FIELDNAME'. "<--- FIELDNAME FOR COLOR
    clrwa-color-col = 6. <------- COLOUR 0-9
    APPEND clrwa TO itab-clr.
    MODIFY ITAB.
    endif.
    ENDLOOP.
    5. just copy paste in new program
    6.
    REPORT abc .
    NECESSARY / MUST
    TYPE-POOLS : slis.
    DATA : alvfc TYPE slis_t_fieldcat_alv.
    DATA : alvly TYPE slis_layout_alv.
    ITAB DECLARATION
    DATA : prg TYPE sy-repid.
    DATA : BEGIN OF itab OCCURS 0.
    INCLUDE STRUCTURE t001.
    DATA : clname(3) TYPE c,
    clr TYPE slis_t_specialcol_alv,
    END OF itab.
    DATA : clrwa TYPE slis_specialcol_alv.
    PARAMETERS : a TYPE c.
    DATA : flname TYPE slis_fieldname.
    SELECT
    START-OF-SELECTION.
    SELECT * FROM t001
    INTO CORRESPONDING FIELDS OF TABLE itab..
    LOOP AT itab..
    IF SY-TABIX <= 5.
    itab-clname = 'C50'.
    ELSE.
    itab-clname = 'C30'.
    ENDIF.
    MODIFY itab.
    ENDLOOP.
    LOOP AT ITAB.
    check itab-bukrs = '1000'
    clrwa-fieldname = 'BUTXT'.
    clrwa-color-col = 6.
    APPEND clrwa TO itab-clr.
    MODIFY ITAB.
    clrwa-fieldname = 'LAND1'.
    clrwa-color-col = 4.
    APPEND clrwa TO itab-clr.
    MODIFY ITAB.
    ENDLOOP.
    prg = sy-repid.
    flname = 'CLNAME'.
    alvly-info_fieldname = 'CLNAME'.
    alvly-coltab_fieldname = 'CLR'.
    LOOP AT ITAB.
    if sy-tabix = 3.
    clrwa-fieldname = 'BUTXT'.
    clrwa-color-col = 6.
    APPEND clrwa TO itab-clr.
    MODIFY ITAB.
    clrwa-fieldname = 'LAND1'.
    clrwa-color-col = 1.
    APPEND clrwa TO itab-clr.
    MODIFY ITAB.
    endif.
    ENDLOOP
    CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
    EXPORTING
    i_program_name = prg
    i_internal_tabname = 'ITAB'
    i_inclname = prg
    CHANGING
    ct_fieldcat = alvfc
    EXCEPTIONS
    inconsistent_interface = 1
    program_error = 2
    OTHERS = 3.
    minimum
    *CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
    EXPORTING
    it_fieldcat = alvfc
    TABLES
    t_outtab = itab
    EXCEPTIONS
    program_error = 1
    OTHERS = 2
    extra
    sy-uname = 'XYZAB'.
    prg = sy-repid.
    Excluding
    DATA : excl TYPE slis_t_extab.
    DATA : exclwa TYPE slis_extab.
    exclwa = '&OUP'.
    APPEND exclwa TO excl.
    exclwa = '&ODN'.
    APPEND exclwa TO excl.
    CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
    EXPORTING
    it_fieldcat = alvfc
    i_callback_program = sy-repid
    is_layout = alvly
    i_callback_user_command = 'ITAB_USER_COMMAND'
    it_excluding =
    excl
    i_save = 'A'
    TABLES
    t_outtab = itab
    EXCEPTIONS
    program_error = 1
    OTHERS = 2.
    *& Form itab_user_command
    text
    -->WHATCOMM text
    -->WHATROW text
    FORM itab_user_command USING whatcomm TYPE sy-ucomm whatrow TYPE
    slis_selfield.
    BREAK-POINT.
    ENDFORM. "itab_user_command
    regards,
    amit m.

  • How to set JTable column's color?

    How can I set JTable Columns' color? I only found this class DefaultTableCellRenderer
    which can set cell's color.

    rmalina wrote:
    You are going to need to derive a renderer class for your Column from DefaultTableCellRenderer and override the following function with something like this:
    @Override
         public Component getTableCellRendererComponent(JTable jTable, Object oValue, boolean isSelected, boolean hasFocus, int nRow, int nColumn) {
    super.setForeground(Color.GREEN);
    super.setBackground(Color.GREEN);
    }That would set your column to green.
    Edited by: rmalina on Jul 28, 2008 8:47 AMHow can I know I only change the columns' color instead of other cells?

  • Entire row conditional formatting based on cell value

    What is the best way to apply conditional formatting to an entire row based on the value of one of the columns on that row? For example, I just want to highlight the entire row (make it red color) if the value of one of the columns is greater than 0. The method described in the past discussions is just way to difficult to apply in real life scenarios. I think it should work without any tricks as it's just a very basic functionality of spreadsheets.

    The other idea is to make a second table, which has a single column and is as wide as the first table.  This second table will reference the value in the first table which indicates how to shade and is formatted with conditional formatting.  The second table is slid behind the first, AND the first is made transparent.
    - Bottom table refers to original (top) table
    bottom table contains conditional formatting as:
    Now make the original table transparent by selecting the table and changing the background fill to none.
    Now send the second table to the back by selecting the table then the menu item "Arrange > Send to Back"
    Now slide the second table under the original

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

  • Custom itemRenderer component based on cell value: error 1009

    I'm working on an item renderer for a dataGrid that has different states depending on the cell and row values.
    The cell value is a toggle (true or null), and sets whether content should be shown in the cell or not
    The row properties determine what is shown when the cell value is true.
    The dataGrid dataProvider is populated based on user id input.
    I created the itemRenderer as a custom actionscript component, closely following this example:
    360Flex Sample: Implementing IDropInListItemRenderer to create a reusable itemRenderer
    However, my component results in Error #1009 (Cannot access a property or method of a null object reference) when a user id is submitted.
    package components
         import mx.containers.VBox;
         import mx.controls.*;     import mx.controls.dataGridClasses.DataGridListData;
         import mx.controls.listClasses.BaseListData;
         import mx.core.*;
         public class toggleCellRenderer extends VBox
              public function ToggleCellRenderer()
              {super();}
              private var _listData:BaseListData;   
                   private var cellState:String;
                   private var cellIcon:Image;
                   private var imagePath:String;
                   private var imageHeight:int;
                   private var qty:String = data.qtyPerTime;
                   private var typ:String = data.type;
              public function get listData():BaseListData
                   {return _listData;}
              public function set listData(value:BaseListData):void
                   {_listData = value;}
              override public function set data(value:Object):void {
                   super.data = value;
                   if (value != null)
                   //errors on next line: Error #1009: Cannot access a property or method of a null object reference.
                   {cellState = value[DataGridListData(_listData).dataField]}
              override protected function createChildren():void {
                   removeAllChildren();
                   if(cellState=='true'){
                        cellIcon = new Image();
                        addChild(cellIcon);
                   //there is another state here that adds another child...
              //next overrides commitProperties()...
    There are no errors if I don't use an itemRenderer--the cells correctly toggle between "true" and empty when clicked.
    I also tried a simple itemRenderer component that disregards the cell value and shows in image based off row data--this works fine without errors or crashing. But I need to tie it to the cell value!
    I have very limited experience programming, in Flex or any other language. Any help would be appreciated.

    Your assumption that the xml file either loads with "true" or nothing  is right.
    After modifying the code to the following, I don't get the error, but it's still not reading the cell value correctly.
    package components
         import mx.containers.VBox;
         import mx.controls.*;   
         import mx.controls.dataGridClasses.DataGridListData;
         import mx.controls.listClasses.BaseListData;
         import mx.core.*;
         public class toggleCellRenderer extends VBox
              public function ToggleCellRenderer()
               super();
              private var _listData:BaseListData;   
              private var cellState:Boolean;
              private var cellIcon:Image;
              private var imagePath:String;
              private var imageHeight:int;
              private var qty:String;
              private var typ:String;
               public function get listData():BaseListData
                 return _listData;
              override public function set data(value:Object):void {
                   cellState = false;
                   if (listData && listData is DataGridListData && DataGridListData(listData).dataField != null){
                       super.data = value;
                       if (value[DataGridListData(this.listData).dataField] == "true"){
                           cellState = true;
              override protected function createChildren():void {
                   removeAllChildren();
                   if(cellState==true){
                        cellIcon = new Image();
                        addChild(cellIcon);
                   //there is another state here that adds another child...
              //next overrides commitProperties()...
    - didn't set the value of qty or typ in the variable declarations (error 1009 by this too--I removed this before but wanted to point out in case its useful)
    - added back in the get listData() function so I could use the listData
    - changed the null check
    All cells are still returning cellState = false when some are set to true, even if I comment out [if (value[DataGridListData(this.listData).dataField] == "true")] and just have it look for non-null data. That shouldn't make a difference anyway, but it confirms that all cells are returning null value.
    Swapping out the first if statement in set data with different variables results in the following:
    [if (listData != null)]  all cells return null (cellState=false for all)
    both [if (value != null)] and  [if (DataGridListData != null)]  results in error 1009 on a line following the if, so I assume they return non-null values.
    All rows have data, just not all fields in all rows, so shouldn't listData be non-null?  Could it be that the xml file hasn't fully loaded before the itemRenderer kicks in?
    I also realized  I had removed the item renderer from many of the columns for testing, and since some columns are hidden by default only one column in the view was using the itemRenderer--hence the single alert per row I was worried about earlier.
    Thanks for your help so far.

  • Dynamic table rows created based on input value

    I've been searching the forum for either a tutorial or input
    on how to create a table where the number of rows are based on a
    user input value. I can't seem to find anything on this.
    Here's what I'm trying to do. I have site where people are
    charged based on the number of items they register. So if they want
    to register 2 items. They would input the number 2 for quanity and
    I would display a dynamic table with 2 rows. The user will input
    their data for the 2 items and I insert this into an
    itemsRegistered table. If they entered 4 I would give them 4 rows
    for input. I am using dreamweaver with PHP and MySql database. Can
    anyone point me in the right direction. Any help would be
    appreciated!

    Not exactly. The items are all the same but have a different
    registration number (like a serial number). So if the user wants to
    register 2 items. They would input the number 2 on the form. Then 2
    rows would appear on the table, one for each registration number.
    When they finish entering the data it would be inserted into the
    database as 2 records.
    So for example if item 1 has registration number 10000 and
    item 2 has registration number 11000. The registration numbers are
    unique. The table would look like this :
    CustomerID (links to customer table)
    RegistrationID (Unique in this table)
    ItemColor
    ItemShape
    I don't know if I've explained this well but thanks for even
    attempting to answer this! I really appreciate it!

  • How can I do a multi-row insert based on a value in a field on my form?

    My Form contains these fields (for the purpose of inserting rows into my 'Ports' table)
    ..Number_of_Ports
    ..Device_Name
    ..Router_Name
    ..Router_Slot_Number
    ..Router_Port_Number
    ..Vlan_Number
    Based on the value entered for 'Number_of_Ports'
    I would like to insert 'X' number of rows into my 'Ports' Table
    with the values which are contained in:
    ..Device_Name
    ..Router_Name
    ..Router_Slot_Number
    ..Router_Port_Number
    ..Vlan_Number
    Can someone help me with this,
    Or should I post this on another forum?
    Thanks in advance- Gary

    Gary,
    You can accomplish this with a PL/SQL process using a FOR LOOP. For the following example, I am going to use fields that would have been generated for Page 1 of an application:
    Begin
    FOR i IN 1..:P1_NUMBER_OF_PORTS LOOP
    INSERT INTO tablename(port_number, device_name, router_name, router_slot_number_vlan_number)
    VALUES(i, :P1_DEVICE_NAME, :P1_ROUTER_NAME, :P1_ROUTER_SLOT_NUMBER, :P1_ROUTER_PORT_NUMBER, :P1_VLAN_NUMBER);
    End Loop;
    End;
    Hope this helps.
    Mike

  • Conditional Formatting - how to change cell colour based on cell value

    I am in the process of moving some Excel spreadsheets into Numbers on my iPad. The Excel spreadsheet had conditional formatting in certain cells. If the cell value was less than a certain amount (<46 for example), the cell background would automatically turn yellow.
    Is there any way to make Numbers on iPad do this?
    Thanks for any help you can provide.
    Scott

    I beleive this is one of those doable tings on desktop that gets taken out on ipad. If you would like to suggest this as an idea for apple to put in, please go to http://www.apple.com/feedback

  • Conditional Format based on cell value

    For example; want to turn a cell yellow if its value is less than another cell value.
    Is this possible?
    Conditional format rules don't seem to like cell references.

    There are many ways to call attention to a value that needs to be emphasized because of its relationship to other values. What you decide to do will be influenced by how particular you are about the end result vs. the trouble that you will have to go through to achieve it. When other cells need to reference a cell that may be either text or numeric, you can use the TRIM function, or other text functions, to remove interfering characters. Here's an example, where you can see the last row multiplying the previous, manipulated row, by 2.
    Also, all the cells are formatted as Currency, with to ill effect. In the example I left the cells with the conditional format in default alignment so you could see the added space character against the left margin of the cell.
    I think the easiest way to call attention to a cell is to add an adjacent row of column with a conditional expression that 'lights-up' when there is an alarm condition.
    Jerry

Maybe you are looking for