Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2011
    Posts
    36
    Rep Power
    0

    Default SOLVED JXTable removeRow doesn't exists with custom AbstractTableModel with removeRow

    Hi all,

    I have created my own custom Table Model by extending AbstractTableModel, I have created removeRow method, but in Eclipse when I try to access my Table Model, removeRow doesn't show up (or any other method I create).

    My Custom TableModel:
    Java Code:
    package com.muzikka.swing;
    
    import java.util.List;
    
    import javax.swing.table.AbstractTableModel;
    
    public class CustomTableModel extends AbstractTableModel {
    
    	private static final long serialVersionUID = 1L;
    	private List<Object> rows = null;
    	private String[] columns = null;
    	private boolean[] columnEditable;
    
    	public CustomTableModel(List<Object> linesNumber, String[] columnsNumber,
    			boolean[] editable) {
    		setRows(linesNumber);
    		setColumns(columnsNumber);
    		columnEditable = editable;
    	}
    
    	public List<Object> getRows() {
    		return rows;
    	}
    
    	public void setRows(List<Object> data) {
    		rows = data;
    	}
    
    	public String[] getColumns() {
    		return columns;
    	}
    
    	public void setColumns(String[] columnsNames) {
    		columns = columnsNames;
    	}
    
    	public void TEST() {
    		System.out.println("asdasidoas");
    	}
    	
    	public int getColumnCount() {
    		return columns.length;
    	}
    
    	public int getRowCount() {
    		return rows.size();
    	}
    	
    	public String getColumnName(int columnNumber) {
    		return columns[columnNumber];
    	}
    
    	public boolean isCellEditable(int columnNumber) {
    		return columnEditable[columnNumber];
    	}
    
    	public Object getValueAt(int lineNumber, int columnNumber) {
    		Object[] row = (Object[]) getRows().get(lineNumber);
    		return row[columnNumber];
    	}
    
    	public void setValueAt(Object data, int lineNumber, int columnNumber) {
    		if (isCellEditable(columnNumber)) {
    			Object[] row = (Object[]) getRows().get(lineNumber);
    			row[columnNumber] = data;
    			fireTableDataChanged();
    		}
    	}
    
    	public void addRow(Object[] data) {
    		getRows().add(data);
    		fireTableDataChanged();
    	}
    
    	public void removeRow(int lineNumber) {
    		getRows().remove(lineNumber);
    		fireTableDataChanged();
    	}
    
    	public Class<?> getColumnClass(int column) {
    		int rowCount = getRowCount();
    		Class<?> leastGeneralUnifier = null;
    		for (int row = 0; row < rowCount; row++) {
    			Object value = getValueAt(row, column);
    			if (value != null) {
    				Class<?> cls = value.getClass();
    				if (leastGeneralUnifier == null) {
    					leastGeneralUnifier = cls;
    				} else if (leastGeneralUnifier.isAssignableFrom(cls)) {
    					;
    				} else if (cls.isAssignableFrom(leastGeneralUnifier)) {
    					leastGeneralUnifier = cls;
    				} else {
    					leastGeneralUnifier = traverseForClass(leastGeneralUnifier,
    							cls);
    				}
    			}
    		}
    		if (leastGeneralUnifier == null) {
    			leastGeneralUnifier = Object.class;
    		}
    
    		return leastGeneralUnifier;
    	}
    
    	protected Class<?> traverseForClass(Class<?> start, Class<?> unifyWith) {
    		if (start == Object.class) {
    			return Object.class;
    		} else {
    			if (start.isAssignableFrom(unifyWith)) {
    				return start;
    			} else {
    				return traverseForClass(start.getSuperclass(), unifyWith);
    			}
    		}
    	}
    }
    My Swing code:
    Java Code:
    		final String[] columns = new String[] { "Permission name",
    				"Permission description", "Permission active" };
    		final boolean[] editableRows = { true, true, true };
    
    		TableModel tableModel = new CustomTableModel(
    				permissionController.permissionsList(), columns, editableRows);
    		customUIDataViewerPrimary.getTableDataViewerPrimary().setModel(
    				tableModel);
    In Eclipse, if I write customUIDataViewerPrimary.getTableDataViewerPrimar y().getModel().removeRow, this method doesn't show up.

    Can anyone help me with this, please?
    Last edited by buyapentiumjerk; 05-29-2011 at 09:56 PM. Reason: Solved!
    There is not knowledge that it is not power!
    buyapentiumjerk.blogspot.com

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    If your table is a JTable, when you call getModel() all that can be returned is a TableModel object (checking the API is key in situations like these). Yes it is your own CustomTableModel object, but the compiler has no way of knowing that, and since TableModel doesn't have a removeRow method, an error occurs. The solution is to cast the object returned to a CustomTableModel, and then you can call the methods of this class.

  3. #3
    Join Date
    Apr 2011
    Posts
    36
    Rep Power
    0

    Default

    Hi Fubarable,

    I dind't know I had to cast the object. Thank you! I've solved this with:

    Java Code:
    private CustomTableModel customTableModel;
    
    customTableModel = (CustomTableModel) customUIDataViewerPrimary.getTableDataViewerPrimary().getModel();
    customTableModel.removeRow(0);
    Thank you
    There is not knowledge that it is not power!
    buyapentiumjerk.blogspot.com

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Update Table GUI that extends custom AbstractTableModel
    By nidhirastogi in forum AWT / Swing
    Replies: 0
    Last Post: 02-09-2011, 04:27 PM
  2. editable row in jxtable
    By new2java2009 in forum New To Java
    Replies: 0
    Last Post: 12-30-2010, 08:43 PM
  3. creating a AbstractTableModel
    By darrenw in forum AWT / Swing
    Replies: 0
    Last Post: 10-31-2010, 01:04 PM
  4. custom jxtable header focus traversal
    By AmitYatagiri in forum AWT / Swing
    Replies: 3
    Last Post: 08-10-2010, 09:37 PM
  5. DefaultTableModel vs AbstractTableModel
    By chyrl in forum AWT / Swing
    Replies: 4
    Last Post: 05-02-2010, 01:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •