CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2008
    Posts
    2

    colouring cells in JTable

    Hello,

    I am using Swing and JTables.
    What I would like to do is to pass to my function colourCells(){..} a list of cells and a colour to colour them with. For example for a given row in my JTable, I would like some of the cells to be one colour and the rest another colour, based on some condition - which always changes.

    I have tried to extend the DefaultTableCellRenderer and to implement the following function, which is called from my function colourCells() when I iterate through the list of cells I would like to colour:

    public Component getTableCellRendererComponent(JTable table,
    Object value,
    boolean isSelected,
    boolean hasFocus,
    int row,
    int column) {...}

    As far as I can see, this renders all the cells in my JTable over and over again based on some colouring rules I put in this function:
    eg. :
    if(value instanceof String){
    cell.setBackground(Color.YELLOW);
    }else{
    cell.setBackground(Color.WHITE);
    }

    now I don't want all my cells the same colour! Like I said above, the list of cells I create should be associated with a certain colour.

    has anyone every tried to colour certain cells in a JTable? and how can you make this colouring dynamic (ie. passing in your own list of random cells to colour along with the chosen colour) ?

    thank you for your help in advance!

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: colouring cells in JTable

    Your custom TableCellRenderer can hold the list of cell colours, and when getTableCellRendererComponent(..) is called, it can use the row & column values to find the required cell colour in the list, then set the component background colour before returning it. If you need to change the colours dynamically, you'll have to keep a reference to your custom renderer and pass it the new list every time the colours change. Of course, this means that your renderer can't be an anonymous class, but it will make a handy utility class.

    Alternatively, you could use an anonymous cell renderer and give it a reference to an object it can query from getTableCellRendererComponent(..) for the correct colour of a cell. The main application could update this object whenever necessary.

    It has been said that the great scientific disciplines are examples of giants standing on the shoulders of other giants. It has also been said that the software industry is an example of midgets standing on the toes of other midgets...
    Alan Cooper
    Last edited by dlorde; November 23rd, 2008 at 05:37 PM.
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Nov 2008
    Posts
    2

    Re: colouring cells in JTable

    Thank you for the quick reply dlorde it worked!
    Just for reference I hold a list of cells inside my CustomTableCellRenderer class which I check when implementing the colouring rules in getTableCellRendererComponent(..).

    Brilliant! Thanks again

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: colouring cells in JTable

    Glad you got it working

    Thanks for getting back to us

    One of the main causes of the fall of the Roman Empire was that, lacking zero, they had no way to indicate successful termination of their C programs...
    Robert Firth
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured