4

I Have created a table model extending DefaultTableModel.

public class TableModel extends DefaultTableModel {
List<VariableDetails> data;
public AttachedVariableTableModel(){
super();        
this.data=Collections.synchronizedList(new ArrayList<VariableDetails>());
}

//method for adding rows
public void addRow(VariableDetails varDetails){

    this.data.add(varDetails);
    fireTableRowsInserted(this.data.size()-1,this.data.size()-1);
    }

}

I tried to add rows to the table which has already data in it.

tModel.addRow(new  VariableDetails());

but rows cannot be added. There is no exceptions and errors. what is actually wrong here? How Can i Solve this? Thanks in advance.

2
  • Is the code shown here your full table model ? Since you are not using the data variable. Adding data to that variable has no effect Commented Nov 8, 2012 at 7:24
  • For better help sooner, post an SSCCE. Commented Nov 8, 2012 at 7:25

2 Answers 2

2
  1. for why reason is there super();

  2. DefaultTableModel can add Object[] or Vector

  3. have to override AbstractTableModel, rather than DefaultTableModel, have to override all get/set methods, with proper fireXxxXxx() in the methods, otherwise there aren't something visible in view (JTable)

  4. can start with List Table Model or Row Table Model

Sign up to request clarification or add additional context in comments.

Comments

0

May i suggest you a complete table model example to undestand how it works. It also uses a List as data. The most important thing is you need to extends AbstractTableModel to use your own variable in order to store data. Here is a complete sample of source code.

import java.util.ArrayList;
import java.util.List;

    import javax.swing.table.AbstractTableModel;

    public class MouseClickTableModel extends AbstractTableModel {
        /**
         * 
         */
        private static final long serialVersionUID = -1807522184370383957L;

        private final String[] columnNames = { "Sr", "X", "Y", "Delay (ms)",
                "Comment" };

        public final Class[] mColTypes = { Integer.class, Integer.class,
                Integer.class, Integer.class, String.class };

        private final List<MouseClick> data;

        public MouseClickTableModel(){
            data = new ArrayList<MouseClick>(10);
        }

        public int getColumnCount() {
            return columnNames.length;
        }

        public int getRowCount() {
            return data.size();
        }

        public String getColumnName(int col) {
            return columnNames[col];
        }

        public Object getValueAt(int row, int col) {
            final MouseClick currentRow = (MouseClick) data.get(row);

            switch (col) {
            case 0:
                return currentRow.getSequNb();
            case 1:
                return currentRow.getXcoo();
            case 2:
                return currentRow.getXycoo();
            case 3:
                return currentRow.getDelay();
            case 4:
                return currentRow.getComment();
            }

            return new String();
        }

        public Class getColumnClass(int c) {
            return mColTypes[c];
        }

        /*
         * Don't need to implement this method unless your table's editable.
         */
        public boolean isCellEditable(int row, int col) {
            return false;
        }

        public void updateRow(Object value, int row, int col) {

        }

        /*
         * Don't need to implement this method unless your table's data can change.
         */
        @Override
        public void setValueAt(Object value, int row, int col) {
            MouseClick currentRow = null;
            if (row >= data.size()) {
                // new data
                currentRow = new MouseClick();
                data.add(0, currentRow);
            }
            // update row
            else {
                currentRow = (MouseClick) data.get(row);
            }

            switch (col) {
            case 0:
                currentRow.setSequNb(((Integer) value).intValue());
                break;
            case 1:
                currentRow.setXcoo(((Integer) value).intValue());
                break;
            case 2:
                currentRow.setXycoo(((Integer) value).intValue());
                break;
            case 3:
                currentRow.setDelay(((Integer) value).intValue());
                break;
            case 4:
                currentRow.setComment(((String) value).toString());
                break;
            }
            // update
            fireTableCellUpdated(row, col);
        }

        public MouseClick getData(int row) {
            return data.get(row);
        }

        public void addMouseClick(MouseClick mc) {
            insertMouseClick(getRowCount(), mc);
        }

        public void insertMouseClick(int row, MouseClick mc) {
            data.add(row, mc);
            fireTableRowsInserted(row, row);
        }

        public void removeMouseClick(int row) {
            data.remove(row);
            fireTableRowsDeleted(row, row);
        }

    }

Then you just have to update your model with your ui design.

JTable table = new JTable(new MouseClickTableModel());
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
MouseClickTableModel model = getTable().getModel());
model.insertMouseClick(0, new MouseClick(0, Integer.valueOf(xValue),  Integer.valueOf(yValue), 2000, "comment"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.