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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:44:10+00:00 2026-06-15T23:44:10+00:00

I have a JTable using AbstractTableModel where I have a JCheckBox in the first

  • 0

I have a JTable using AbstractTableModel where I have a JCheckBox in the first column for selecting rows. Now, I need to get the selected rows from the table which are checked. Right now, I am sequentially traversing from first row to the last row and getting all the rows that are selected like the following,

List<Integer> selectedRows = new ArrayList<Integer>();
for(int i = 0; i < table.getRowCount(); i++) {
     if((Boolean) table.getValuAt(i, 0)) {
         selectedRows.add(i);
     }
}

The problem here is, I need to traverse all the rows when ever I need to get the selected rows. Right now I am having 10 to 20 rows. But in future I will get around 5000 rows. My question is, if there are 5000 rows and if the user selects only 5000nd (last record) row then I need to traverse all the 5000 rows to get the selected row. Which I think is not a good approach.

One approach which I want to implement is, to add a listener to the JCheckBox column, such that when ever there is a change (SELECTED/DESELECTED) then I need to update my array of the selected rows in the listener class. In this listener class when ever user selectes a JCheckBox I need to call table.getSelectedRow(..) and I need to store if that JCheckBox is selected.

Are there any better approaches ?

  • 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-06-15T23:44:10+00:00Added an answer on June 15, 2026 at 11:44 pm

    In the example below, the TableModel updates a Set<Integer> checked in the implementation of setValueAt(). The model of an adjacent JList listens to the table’s model and displays the currently selected row numbers. The example assumes that the number of selected rows is small compared to the number of rows. Note the use of TreeSet, whose iterator retains the natural order of the elements.

    image

    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.GridLayout;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Set;
    import java.util.TreeSet;
    import javax.swing.BorderFactory;
    import javax.swing.DefaultListModel;
    import javax.swing.JFrame;
    import javax.swing.JList;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.event.TableModelEvent;
    import javax.swing.event.TableModelListener;
    import javax.swing.table.AbstractTableModel;
    
    /** @see http://stackoverflow.com/a/13919878/230513 */
    public class CheckTable {
    
        private static final CheckModel model = new CheckModel(5000);
        private static final JTable table = new JTable(model) {
    
            @Override
            public Dimension getPreferredScrollableViewportSize() {
                return new Dimension(150, 300);
            }
        };
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    JFrame f = new JFrame("CheckTable");
                    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    f.setLayout(new GridLayout(1, 0));
                    f.add(new JScrollPane(table));
                    f.add(new DisplayPanel(model));
                    f.pack();
                    f.setLocationRelativeTo(null);
                    f.setVisible(true);
                }
            });
        }
    
        private static class DisplayPanel extends JPanel {
    
            private DefaultListModel dlm = new DefaultListModel();
            private JList list = new JList(dlm);
    
            public DisplayPanel(final CheckModel model) {
                super(new GridLayout());
                this.setBorder(BorderFactory.createTitledBorder("Checked"));
                this.add(new JScrollPane(list));
                model.addTableModelListener(new TableModelListener() {
    
                    @Override
                    public void tableChanged(TableModelEvent e) {
                        dlm.removeAllElements();
                        for (Integer integer : model.checked) {
                            dlm.addElement(integer);
                        }
                    }
                });
            }
        }
    
        private static class CheckModel extends AbstractTableModel {
    
            private final int rows;
            private List<Boolean> rowList;
            private Set<Integer> checked = new TreeSet<Integer>();
    
            public CheckModel(int rows) {
                this.rows = rows;
                rowList = new ArrayList<Boolean>(rows);
                for (int i = 0; i < rows; i++) {
                    rowList.add(Boolean.FALSE);
                }
            }
    
            @Override
            public int getRowCount() {
                return rows;
            }
    
            @Override
            public int getColumnCount() {
                return 2;
            }
    
            @Override
            public String getColumnName(int col) {
                return "Column " + col;
            }
    
            @Override
            public Object getValueAt(int row, int col) {
                if (col == 0) {
                    return row;
                } else {
                    return rowList.get(row);
                }
            }
    
            @Override
            public void setValueAt(Object aValue, int row, int col) {
                boolean b = (Boolean) aValue;
                rowList.set(row, b);
                if (b) {
                    checked.add(row);
                } else {
                    checked.remove(row);
                }
                fireTableRowsUpdated(row, row);
            }
    
            @Override
            public Class<?> getColumnClass(int col) {
                return getValueAt(0, col).getClass();
            }
    
            @Override
            public boolean isCellEditable(int row, int col) {
                return col == 1;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JTable that get's populated with data from a MySQL Table, but
I have a JTable which is loaded from a data-structure using table model.The data-structure
I have a jTable with two columns.First column is set as Boolean(for checkbox) and
I am using 2 Tables (JTable) with their DefaultTableModels. The first table is already
I am using a jtable which have 8 rows by default. When I add
I am using a JInternalFrame in that i have added JTable . Now i
I have a JTable where I want to highlight some rows using a custom
I have a Jtable with a checkbox in first column. I want to strikethrough
I have a JTable with N rows and 2 columns I need to implement
I have a JTable that is using a TableColumnModelListener() to detect when the column

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.