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 6324459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:42:30+00:00 2026-05-24T16:42:30+00:00

I don’t understand how prepareEditor works, I’m not able set visibility rules for prepareRenderer

  • 0

I don’t understand how prepareEditor works, I’m not able set visibility rules for prepareRenderer with Action from prepareEditor, as I captured in attached Image

enter image description here

rules for 1.st row works as I expected, other TableCells are un-visible if JCheckBox isn’t selected (with JTable selection hightlighter too), but if I select JCheckBox in 1.st TableColumn, then I can’t shows only Rule + Mail TableColumn, only to shows all Components in the TableRow, how can I archieve that

import java.awt.Component;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

public class TableWithCheckBoxOrBlank {

    private Object[] columns = new Object[]{
        "Select", "Name", "Rule", "Mail", "Include", "Phone"};
    private Object[][] data = {
        {false, "Bill", false, "Blabla@bla", false, "00 000 000"},
        {false, "Edd", false, "Blabla@bla", false, "00 000 000"},
        {false, "Paul", false, "Blabla@bla", false, "00 000 000"},
        {false, "ZOO", false, "Blabla@bla", false, "00 000 000"}};

    public void makeUI() {
        DefaultTableModel model = new DefaultTableModel(data, columns) {

            private static final long serialVersionUID = 1L;

            @Override
            public boolean isCellEditable(int row, int column) {
                return column != 1;
            }

            @Override
            public Class getColumnClass(int columnIndex) {
                switch (columnIndex) {
                    case 0:
                        return Boolean.class;
                    case 1:
                        return String.class;
                    case 2:
                        return Boolean.class;
                    case 3:
                        return String.class;
                    case 4:
                        return Boolean.class;
                    case 5:
                        return String.class;
                }
                return null;
            }
        };
        final JTable table = new JTable(model) {

            private static final long serialVersionUID = 1L;

            @Override
            public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
                Component c = super.prepareRenderer(renderer, row, column);
                if (column == 0 || column == 1 || (Boolean) getValueAt(row, 0)) {
                    return c;
                }
                return Box.createRigidArea(c.getPreferredSize());
            }

            @Override
            public Component prepareEditor(TableCellEditor editor, int row, int column) {
                Component c = super.prepareEditor(editor, row, column);
                if (column == 0 || column == 1 || (Boolean) getValueAt(row, 0)) {
                    return c;
                }
                return Box.createRigidArea(c.getPreferredSize());
            }
        };
        model.addTableModelListener(new TableModelListener() {

            @Override
            public void tableChanged(TableModelEvent e) {
                if (e.getColumn() == 0) {
                    if ((Boolean) table.getValueAt(table.getSelectedRow(), 0)) {
                        table.setValueAt(Boolean.FALSE, table.getSelectedRow(), 2);
                    }
                    table.repaint();
                }
            }
        });
        JScrollPane scroll = new JScrollPane(table);
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JFrame frame = new JFrame("Table With Check Box Or Blank");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(150, 150);
        frame.add(scroll);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TableWithCheckBoxOrBlank().makeUI();
            }
        });
    }
}

+++

EDIT really no baterry included, I can’t see the forest for the trees 🙁

thanks to @camickr for kick between eyes, there were lots of big mistakes/problems everywhere…

enter image description here
enter image description here
enter image description here
enter image description here

import java.awt.Component;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

public class TableWithCheckBoxOrBlank {

    private Object[] columns = new Object[]{
        "Select", "Name", "Rule", "Mail", "Include", "Phone"};
    private Object[][] data = {
        {false, "Bill", false, "Blabla@bla", false, "00 000 000"},
        {false, "Edd", false, "Blabla@bla", false, "00 000 000"},
        {false, "Paul", false, "Blabla@bla", false, "00 000 000"},
        {false, "ZOO", false, "Blabla@bla", false, "00 000 000"}};

    public void makeUI() {
        DefaultTableModel model = new DefaultTableModel(data, columns) {

            private static final long serialVersionUID = 1L;

            @Override
            public boolean isCellEditable(int row, int column) {
                if (column == 0 || column == 2 || column == 4) {
                    return true;
                }
                if (column == 1 || column == 3 || column == 5) {
                    return false;
                }
                return (Boolean) getValueAt(row, 0);
            }

            @Override
            public Class getColumnClass(int columnIndex) {
                switch (columnIndex) {
                    case 0:
                        return Boolean.class;
                    case 1:
                        return String.class;
                    case 2:
                        return Boolean.class;
                    case 3:
                        return String.class;
                    case 4:
                        return Boolean.class;
                    case 5:
                        return String.class;
                }
                return null;
            }
        };
        final JTable table = new JTable(model) {

            private static final long serialVersionUID = 1L;

            @Override
            public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
                Component c = super.prepareRenderer(renderer, row, column);
                if ((column == 4 || column == 5) && (Boolean) getValueAt(row, 2)) {
                    return c;
                } else if ((column == 2 || column == 3) && (Boolean) getValueAt(row, 0)) {
                    return c;
                } else if (column == 0 || column == 1) {
                    return c;
                }
                return Box.createRigidArea(c.getPreferredSize());
            }

            @Override
            public Component prepareEditor(TableCellEditor editor, int row, int column) {
                Component c = super.prepareEditor(editor, row, column);
                if ((column == 4 || column == 5) && (Boolean) getValueAt(row, 2)) {
                    return c;
                } else if ((column == 2 || column == 3) && (Boolean) getValueAt(row, 0)) {
                    return c;
                } else if (column == 0 || column == 1) {
                    return c;
                }
                return Box.createRigidArea(c.getPreferredSize());
            }
        };
        model.addTableModelListener(new TableModelListener() {

            @Override
            public void tableChanged(TableModelEvent e) {
                if (e.getColumn() == 0) {
                    if ((Boolean) table.getValueAt(table.getSelectedRow(), 0)) {
                        table.setValueAt(Boolean.FALSE, table.getSelectedRow(), 2);
                    }
                    table.repaint();
                } else if (e.getColumn() == 2) {
                    if ((Boolean) table.getValueAt(table.getSelectedRow(), 2)) {
                        table.setValueAt(Boolean.FALSE, table.getSelectedRow(), 4);
                    }
                    table.repaint();
                }
            }
        });
        JScrollPane scroll = new JScrollPane(table);
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JFrame frame = new JFrame("Table With Check Box Or Blank");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(150, 150);
        frame.add(scroll);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TableWithCheckBoxOrBlank().makeUI();
            }
        });
    }
}
  • 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-24T16:42:31+00:00Added an answer on May 24, 2026 at 4:42 pm

    I think the prepareRenderer(...) code should be:

            public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
                Component c = super.prepareRenderer(renderer, row, column);
    
                if ((column == 2 || column == 3) && (Boolean)getValueAt(row, 0)) {
                    return c;
                }
    
            //    if (column == 0 || column == 1 || (Boolean) getValueAt(row, 0)) {
                if (column == 0 || column == 1) {
                    return c;
                }
    
                return Box.createRigidArea(c.getPreferredSize());
            }
    

    Then you should get rid of the prepareEditor(...) code. Instead you should have more logic in the isCellEditable(...) method. Something like:

            public boolean isCellEditable(int row, int column) {
    
                if (column == 0) return true;
    
                if (column == 1 || column == 4 || column == 5) return false;
    
                return (Boolean)getValueAt(row, 0);
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't understand where the extra bits are coming from in this article about
Don't think that I'm mad, I understand how php works! That being said. I
don't seem to be able to set the expiration date of a cookie within
Don't understand why #include <Header.h> is not compiling while #include Header.h is compiling with
Don't dismiss this as a newbie question! It's not, I'm not, I've tried everything,
Don't know why people do not practice AJAX implementation for authentication systems. Is it
I don’t think I’ve grokked currying yet. I understand what it does, and how
Don't worry, I'm not going to ask that question, yet again... I am wanting
I don't know why this double json response are not successful: [{first_content:content,...}][{second_content:content,...}] So i
don't understand: in my controller: @json = User.all.to_gmaps4rails do |user| \Title\: \#{user.email}\ end in

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.