Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7524231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:02:11+00:00 2026-05-30T03:02:11+00:00

I am highlighting JTable cells based on validation. In some conditions, I have to

  • 0

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]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T03:02:14+00:00Added an answer on May 30, 2026 at 3:02 am

    @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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table and I am highlighting alternate columns in the table using
The CSS syntax highlighting in vim is not entirely optimal. For example: div.special_class stops
I have a syntax highlighting function in vb.net. I use regular expressions to match
Has anyone else found VIM's syntax highlighting of Javascript sub-optimal? I'm finding that sometimes
I am not highlighting my uitableview cells when they are selected, but they contain
ReSharper has the habit of applying C# Highlighting to XAML files, meaning that every
I have syntax highlighting on, but comments are set to dark blue. This hard
I have a simple sounding problem according highlighting of a field with elasticsearch version
I have a highlighting mode (forget what it's called) that shows me, through highlighting
I have a highlighting function using JQuery, that changes the css for the clicked

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.