removeRow() method in custom TableModel Swing
I have a JTable that is populated using a custom TableModel I created. I have another JTable that rows can be added to from the first JTable. I can add the row to the new JTable no problem, but I need to remove that row from the first JTable upon adding it to the new JTable. Unfortunately removeRow() is only a method for DefaultTableModel, and I've checked the source code on that and it doesn't show up...
Thanks in advance!
here's my custom table model:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ttp;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.*;
import java.util.*;
/**
*
* @author ericrea
*/
/**creates the model for the accounts table*/
public class tableModel extends AbstractTableModel {
//private int rowCount = 0;
private static final int COLUMN_COUNT = 3;
private Conceptual_Package pp;
Conceptual_Package j = GUIpos.i;
public tableModel(Conceptual_Package pp) {
this.pp = pp;
}
/**sets the c开发者_StackOverflowolumn headers*/
public String getColumnName(int i){
switch (i) {
case 0:
return "Sec";
case 1:
return "Row";
case 2:
return "Seat";
default:
return null;
}
}
/**figures out how many rows the model needs*/
public int getRowCount() {
int h = 0;
try {
h = Physical_PackageDAO.getInstance().getByConceptual_Package(j.getId()).size();
} catch (DataException ex) {
Logger.getLogger(tableModel.class.getName()).log(Level.SEVERE, null, ex);
}
return h;
}
/**Figures out number of columns*/
public int getColumnCount() {
return COLUMN_COUNT;
}
/**gets the account information from the Physical_Package*/
public Object getValueAt(int rowIndex, int columnIndex) {
String a = null;
String b = null;
String c = null;
try {
Physical_Package pp = Physical_PackageDAO.getInstance().getByConceptual_Package(j.getId()).get(rowIndex);
a = pp.getVenueSeat().getRowInVenue().getSectionInVenue().getSectionNumber();
b = pp.getVenueSeat().getRowInVenue().getRowNumber();
c = pp.getVenueSeat().getSeatNumber();
} catch (DataException ex) {
Logger.getLogger(tableModel.class.getName()).log(Level.SEVERE, null, ex);
}
switch (columnIndex) {
case 0:
return a.trim();
case 1:
return b.trim();
case 2:
return c.trim();
default:
return null;
}
}
/**gets the right account for the Physical_Package*/
public Physical_Package getCPackage(int index){
Physical_Package d = null;
try {
Physical_PackageDAO.getInstance().getByConceptual_Package(j.getId()).get(index);
} catch (DataException ex) {
Logger.getLogger(tableModel.class.getName()).log(Level.SEVERE, null, ex);
}
return d;
}
public void removeRow(int index){
}
//write in mainFrame, in panel
//ValueChanged{
// get the selected name
// Physical_Packages.get()
// namefield.settext(pp.getname);
//
//}
}
Unfortunately removeRow() is only a method for DefaultTableModel
Take a look at the source code for the DefaultTableModel. Both of these methods are implemented so you can use the code and an example of what your code might look like. The key ar the "fireXXX" methods. Invoking these methods will notify the table that the model has changed so the table can repaint itself.
From your older question (which you just deleted) you state "I can add the row to the new jTable no problem,"
From looking at your code (before you deleted the question) I have the following comment:
Its looks to me like you are creating a completely new TableModel. That is not the same as adding a row to an existing model. That does not seem like a very good solution. Instead you need to create an addRow(...)
If you find the code too confusing in the DefaultTableModel, then here is some code that shows how I've implemented addRow(...) and removeRow(...) methods. See Row Table Model. The class shows the proper fireXXX methods to use for each method.
精彩评论