Results 1 to 4 of 4
  1. #1
    shivam0101 is offline Member
    Join Date
    Mar 2010
    Posts
    28
    Rep Power
    0

    Default Validating and Highlighting Jtable Cell

    I am highlighting jtable cell based on validation. In some conditions, i have to take the value of other columns, for example, if column2 has USA then column3 should be only numeric. another example, if col2 is "USA" AND col4 is numeric then col5 should be only 3 chars. Can some one point how this can be done?

    col3 contains country names. col4 and col5 depends on col3. when i am in case 3 and in case 4, i cannot check the value of case 2. example, i want like, if(col3.value == "USA")

    I posted this question in Stack Overflow and got only 2 responses. It would be very helpful if i get a solution to the problem with the present code itself with small alterations. Thanks

    Java Code:
        tcol = editorTable.getColumnModel().getColumn(0);
        tcol.setCellRenderer(new CustomTableCellRenderer());
    
        tcol = editorTable.getColumnModel().getColumn(1);
        tcol.setCellRenderer(new CustomTableCellRenderer());
    
        tcol = editorTable.getColumnModel().getColumn(2);
        tcol.setCellRenderer(new CustomTableCellRenderer());
    
        public class CustomTableCellRenderer extends DefaultTableCellRenderer {
            @Override
            public Component getTableCellRendererComponent (JTable table, Object
            value,boolean isSelected, boolean hasFocus, int row, int col){
    
            Component cell = super.getTableCellRendererComponent(table, value,
            isSelected,hasFocus, row, col);
    
           if (value instanceof String) {
               String str = (String) value;
    
               switch (col) {
                    case 0:
                        col1(str, cell);
                        break;
                    case 1:
                        col2(str, cell);
                        break;
                    case 2:
                        col3(str, cell);
                        break; 
               }
            }
            return cell;
         }
    
          private void col1(String str, Component cell) {       
                if(!str.matches("[0-9a-zA-z]")){
                    cell.setBackground(Color.RED);
                } else {
                    cell.setBackground(Color.GREEN); 
               }
         }
    
          private void col2(String str, Component cell) {       
             if(!str.matches("[A-Z]{3}")){
                 cell.setBackground(Color.RED);
             } else {
                  cell.setBackground(Color.GREEN); 
             }
         }

    [/code]

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

    Default Re: Validating and Highlighting Jtable Cell

    Moved from Advanced Java.

    Use JTable#getValueAt(row, 0/1/2)

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

  3. #3
    shivam0101 is offline Member
    Join Date
    Mar 2010
    Posts
    28
    Rep Power
    0

    Default Re: Validating and Highlighting Jtable Cell

    Excellent, that's what i was looking for.
    One more thing is, should i have to convert, row/column coordinates area view?

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

    Default Re: Validating and Highlighting Jtable Cell

    The row/column received by the renderer's method are the view coordinates. If you are querying the values from the model then yes, you need to convert from view to model coordinates.

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

Similar Threads

  1. JTable Cell Merge Highlighting and Redrawing Issue
    By officialhopsof in forum AWT / Swing
    Replies: 3
    Last Post: 12-21-2010, 07:32 AM
  2. Validating the jtable data on cell
    By anilkumar_vist in forum Advanced Java
    Replies: 4
    Last Post: 10-13-2010, 02:31 PM
  3. jtable cell background
    By anilkumar_vist in forum New To Java
    Replies: 0
    Last Post: 02-22-2010, 05:08 PM
  4. Validating Jtable
    By anilkumar_vist in forum Advanced Java
    Replies: 1
    Last Post: 11-24-2009, 03:54 PM
  5. jTable Cell editing
    By mahaling_m in forum AWT / Swing
    Replies: 0
    Last Post: 06-23-2008, 09:17 AM

Tags for this Thread

Posting Permissions

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