Results 1 to 4 of 4
  1. #1
    simmi is offline Member
    Join Date
    Apr 2009
    Posts
    29
    Rep Power
    0

    Default Problem in row selection in JTable

    Hi,

    I have a Jtable in my application. For its cell rendering I have created a class which extends DefaultTableCellRenderer and overridden the method getTableCellRendererComponent. I am trying to change the background color of a particular column based on its value. But after doing that when I select a row its selection is not visible to me. Row is getting selected as the action on row selection is working properly but selection is not visible to me(selected row is not getting highlighted).


    public Component getTableCellRendererComponent(JTable table, Object value,
    boolean isSelected, boolean hasFocus, int row, int col) {
    Component comp = super.getTableCellRendererComponent(table, value,
    isSelected, hasFocus, row, col);
    TableModel fm = table.getModel();
    if(col == 5 &&
    ((fm.getValueAt(row, col).toString().trim()).equals("abc"))){
    comp.setBackground(Color.LIGHT_GRAY);
    }
    else{
    comp.setBackground(Color.WHITE);
    }
    return comp;
    }

    This seems to be happening because of "comp.setBackground(Color.WHITE);" in else condition. But if I remove else condition then background color of all the columns after 5th column of first row gets changed. Color of viewport of Jscrollpane of Jtbale is WHITE so I am setting the color to white in else condition.
    What should I do to see the selection.

    Thanks in Advance,
    Simmi

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Did you notice that one of the parameters to the method you are overriding is boolean isSelected? Make good use of it.

    db

  3. #3
    simmi is offline Member
    Join Date
    Apr 2009
    Posts
    29
    Rep Power
    0

    Default

    Yeah...I used if(!isSelected) and it worked.

    Thanks

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    FYI. getTableCellRendererComponent() returns the same object by default. Therefore, if you modify the object, it will stay modified. JTable does this for efficiency.

Similar Threads

  1. Jtable selection question
    By casid in forum New To Java
    Replies: 2
    Last Post: 02-04-2010, 08:11 PM
  2. Jtable Column Selection Problem
    By REVANSIDDHA in forum Advanced Java
    Replies: 0
    Last Post: 03-31-2009, 08:53 AM
  3. Problem in JTable row selection
    By shanssat in forum AWT / Swing
    Replies: 1
    Last Post: 02-04-2009, 08:50 AM
  4. Multiple selection on JTable
    By hendrix79 in forum New To Java
    Replies: 2
    Last Post: 01-30-2009, 07:11 PM
  5. JTable Combo Box Row Selection Problem
    By hemanthjava in forum AWT / Swing
    Replies: 2
    Last Post: 12-18-2008, 08:31 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
  •