Results 1 to 4 of 4

Thread: A customized JTable...

  1. #1

    Thread Starter
    Banned debbie_82's Avatar
    Join Date
    Jan 2004
    Location
    inside a hollow void
    Posts
    104

    Question A customized JTable...

    Is it possible to have different renderers for cells in a JTable... Let's just say one column has different kinds of data..
    You want one data to be rendered with a black colored font and the last element in the column be rendered blue?

    Can this be done I mean individually setting the cells of a column... Can anyone give me a code or a good site that shows how to customize JTable... I know very little and it only involves data that is same for all cells in a column...

  2. #2
    Addicted Member
    Join Date
    May 2001
    Location
    UK
    Posts
    222

    How custom?

    Hello

    I think i know what you mean, so here we go

    The JTable can take any number of cell renderers, and these are assigned to the column type, e.g. if the column is representing String.class, then you can register a renderer with the Jtable to just render the String column:
    Code:
    setDefaultRenderer(String.class, new MyStringRenderer)
    This means that whenever the table trys to render an object of type String it will use the MyStringRenderer.

    You then have to implement MyStringRenderer, now if you choose to implement DefaultTableCellRenderer then you only have to play with one method, and its pretty much a JLabel underneath it all, so you can change the colour of the text and backgrounds etc.. i'm sure you get the picture.
    Suns Java site is pretty good for this: http://java.sun.com/docs/books/tutorial/uiswing/components/table.html


    Now i wasn't sure if you wanted entire columns rendered or individual cells, if you wanted individual cells then you could implement a Renderer and use the Column and Row coordinates to decide what was in it, or use the Object to determine what you were looking at. you'd then simply register it in on the JTable against the Object.class, and i think.. and don't hold me to this, that it will try and render everything for you!

    helpful? or have i missed the point?!

    Andy

  3. #3

    Thread Starter
    Banned debbie_82's Avatar
    Join Date
    Jan 2004
    Location
    inside a hollow void
    Posts
    104

    Re: How custom?

    Originally posted by Andy_Hollywood
    Hello

    I think i know what you mean, so here we go

    The JTable can take any number of cell renderers, and these are assigned to the column type, e.g. if the column is representing String.class, then you can register a renderer with the Jtable to just render the String column:
    Code:
    setDefaultRenderer(String.class, new MyStringRenderer)
    This means that whenever the table trys to render an object of type String it will use the MyStringRenderer.

    You then have to implement MyStringRenderer, now if you choose to implement DefaultTableCellRenderer then you only have to play with one method, and its pretty much a JLabel underneath it all, so you can change the colour of the text and backgrounds etc.. i'm sure you get the picture.
    Suns Java site is pretty good for this: http://java.sun.com/docs/books/tutorial/uiswing/components/table.html


    Now i wasn't sure if you wanted entire columns rendered or individual cells, if you wanted individual cells then you could implement a Renderer and use the Column and Row coordinates to decide what was in it, or use the Object to determine what you were looking at. you'd then simply register it in on the JTable against the Object.class, and i think.. and don't hold me to this, that it will try and render everything for you!

    helpful? or have i missed the point?!

    Andy
    Helpful.... .

  4. #4
    Addicted Member
    Join Date
    May 2001
    Location
    UK
    Posts
    222
    serious?! i thought i just skilfully skirted the problem!

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