0

I have a JTable that uses an AbstractTableModel. I'm trying to make the first row of the table a row of JCheckboxes.

EDIT: The goal is to take the columns with checked checkboxes and create a new table. This is my first time trying something like this, so I'm open to suggestions.

Here is the code I'm trying in NetBeans 7.1.1 :

private void selectSourceCBActionPerformed(java.awt.event.ActionEvent evt) {                                           
        int sourceNum = selectSourceCB.getSelectedIndex();

        DataSource currentDS = datSourceArrList.get(sourceNum);
        final ArrayList<Object[]> workArrLst1 = currentDS.getSampSet();

        sourceDetailTable.setAutoResizeMode(sourceDetailTable.AUTO_RESIZE_OFF);
        sourceDetailTable.setColumnSelectionAllowed(true);
        JTableHeader header = sourceDetailTable.getTableHeader();

        AbstractTableModel mytable1 = new AbstractTableModel() {
        Object colNames[] = workArrLst1.get(0);

            @Override
            public int getRowCount() {
                return workArrLst1.size();
            }

            @Override
            public int getColumnCount() {
                return workArrLst1.get(1).length;
            }

            @Override
            public Object getValueAt(int rowIndex, int columnIndex) {
                return workArrLst1.get(rowIndex+1)[columnIndex];
             }

            @Override
            public void setValueAt(Object value, int row, int col) {
                if(row == 1){
                        workArrLst1.get(row)[col] = Boolean(false);
                        fireTableCellUpdated(row, col);
                }
            workArrLst1.get(row)[col] = (String) value;
            fireTableCellUpdated(row, col);
            }

            @Override
            public String getColumnName(int column) {
                return (String) colNames[column];
            }

        };    

    } 

Is there anything obvious I'm missing here?

screechOwl
  • 25,740
  • 58
  • 153
  • 258
  • 1
    Usually we set a column to checkboxes, and this is often done by overriding `getColumnClass(...)`. What is your goal? Do you instead want to use check boxes in the table header? Where are you returning the boolean data for this first row? – Hovercraft Full Of Eels Jun 02 '12 at 14:50
  • @HovercraftFullOfEels: The goal is to take the columns with checked checkboxes and create a new table. This is my first time trying something like this, so I'm open to suggestions. – screechOwl Jun 02 '12 at 14:56
  • 2
    Consider putting the control in the JTable's column headers, although this isn't trivial to do well either. Have a look at this SO thread: [How can I put a control in the JTableHeader of a JTable?](http://stackoverflow.com/questions/7137786/how-can-i-put-a-control-in-the-jtableheader-of-a-jtable) – Hovercraft Full Of Eels Jun 02 '12 at 15:09
  • 1
    In a project few months ago I've used the code from http://java-swing-tips.blogspot.in/2011/03/checkboxes-in-jtable-cell.html . Don't know that may helps you too.. – Asif Jun 02 '12 at 18:14
  • 1
    You can use [multiple column selection](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#selection). – trashgod Jun 03 '12 at 03:35
  • *"I'm open to suggestions."* I suggest in future you state the 'program feature' in the question (preferably before you ask it, but edited into the question later, if need be). – Andrew Thompson Jun 03 '12 at 07:02
  • You can also [show/hide columns](http://stackoverflow.com/q/6793257/230513). Do you intend to reuse existing data? Will the two tables co-exist? Be editiable? More context is required. – trashgod Jun 03 '12 at 13:57

0 Answers0