Results 1 to 7 of 7
  1. #1
    reevosoft is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Post Why does the values of JTable rows change?

    Good day,

    I have not worked with JTables beyond the basics before. Can anyone give me an explanation of why the values in the JTable change when I only want to change the background color of some rows?

    What I want to do with the following code is to check if the value of the last column is 0 or less and then change only the background color of that row to yellow but for some or other reason that I am not aware of, it's changing the rest of the cells' values to 0. Below the code I have added a print screen of what the results looks like. I have smudged out the customer information so you will need to look at the headings to know more or less what values were in those columns.

    Java Code:
    JTable tblRacClientList = new javax.swing.JTable(){
        public boolean isCellEditable(int cols, int rows) {
            return false;
        }
    
        public Component prepareRenderer(TableCellRenderer renderer,int Index_row, int Index_col) {
            Component comp = super.prepareRenderer(renderer, Index_row, Index_col);
            comp.setBackground(Color.white);
            comp.setForeground(Color.black);
    
            BigInteger capLimit = new BigInteger(super.getValueAt(Index_row, 6).toString());
            if(capLimit.compareTo(new BigInteger("0")) < 1) {
                for(int x=0; x < super.getColumnCount(); x++) {
                    comp = super.prepareRenderer(renderer, Index_row, x);
                    comp.setBackground(Color.yellow);
                    comp.setForeground(Color.black);
                }
            }
            return comp;
        }
    };
    Why does the values of JTable rows change?-cap-calculation.png

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Why does the values of JTable rows change?

    What does 'super.prepareRenderer' call?
    What is the parent class?

    As an aside:
    Java Code:
    if(capLimit.compareTo(new BigInteger("0")) < 1)
    This should be:
    Java Code:
    if(capLimit.compareTo(BigInteger.ZERO) < 1)
    No sense making a new zero everytime, since BigInteger has one as a constant.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Why does the values of JTable rows change?

    Oops, should have seen it was a JTable being extended.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    reevosoft is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default Re: Why does the values of JTable rows change?

    Thanks for the tip :D.

    The parent class is the actual JTable that is created. In this case tblRacClientList is the parent object.

    I do not have much knowledge regarding this so my guess is that super.prepareRenderer gets the cell component from the parent object which can then be modified as needed. Please correct me if I'm wrong.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Why does the values of JTable rows change?

    Ah!
    Spotted it.
    prepareRendere() is applicable to a single Component, specifically the one at the cordinates supplied to the method.
    However, after getting that Component (from the super.prepareRenderer call) you then loop over the row, if the last column is a ZERO.
    This looping gets the Component for the cell at each location, setting 'comp' to that returned Component...and then returns comp after getting the last cell in the row...the cell containing the ZERO.

    You are essentially replacing each cell in your row with the last Component...the one with the ZERO.
    What happens if you don't do the loop?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    reevosoft is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default Re: Why does the values of JTable rows change?

    Hahaha, your a genius... It actually works the way I want it to. Thank you very much.

    I'll just need to sit down and go through the code making sure I understand how it works and why it did what it did.

    Java Code:
    JTable tblRacClientList = new javax.swing.JTable(){
        public boolean isCellEditable(int cols, int rows) {
            return false;
        }
    
        public Component prepareRenderer(TableCellRenderer renderer,int Index_row, int Index_col) {
            Component comp = super.prepareRenderer(renderer, Index_row, Index_col);
            comp.setBackground(Color.white);
            comp.setForeground(Color.black);
    
            BigInteger capLimit = new BigInteger(super.getValueAt(Index_row, 6).toString());
            if(capLimit.compareTo(BigInteger.ZERO) < 1) {
                comp.setBackground(Color.yellow);
                comp.setForeground(Color.black);
            }
            return comp;
        }
    };
    Why does the values of JTable rows change?-cap-calculation.png

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default Re: Why does the values of JTable rows change?

    Moved from Advanced Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. how to add rows dynamically and retrieving values in jsp
    By Gopinathsachin in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 11-27-2011, 03:06 PM
  2. JSlider change JPanel[][] rows and columns
    By Charlie161 in forum AWT / Swing
    Replies: 3
    Last Post: 02-28-2011, 05:44 PM
  3. Jtable rows
    By riddhishah28 in forum AWT / Swing
    Replies: 3
    Last Post: 02-18-2011, 06:24 AM
  4. Remove Jtable rows
    By anilkumar_vist in forum Advanced Java
    Replies: 2
    Last Post: 09-17-2010, 08:03 AM
  5. display rows in jtable
    By osval in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 08:54 PM

Posting Permissions

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