Hi!!
AbstractTableModel has been used in my Jtable. Mymodel is the object of MyTableModel.When I use ‘Findbtn’ for query from oracle database . data is retrieved
,but at that time any new record cann’t be inserted below the existing record.General error message has been shown.Normally data can be inserted through ‘savebtn’.But I need to do it for Auto numbering.It means when I press the Enterbtn of the Keyboard it will increase 1 for the next record. I need it for IncrementID(field of a table).I have given the partial code of mine.Plz help me how can I insert data and create auto nubering with sample code or idea.
/////////////////////////////////////
class MyTableModel extends AbstractTableModel {
String[] columnNames = {"IncrementID","Designation","Department","Date"}; //&&&&&
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return NRows;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return rowData[row][col];
}
public Class getColumnClass(int c) {
if(c==0) return Integer.class; //&&&&&&&
//if(c==1) return Integer.class;
if(c==1) return String.class;
if(c==2) return String.class;
if(c==3) return String.class;
if(c==4) return String.class;
return null;
}
public boolean isCellEditable(int row, int col) {
return true;
}
public void setValueAt(Object value, int row, int col) {
if (col==0){
rowData[row][0]=value;
}
if(col==1){
rowData[row][1]=value;
// showId(value.toString(), row);
// to load
unit of measure combo
}
if(col==2){
rowData[row][2]=value;
}
if(col==3 ){//col 4 contains qty
rowData[row][3]=value;
int r=jTable1.getSelectedRow(); //?? r&nr
int nr=jTable1.getRowCount()-1;
if(r==nr)
insertRow();
}
if(col==4 ){//col 4 contains qty
rowData[row][col]=value;
}
}
//////////////////////////////////
void insertRow(){
int rows=myModel.getRowCount();
NRows=NRows+1;
int i=0;
if(rows>0)
i=rows;
rowData[i][0]=null;
rowData[i][1]="";
rowData[i][2]="";
rowData[i][3]="";
myModel.fireTableDataChanged();
}
////////////////////////actionevent for save/////////////////////////////
void jButtonSave_actionPerformed(ActionEvent e) {
setEmployeeTableData();
}
public void setEmployeeTableData(){
getSwingFieldsData();
PreparedStatement prepStatement=null;
try{
int numRows = jTable1.getRowCount();
for(int i=0; i<numRows; i++){
PreparedStatement stmt = con.prepareStatement("INSERT INTO TBL_INCREMENT VALUES (?, ?, ?, ?, ?)");
stmt.setInt(1, Integer.parseInt(rowData[i][0].toString()));
stmt.setInt(2, Integer.parseInt(strEMPMID));
// stmt.setInt(1, Integer.parseInt(rowData[i][0].toString()));
stmt.setString(3, rowData[i][1].toString());
stmt.setString(4, rowData[i][2].toString());
stmt.setString(5, rowData[i][3].toString());
stmt.execute();
stmt.clearParameters();
JOptionPane.showMessageDialog(null," Saved IncrementTable data");
}
}
catch(SQLException e){
JOptionPane.showMessageDialog(null,"User1 caught"+e.getMessage());
}
catch(Exception e1){
JOptionPane.showMessageDialog(null,"User2 caught"+e1.getMessage()+"\nError !");
}
}
}
Hi!!
AbstractTableModel has been used in my Jtable. Mymodel is the object of MyTableModel.When I use ‘Findbtn’ for query from oracle database . data is retrieved
,but at that time any new record cann’t be inserted below the existing record.Normally data can be inserted through ‘savebtn’.But I need to do it for Auto numbering.It means when I press the Enterbtn of the Keyboard it will increase 1 for the next record. I need it for IncrementID(field of a table).I have given the partial code of mine.Plz help me how can I insert data and create auto nubering with sample code or idea.
/////////////////////////////////////
class MyTableModel extends AbstractTableModel {
String[] columnNames = {"IncrementID","Designation","Department","Date"}; //&&&&&
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return NRows;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return rowData[row][col];
}
public Class getColumnClass(int c) {
if(c==0) return Integer.class; //&&&&&&&
//if(c==1) return Integer.class;
if(c==1) return String.class;
if(c==2) return String.class;
if(c==3) return String.class;
if(c==4) return String.class;
return null;
}
public boolean isCellEditable(int row, int col) {
return true;
}
public void setValueAt(Object value, int row, int col) {
if (col==0){
rowData[row][0]=value;
}
if(col==1){
rowData[row][1]=value;
// showId(value.toString(), row);
// to load unit of measure combo
}
if(col==2){
rowData[row][2]=value;
}
if(col==3 ){//col 4 contains qty
rowData[row][3]=value;
int r=jTable1.getSelectedRow(); //?? r&nr
int nr=jTable1.getRowCount()-1;
if(r==nr)
insertRow();
}
if(col==4 ){//col 4 contains qty
rowData[row][col]=value;
}
}
//////////////////////////////////
void insertRow(){
int rows=myModel.getRowCount();
NRows=NRows+1;
int i=0;
if(rows>0)
i=rows;
rowData[i][0]=null;
rowData[i][1]="";
rowData[i][2]="";
rowData[i][3]="";
myModel.fireTableDataChanged();
}
////////////////////////actionevent for save/////////////////////////////
void jButtonSave_actionPerformed(ActionEvent e) {
setEmployeeTableData();
}
public void setEmployeeTableData(){
getSwingFieldsData();
PreparedStatement prepStatement=null;
try{
int numRows = jTable1.getRowCount();
for(int i=0; i<numRows; i++){
PreparedStatement stmt = con.prepareStatement("INSERT INTO TBL_INCREMENT VALUES (?, ?, ?, ?, ?)");
stmt.setInt(1, Integer.parseInt(rowData[i][0].toString()));
stmt.setInt(2, Integer.parseInt(strEMPMID));
// stmt.setInt(1, Integer.parseInt(rowData[i][0].toString()));
stmt.setString(3, rowData[i][1].toString());
stmt.setString(4, rowData[i][2].toString());
stmt.setString(5, rowData[i][3].toString());
stmt.execute();
stmt.clearParameters();
JOptionPane.showMessageDialog(null," Saved IncrementTable data");
}
}
catch(SQLException e){
JOptionPane.showMessageDialog(null,"User1 caught"+e.getMessage());
}
catch(Exception e1){
JOptionPane.showMessageDialog(null,"User2 caught"+e1.getMessage()+"\nError !");
}
}
}