Results 1 to 3 of 3
  1. #1
    ankitmcgill is offline Member
    Join Date
    Nov 2008
    Posts
    22
    Rep Power
    0

    Default jtable cell renderer

    Have a quick question regarding setting my custom cellrenderer to a jtable.

    I can set the renderer for a jtable column using the code

    Java Code:
    TableColumnModel cm = table.getColumnModel();
    TableColumn tc = cm.getColumn(0);
    tc.setCellRenderer(new myCustomRenderer());
    Now this works just fine. What i want to ask is can have more that one type of renderer in the same column. I want the cell to have different renderer based on its value (+ve/-ve).

    Also on a side note, is it possible to assign MouseListener to a specific cell in the table ?

    Any help would be much appreciated

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    You have to change the single renderer so that it treats the cells differently. Likewise, you can have a MouseListener on the table that only does things when specific cells are involved.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    For handling MouseEvents on specific cells, use something like
    Java Code:
    public void mouseClicked(MouseEvent me){
         if(myList.locationToIndex(me.getPoint() == indexToPerformOn){
              //Perform action
    }
    if you need to have extremely different actions for each index, you may want to subclass JTable and add a performIndexAction(int index) method or something like it that calls the action for the specified index, but that could become complicated.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. add a check box to only one cell in a JTable
    By aruna1 in forum New To Java
    Replies: 4
    Last Post: 04-03-2009, 06:28 PM
  2. Color cell in JTable
    By ippacciani in forum AWT / Swing
    Replies: 3
    Last Post: 03-25-2009, 11:53 AM
  3. how to use renderer in JTable
    By sunilpatel28 in forum Advanced Java
    Replies: 0
    Last Post: 12-09-2008, 08:01 AM
  4. set different font for each cell in JTable
    By success21061985 in forum AWT / Swing
    Replies: 3
    Last Post: 09-10-2008, 02:06 PM
  5. jTable Cell editing
    By mahaling_m in forum AWT / Swing
    Replies: 0
    Last Post: 06-23-2008, 08:17 AM

Posting Permissions

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