Question

I am highlighting JTable cells 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. As another example, if col2 is "USA" and col4 is numeric, then col5 should be only three chars. Can someone suggest how this can be done?

In the fragment below, col3 contains country names; col4 and col5 depend on col3. When I am in case 3 and in case 4, I cannot check the value of case 2. For example, I want like, if (col3.value == "USA").

    [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]
Was it helpful?

Solution

@kleopatra and @mKorbel are correct. Your fragment is incomplete, but it appears as if you are trying to solve editor and model problems in the renderer.

You can validate entered values in a custom TableCellEditor, as shown in this example. You can handle dependent columns in the TableModel, as shown in this example.

In a comment you say, "If I am not wrong, prepareRenderer() needs looping of all the rows, right?"

No, JTable "internal implementations always use this method to prepare renderers so that this default behavior can be safely overridden by a subclass." Overriding prepareRenderer() is most useful when changes must be be selectively applied to all renderers.

See Concepts: Editors and Renderers for more details.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top