Forum Stats

  • 3,750,854 Users
  • 2,250,264 Discussions
  • 7,867,184 Comments

Discussions

JTable-Row color issue

701922
701922 Member Posts: 44
edited Jul 4, 2008 6:07AM in Swing
Hi !

I have JTable... with its 5columns,
5th column is off Boolean Type.(check boxes)
What I require.. is when user checks the JCheckbox.. I want that entire row to change its color and when un-checked reture back to its orginal color.

I use DefaultTableModel.

Pls post a simple samplecode..Thanks in advance.

Pls help !!

Edited by: dashok.83 on Jun 30, 2008 2:15 AM
«1

Comments

  • PhHein
    PhHein Member, Moderator Posts: 7,160 Silver Trophy
    [JTable tutorial about renderers|http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#renderer]
  • camickr
    camickr Member Posts: 24,931
    I would override the prepareRenderer() method of JTable instead of creating a custom renderer. I've posted an example in the forum.
  • 701922
    701922 Member Posts: 44
    edited Jul 1, 2008 12:16AM
    I got this from the other thread...
    public Component prepareRenderer(
    TableCellRenderer renderer, int row, int column)
    	{
         	   Component c = super.prepareRenderer(renderer, row, column);
     		if (!isRowSelected(row))
    		    {
    			String type = (String)getModel().getValueAt(row, 0);
    			c.setBackground(
    			"Buy".equals(type) ? Color.GREEN : Color.YELLOW );
    		}	
     
    //				if (isRowSelected(row) && isColumnSelected(column))
    //					((JComponent)c).setBorder(new LineBorder(Color.red));
     
    				return c;
    			}
    Can u tell .. is there a way to set the row color.. depending on the cell value.

    ie, my fifth column has JCheckboxs... if user checks them I want the row to be colored(any color) and if he un-checks... the color s'd be re-set to the original...

    pls help ... !!!

    Edited by: dashok.83 on Jun 30, 2008 9:14 PM
  • 843806
    843806 Member Posts: 49,998
    getModel().getValueAt(row, 4);
    depending on the value of you can set color accordingly
  • 701922
    701922 Member Posts: 44
    I try like this... But its not working...

    It is setting color for all the rows above... the the row that is selected.

    Moreover ... it is not checking for the cell value :-(


    public Component prepareRenderer(
    						TableCellRenderer renderer, int row, int column)
    					{
    						Component c = super.prepareRenderer(renderer, row, column);
    		 
    						/*if (!isRowSelected(row))
    						{
    							String type = (String)getModel().getValueAt(row, 0);
    							c.setBackground(
    								"Buy".equals(type) ? Color.GREEN : Color.YELLOW );
    						}*/
    						
    						//if (isRowSelected(row) && isColumnSelected(5))
    						if (isRowSelected(row) || getModel().getValueAt(row, 5)==new Boolean(true))
    							//((JComponent)c).setBorder(new LineBorder(Color.red));
    							c.setBackground(Color.ORANGE);
    						else if (getModel().getValueAt(row, 5)==new Boolean(false))
    							c.setBackground(Color.LIGHT_GRAY);
    						return c;
    					}
  • darrylburke
    darrylburke Member Posts: 18,007
    It is setting color for all the rows above... the the row that is selected.
    You have an if / else if structure with no default fallback. If both conditions evaluate to false, the color last set will carry over.

    You also a lot of krap with new Boolean(...) in your test conditions which makes me think you have no business coding GUIs with custom features: you need to go back to basics and learn the Java language.

    Maybe something like (tweak the colors to what you want to achieve)
    if (isRowSelected(row) || getModel().getValueAt(row, 5)) {
       c.setBackground(Color.ORANGE);
    } else if (!getModel().getValueAt(row, 5)) {
       c.setBackground(Color.LIGHT_GRAY);
    } else {
       c.setBackground(Color.GREEN);
    }
    For posting code, indent with 2 to 4 spaces, not tabs as the forum software doesn't handle tabs very well.

    db
  • 701922
    701922 Member Posts: 44
    edited Jul 1, 2008 6:57AM
    Thanks ! Darryl.

    Yep..will learn the basics.
    But tell me this...
    below code does change the color.. but it is changing only when I click again on another CheckBox.
    ie, To see teh color change I need to check/uncheck another checkBox
    public Component prepareRenderer(
      TableCellRenderer renderer, int row, int column)
         {
            Component c = super.prepareRenderer(renderer, row, column);
            if (isRowSelected(row) || getModel().getValueAt(row, 5).equals(true)) {
    	c.setBackground(Color.ORANGE);
            }
            else if (getModel().getValueAt(row, 5).equals(false)) {
                    c.setBackground(Color.LIGHT_GRAY);
            } 
            else{
                   c.setBackground(Color..WHITE);
             }
    return c;
    }
    Edited by: dashok.83 on Jul 1, 2008 3:46 AM

    Edited by: dashok.83 on Jul 1, 2008 3:56 AM
  • darrylburke
    darrylburke Member Posts: 18,007
    edited Jul 1, 2008 7:22AM
    To get better help sooner, post a SSCCE that clearly demonstrates your problem.

    And WTF are you testing .equals(true) and .equals(false) for?
    if (something == true)
    is the same as
    if (something)
    and
    if (something == false)
    is the same as
    if (!something)
    And using the equals method to compare a primitive return value to a primitive literal (both of which will probably be autoboxed) may or may not work as expected (I wouldn't even want to try) but makes for code that is painful to read and maintain.
    db
  • 701922
    701922 Member Posts: 44
    hey,

    WILL TALK ABOUT THAT LATER....

    Can address to my question... ???
  • camickr
    camickr Member Posts: 24,931
    edited Jul 1, 2008 10:35AM
    Can address to my question... ???
    Your question was addressed. The answer was to post a SSCCE so we can see exactly what is happening.
This discussion has been closed.