Pages

Wednesday, 6 February 2013

My own TableModel (swing)

I think every java beginner encounters with this problem. How to create and to edit a JTable instance. I met this challenge as well. My first approach was simple. It was easy to choose DefaultTableModel 

 
   private static void createTable(){
       DefaultTableModel model = new DefaultTableModel();
       JTable table = new JTable(model);  
       //#1st - table constructor with a TableModel argument 
       //table = new JTable();  
       //#2 - i can add a TableModel later  
       //using setModel(model) method
       //table.setModel(model);

       Vector column1 = new Vector(); /Vector as a table column
       column1.add("data1");
       column1.add(" "); //column1 has two rows
       
       Vector column2 = new Vector();
       column2.add("data2"); //column2 has two rows
       
       model.addColumn("First", column1); 
       //model has maximum number of columns (two) 
       model.addColumn("Second", column2); 
       model.setValueAt("Change", 1, 1);
       
       table.setAutoCreateRowSorter(true); //autosorting
       //table.setSelectionMode(0);
       
   }
 
 
And then i will be able to add row such a way using tableModel.addRow(new Vector()); method or to add column: tableModel.addColumn("ColumnName", new Vector()); I added such a menu element:


      JMenuItem addRowItem = new JMenuItem("Add Row");
      addRowItem.addActionListener(new ActionListener()
      {
         @Override
         public void actionPerformed(ActionEvent event)
         {
            // add a new row to the table and in the model
            DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
            int RowCount = tableModel.getRowCount();
            tableModel.addRow(new Vector());
           // tableModel.addColumn("Added", new Vector()); so i can add a new column
            tableModel.fireTableStructureChanged();
         }
      }); 

But this approach had one "shortage" - At that time i didn't know the way how to remove a column. Now i use following code:


      JMenuItem removeColumnItem = new JMenuItem("Remove Column");
      removeColumnItem.addActionListener(new ActionListener()
      {
         @Override
         public void actionPerformed(ActionEvent event)
         {
             //table.setAutoCreateColumnsFromModel(false);
             int selected = table.getSelectedColumn();

             if (selected>=0){ //if some column was selected
                 TableColumn tcol = 
                     table.getColumnModel().getColumn(selected);
                 table.getColumnModel().removeColumn(tcol);
             }
             //DefaultTableModel tableModel = 
                    (DefaultTableModel) table.getModel();
             //tableModel.fireTableStructureChanged();
         }
      }); 
_____________
My second approach was to extend AbstractTableModel class which implements TableModel, Serializable interfaces and to use my own collection class to keep data. But AbstractTableModel class doesn't have addRow(), addColumn(), removeRow(), removeColumn() methods. I had to add and implement that methods for my class.



class MyTableModel extends AbstractTableModel{
    private  ArrayList> matrix = new ArrayList<>();
    private ArrayList columnNames = new ArrayList<>();

    public MyTableModel(int ColumnCount, int RowCount){
        super();
        if (ColumnCount < 1) ColumnCount = 1;
        if (RowCount < 1) RowCount = 1;
        
        for(int x = 0; x ());
            for (int y = 0; y < RowCount; y++){
               matrix.get(x).add("");
            }
        }
    }

    @Override
    public int getRowCount() {
        if (matrix.isEmpty()) return 0;
        return matrix.get(0).size();
    }

    @Override
    public int getColumnCount() {
        return matrix.size();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if (matrix.isEmpty()) return null;
        return matrix.get(columnIndex).get(rowIndex); //String
    }
    
    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        if (aValue instanceof String)  
            matrix.get(columnIndex).set(rowIndex, (String)aValue);
        fireTableCellUpdated(rowIndex, columnIndex);
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return true; //
 
//------------Adding a new row-------------------------------
    public int addRow(){
        if (matrix.isEmpty()) addColumn();
        
        for (int x = 0; x < matrix.size(); x++){
            matrix.get(x).add(""); //adding row for each column
        }
        fireTableStructureChanged();
        return matrix.get(0).size();
    } 
 
//------------Adding a new column-------------------------------
     public int addColumn(){
        matrix.add(new ArrayList());
        int rowsCount = matrix.get(0).size();
        
        for(int x = 0; x < rowsCount; x++)
            matrix.get(matrix.size()-1).add(""); //adding rows
        
        columnNames.add(super.getColumnName(matrix.size()-1)); 
        fireTableStructureChanged();
        return matrix.size();
    }
 

 

No comments:

Post a Comment