aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JTable with inseting and autonumbering problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable with inseting and autonumbering problem" Watch "JTable with inseting and autonumbering problem" New topic
Author

JTable with inseting and autonumbering problem

Faisal Ahmed
Greenhorn

Joined: Aug 22, 2002
Posts: 8
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 !");
}

}
}
Bill Liteplo
Ranch Hand

Joined: Oct 16, 2002
Posts: 88
Please use UBB CODE tags for code, especially large chunks like this.
I noticed a few things in your code.
First of all, you could consolidate at lot of code by using OR statements in setValueAt
(e.g. if (col == 1 || col ==2 || col == 3) ...), and other places. That is just coding practice, and not related to your problem, but I find consise, well commented code to be less bug prone.
In insertRow() you assign values in an array (rowData). You don't show how you initialize that double array. If there is not enough room in the array, you will get an ArrayIndexOutOfBoundsException (if you're lucky). My guess is that you keep that separate variable NRows around just to solve that problem, and you initialize rowData to be "sufficiently" large. Fine.
I don't know if this will do it, but you might want to call fireTableStructureChanged instead of fireTableDataChanged. The latter is an optimized version of the former that re-gets the data, but doesn't re-get the dimensions. The former assumes everything may have changed, including the size.
You mention a lot of different things in your post. If the above comments don't help, try stating your problems more clearly. For example, if the added row doesn't show up, what happens? Is an exception thrown? Nothing?
Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable with inseting and autonumbering problem