Problem with insertion of row into a custom Table Model
I have a custom table model like this.
class GrvTableModel extends AbstractTableModel {
public String[] columnNames = ["Key" , "Value" ]
public Object[][] data = []
public selectedObjArray
//Returns the number of columns in the datamodel
public int getColumnCount() {
return columnNames.length;
}
//Returns the number of rows in the datamodel
public int getRowCount() {
return data.length;
}
//Returns the name of column in the datamodel at the specified index
public String getColumnName(int col) {
return columnNames[col];
}
//Returns the name of column in the datamodel at the specified index
def String ge开发者_JAVA百科tColumn(int colIndx) {
return super.getColumn(colIndx)
}
//Returns the object at the specified [row,column] index
public Object getValueAt(int row, int col) {
return data[row][col];
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
if (col == 1) {
return true;
} else {
return false;
}
}
//sets the object at the row and column as specified
public void setValueAt(Object value, int row, int col) {
// println "change table row: " + row
data[row][col] = value;
fireTableCellUpdated(row, col);
selectedObjArray[row].val = value
}
}
I need to insert an empty row below the selected row of the table upon clicking the "Insert Row Below" menuitem in the Popupmenu that popsup upon right click on the table. For achieving this, I've done the following.
class TablePopupImpl extends MouseAdapter implements ActionListener{
JPopupMenu tablePopup;
JMenuItem deleteCells,insertRowBelow;
public TablePopupImpl() {
tablePopup = new JPopupMenu()
deleteCells = new JMenuItem("Delete Cells")
insertRowBelow = new JMenuItem("Insert Rows below")
tablePopup.add(deleteCells)
tablePopup.add(insertRowBelow)
insertRowBelow.addActionListener(this)
}
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger()) {
tablePopup.show((Component)e.getSource(), e.getX(), e.getY());
}
}
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
tablePopup.show((Component)e.getSource(),e.getX(), e.getY());
}
}
public void actionPerformed(ActionEvent e) {
Object src=e.getSource()
if(src.equals(insertRowBelow)){
DefaultTreeModel obj = (DefaultTreeModel)table.getModel() //error here
obj.insertRow(table.getSelectedRow()+1,null)
}
}
}
The above lines throw an error stating that GrvtableModel i.e my custom table model cannot be cast into default table model.
In order to achieve this I must replace the casting of DefaultTreeModel with a cast to My custom Table Model.
GrvTableModel obj = (GrvTableModel)table.getModel()
and i must write a method inside my custom table model that can add an empty row below the selected row. Please tell me how can I write such a method/ how can I achieve my requirement?
How about using a List in stead of an array? You can then simply insert a new row at a specified index. However, you will probably need to redraw the Table somehow.
class GrvTableModel extends AbstractTableModel {
public String[] columnNames = ["Key" , "Value" ];
public List<Object[]> data = new ArrayList<Object[]>();
public selectedObjArray
//Returns the number of columns in the datamodel
public int getColumnCount() {
return columnNames.length;
}
//Returns the number of rows in the datamodel
public int getRowCount() {
return data.size();
}
//Returns the name of column in the datamodel at the specified index
public String getColumnName(int col) {
return columnNames[col];
}
//Returns the name of column in the datamodel at the specified index
def String getColumn(int colIndx) {
return super.getColumn(colIndx)
}
//Returns the object at the specified [row,column] index
public Object getValueAt(int row, int col) {
return data.get(row)[col];
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
if (col == 1) {
return true;
} else {
return false;
}
}
//sets the object at the row and column as specified
public void setValueAt(Object value, int row, int col) {
// println "change table row: " + row
data.get(row)[col] = value;
fireTableCellUpdated(row, col);
selectedObjArray[row].val = value
}
public void insertNewRow(Object[] values, int row) {
data.add(row, values);
// Trigger a table update / redraw somehow.
}
}
精彩评论