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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:33:21+00:00 2026-05-26T12:33:21+00:00

I have a JTable using a custom DefaultTableModel which has some Booleans in the

  • 0

I have a JTable using a custom DefaultTableModel which has some Booleans in the last column (displayed as tick boxes).

When I add a MouseListener to retrieve the value of what’s been clicked it appears that the toggling of tick boxes no longer takes place.

// As soon as this is used in the component
// that is using the JTable, the toggling stops
table.addMouseListener(new MouseAdapter() {  
            public void mouseClicked(MouseEvent evt) {  
                int col = table.getSelectedColumn();  
                int row = table.getSelectedRow();

                Object o = table.getModel().getValueAt(row, col);

I assume that the event is being consumed by the listener. What can I add to the MouseListener code to restore the toggling behaviour?

Edit:

Oops, it appears that the issue lies with my override:

@Override
public void setValueAt(Object aValue, int row, int column) {

    // Attempt at mutually exclusive checkboxes
    if( column == 2 ){ // Starts at 0. Seek an alternative solution to avoid hardcoding?
        // Algorithm: cycle through List to set other Booleans to false

        // Uses entities. Is there another way of getting the number of rows from model?
        List<MyEntity> myEntities = this.getDatas();

        for( int i = 0; i < myEntities.size(); i++ ){
            if( i != row ){
                // Make sure this calls parent
                super.setValueAt( false , i, 2);
            }
        }

    } else {
        super.setValueAt(aValue, row, column); // Call parent class
    }

}  
  • 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-26T12:33:21+00:00Added an answer on May 26, 2026 at 12:33 pm

    Don’t add your own MouseListener. Instead override setValueAt() in the TableModel to see the new value set by the default editor for Boolean.class.

    Addendum: Here’s an sscce. For expedience, it simply clears all entries in CHECK_COL, sets the new value and conditions the button accordingly.

    CheckOne image

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.table.DefaultTableModel;
    
    /**
     * @see http://stackoverflow.com/questions/7920068
     * @see http://stackoverflow.com/questions/4526779
     */
    public class CheckOne extends JPanel {
    
        private static final int CHECK_COL = 1;
        private static final Object[][] DATA = {
            {"One", Boolean.FALSE}, {"Two", Boolean.FALSE},
            {"Three", Boolean.FALSE}, {"Four", Boolean.FALSE},
            {"Five", Boolean.FALSE}, {"Six", Boolean.FALSE},
            {"Seven", Boolean.FALSE}, {"Eight", Boolean.FALSE},
            {"Nine", Boolean.FALSE}, {"Ten", Boolean.FALSE}};
        private static final String[] COLUMNS = {"Number", "CheckBox"};
        private DataModel dataModel = new DataModel(DATA, COLUMNS);
        private JTable table = new JTable(dataModel);
        private ControlPanel cp = new ControlPanel();
    
        public CheckOne() {
            super(new BorderLayout());
            this.add(new JScrollPane(table));
            this.add(cp, BorderLayout.SOUTH);
            table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            table.setPreferredScrollableViewportSize(new Dimension(250, 175));
        }
    
        private class DataModel extends DefaultTableModel {
    
            public DataModel(Object[][] data, Object[] columnNames) {
                super(data, columnNames);
            }
    
            @Override
            public void setValueAt(Object aValue, int row, int col) {
                if (col == CHECK_COL) {
                    for (int r = 0; r < getRowCount(); r++) {
                        super.setValueAt(false, r, CHECK_COL);
                    }
                }
                super.setValueAt(aValue, row, col);
                cp.button.setEnabled(any());
            }
    
            private boolean any() {
                boolean result = false;
                for (int r = 0; r < getRowCount(); r++) {
                    Boolean b = (Boolean) getValueAt(r, CHECK_COL);
                    result |= b;
                }
                return result;
            }
    
            @Override
            public Class<?> getColumnClass(int col) {
                if (col == CHECK_COL) {
                    return getValueAt(0, CHECK_COL).getClass();
                }
                return super.getColumnClass(col);
            }
    
            @Override
            public boolean isCellEditable(int row, int col) {
                return col == CHECK_COL;
            }
        }
    
        private class ControlPanel extends JPanel {
    
            JButton button = new JButton("Button");
    
            public ControlPanel() {
                button.setEnabled(false);
                this.add(new JLabel("Selection:"));
                this.add(button);
            }
        }
    
        private static void createAndShowUI() {
            JFrame frame = new JFrame("CheckOne");
            frame.add(new CheckOne());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    createAndShowUI();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using a jtable which have 8 rows by default. When I add
I have a JTable where I want to highlight some rows using a custom
I have a JTable that is using a TableColumnModelListener() to detect when the column
I have some problems displaying data in JTable. My app is using a JTable
How do i sort jtable column using radio button? my jtable is defaultTableModel not
I have a JTable that is populated using a custom TableModel I created. I
I have a JTable that will have the last column data field change to
I have a GUI with a JTable using the DefaultTableModel. These instance variables are
I have a JTable where one column occasionally has a fair amount of text
I have a JTable that holds several JPanels using a custom renderer/editor. The JPanel

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.