Hi!!
I am in a cofusion for a long to solve a problem in jtable(swing) . If it is single row not a problem ,but in case of multiple rows the problem arise.When one row is updated other records of the table updated.Another problem lies in specific cell delete.
I will be greatful to you if you help me in refining the provided code or by suggestions,references.
/////////////////////////
package starpharma;
import com.borland.jbcl.layout.*;
import java.sql.*;
import javax.swing.*;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;
import javax.swing.DefaultCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.JScrollPane;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
/**
* Title:
* Description:
* Copyright: Copyright (c) 2002
* Company: Star Pharma
* @author shaiful;
* @version 1.0
*/
public class RMREntry extends JInternalFrame {
String st[]= new String[]{"Keep below 30 c","Cool place","Deep freeze","Middle Temp.","4 Deg c"};
//String st2[]={};
XYLayout xYLayout1 = new XYLayout();
JLabel lblRawMaterialsID = new JLabel();
JLabel lblRawmaterialsName = new JLabel();
JLabel lblCategory = new JLabel();
JLabel lblStorageType = new JLabel();
JLabel lblRequiredAnalysis = new JLabel();
JLabel lblNote = new JLabel();
JTextField txtRawMaterialsID = new JTextField();
JTextField txtRawMaterialsName = new JTextField();
JTextField txtCategory = new JTextField();
JTextField txtStorageType = new JTextField();
JComboBox cmbCategory = new JComboBox();
JComboBox cmbStorageType = new JComboBox(st);
JTextArea txaNote = new JTextArea();
JButton btnFind = new JButton();
JButton btnAuto = new JButton();
JButton btnDelete = new JButton();
JButton btnSave = new JButton();
JButton btnUpdate = new JButton();
JButton btnCancel = new JButton();
JButton btnShow = new JButton();
JButton btnClose = new JButton();
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
String str = "";
/**
* Code For JTable
*
*/
MyTableModel myModel;
//String anaIDs[]=new String [9999];
int NRows;
Object[][] rowData;
// Connection connection =null;
ResultSet rset = null;
String strRMID="";
String strRMName="";
String strCategory="";
String strStorageType="";
String strNote="";
String sqlID = " ";
JPanel pnl = new JPanel();
XYLayout xYLayout2 = new XYLayout();
JScrollPane spn = new JScrollPane();
JTable tblRequiredAnalysis = new JTable();
public RMREntry() {
super("RMREntry Form",true,true,true,true);
NRows=6;
rowData = new Object[9999][8];
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
setSize(700,450);
initTable();
loadAnalysisCombo();
//conn();
}
/*public static void main(String[] args) {
RMREntry RMREntry = new RMREntry();
RMREntry.setVisible(true);
RMREntry.setSize(720,390);
}*/
private void jbInit() throws Exception {
lblRawMaterialsID.setFont(new java.awt.Font("Dialog", 1, 12));
lblRawMaterialsID.setForeground(SystemColor.activeCaption);
lblRawMaterialsID.setText("Raw Material\'s ID :");
this.getContentPane().setLayout(xYLayout1);
pnl.setLayout(xYLayout2);
pnl.setLayout(xYLayout2);
txtRawMaterialsID.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(KeyEvent e) {
// txtRawMaterialsID_keyPressed(e);
}
});
/* txtStorageType.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
txtStorageType_actionPerformed(e);
}
});*/
txtCategory.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
txtCategory_actionPerformed(e);
}
});
txtStorageType.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
txtStorageType_actionPerformed(e);
}
});
this.getContentPane().add(pnl, new XYConstraints(180, 146, 492, 67));
pnl.add(spn, new XYConstraints(4, 0, 456, 64));
spn.getViewport().add(tblRequiredAnalysis, null);
lblRawmaterialsName.setFont(new java.awt.Font("Dialog", 1, 12));
lblRawmaterialsName.setForeground(SystemColor.activeCaption);
lblRawmaterialsName.setText("Raw Material\'s Name :");
lblCategory.setFont(new java.awt.Font("Dialog", 1, 12));
lblCategory.setForeground(SystemColor.activeCaption);
lblCategory.setText("Category :");
lblStorageType.setFont(new java.awt.Font("Dialog", 1, 12));
lblStorageType.setForeground(SystemColor.activeCaption);
lblStorageType.setText("Storage Type :");
lblRequiredAnalysis.setFont(new java.awt.Font("Dialog", 1, 12));
lblRequiredAnalysis.setForeground(SystemColor.activeCaption);
lblRequiredAnalysis.setText("Required Analysis :");
lblNote.setFont(new java.awt.Font("Dialog", 1, 12));
lblNote.setForeground(SystemColor.activeCaption);
lblNote.setText("Note :");
btnFind.setBackground(SystemColor.activeCaption);
btnFind.setFont(new java.awt.Font("Dialog", 1, 12));
btnFind.setForeground(Color.white);
btnFind.setText("Find");
btnAuto.setMnemonic(KeyEvent.VK_A);
btnCancel.setMnemonic(KeyEvent.VK_L);
btnClose.setMnemonic(KeyEvent.VK_C);
btnDelete.setMnemonic(KeyEvent.VK_D);
btnFind.setMnemonic(KeyEvent.VK_F);
btnSave.setMnemonic(KeyEvent.VK_V);
btnShow.setMnemonic(KeyEvent.VK_S);
btnUpdate.setMnemonic(KeyEvent.VK_U);
btnFind.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
btnFind_actionPerformed(e);
}
});
btnAuto.setBackground(SystemColor.activeCaption);
btnAuto.setFont(new java.awt.Font("Dialog", 1, 12));
btnAuto.setForeground(Color.white);
btnAuto.setText("Auto");
btnAuto.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
btnAuto_actionPerformed(e);
}
});
btnDelete.setBackground(SystemColor.activeCaption);
btnDelete.setFont(new java.awt.Font("Dialog", 1, 12));
btnDelete.setForeground(Color.white);
btnDelete.setText("Delete");
btnDelete.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
btnDelete_actionPerformed(e);
}
});
xYLayout1.setWidth(714);
xYLayout1.setHeight(399);
btnSave.setBackground(SystemColor.activeCaption);
btnSave.setFont(new java.awt.Font("Dialog", 1, 12));
btnSave.setForeground(Color.white);
btnSave.setText("Save");
btnSave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
btnSave_actionPerformed(e);
}
});
btnUpdate.setBackground(SystemColor.activeCaption);
btnUpdate.setFont(new java.awt.Font("Dialog", 1, 12));
btnUpdate.setForeground(Color.white);
btnUpdate.setText("Update");
btnUpdate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
btnUpdate_actionPerformed(e);
}
});
btnCancel.setBackground(SystemColor.activeCaption);
btnCancel.setFont(new java.awt.Font("Dialog", 1, 12));
btnCancel.setForeground(Color.white);
btnCancel.setText("Cancel");
btnCancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
btnCancel_actionPerformed(e);
}
});
btnShow.setBackground(SystemColor.activeCaption);
btnShow.setFont(new java.awt.Font("Dialog", 1, 12));
btnShow.setForeground(Color.white);
btnShow.setText("Show");
btnShow.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
btnShow_actionPerformed(e);
}
});
btnClose.setBackground(SystemColor.activeCaption);
btnClose.setFont(new java.awt.Font("Dialog", 1, 12));
btnClose.setForeground(Color.white);
btnClose.setText("Close");
btnClose.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
btnClose_actionPerformed(e);
}
});
this.getContentPane().add(lblRawmaterialsName, new XYConstraints(11, 42, -1, -1));
this.getContentPane().add(lblCategory, new XYConstraints(11, 68, -1, -1));
this.getContentPane().add(lblStorageType, new XYConstraints(11, 94, -1, -1));
this.getContentPane().add(lblRequiredAnalysis, new XYConstraints(11, 125, -1, -1));
this.getContentPane().add(lblNote, new XYConstraints(11, 240, -1, -1));
this.getContentPane().add(txtRawMaterialsID, new XYConstraints(153, 14, 83, -1));
this.getContentPane().add(txtCategory, new XYConstraints(153, 66, 147, -1));
this.getContentPane().add(txtRawMaterialsName, new XYConstraints(153, 40, 548, -1));
this.getContentPane().add(txtStorageType, new XYConstraints(153, 92, 147, -1));
this.getContentPane().add(btnFind, new XYConstraints(243, 14, -1, 21));
this.getContentPane().add(btnAuto, new XYConstraints(304, 14, -1, 21));
this.getContentPane().add(btnDelete, new XYConstraints(369, 14, -1, 21));
this.getContentPane().add(cmbCategory, new XYConstraints(305, 66, 396, 21));
this.getContentPane().add(cmbStorageType, new XYConstraints(305, 92, 396, 21));
this.getContentPane().add(txaNote, new XYConstraints(154, 240, 546, 72));
this.getContentPane().add(btnClose, new XYConstraints(633, 328, -1, 21));
this.getContentPane().add(btnShow, new XYConstraints(554, 328, -1, 21));
this.getContentPane().add(btnCancel, new XYConstraints(469, 328, -1, 21));
this.getContentPane().add(btnUpdate, new XYConstraints(382, 328, -1, 21));
this.getContentPane().add(btnSave, new XYConstraints(307, 328, -1, 21));
this.getContentPane().add(lblRawMaterialsID, new XYConstraints(11, 18, -1, -1));
this.getContentPane().add(pnl, new XYConstraints(155, 124, 549, 101));
connection();
addComboItemCat();
addComboItemSto();
}
// here is to add comboitem
public void addComboItemCat()
{
cmbCategory.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
cmbCategory_actionPerformed(e);
}
});
try{
Statement statement = con.createStatement();
String query = "select Catagory from TBL_FGINFO";
ResultSet rs = statement.executeQuery(query);
System.out.println("as ");
while(rs.next()){
cmbCategory.addItem(rs.getString(1));
System.out.println("asd ");
}
cmbCategory.setMaximumRowCount(3);
cmbCategory.setEditable(false);
}catch(SQLException ex){
System.out.println("User Caught Error: "+ex);
}
cmbCategory.addItemListener(
new ItemListener(){
public void itemStateChanged(ItemEvent e){
String string = (String)cmbCategory.getSelectedItem();
txtCategory.setText(string);
}
});
}
// here is to try
public void addComboItemSto()
{
cmbStorageType.setMaximumRowCount(3);
cmbStorageType.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
cmbStorageType_actionPerformed(e);
}
});
cmbStorageType.addItemListener(
new ItemListener(){
public void itemStateChanged(ItemEvent e){
String string = (String)cmbStorageType.getSelectedItem();
txtStorageType.setText(string);
}
});
}
//Method for Connection
public void connection(){
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
//Class.forName("oracle.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc
racle:thin:@hirasql:1521:gcep","scott","tiger");
// DriverManager.getConnection("jdbc
racle:thin:@Dc1:1521:gcep","scott","tiger");
// con=DriverManager.getConnection("jdbc
dbc:gsestudent","anonymous","guest");
}catch(ClassNotFoundException ex){
JOptionPane.showMessageDialog(null,"Error :"+ex);
}catch(SQLException es){
JOptionPane.showMessageDialog(null,"Error while connecting to the database:"+es);
}
}
//find Action perform
void btnFind_actionPerformed(ActionEvent e) {
/*try{
Statement statement = con.createStatement();
rs=null;
rs = statement.executeQuery("SELECT * from tbl_rmiinfo_h where rmid ="+Integer.parseInt(txtRawMaterialsID.getText()));
if (rs.next()){
txtRawMaterialsName.setText(rs.getString(2));
txtCategory.setText(rs.getString(3));
txtStorageType.setText(rs.getString(4));
txaNote.setText(rs.getString(5));
JOptionPane.showMessageDialog(null,"find success ");
}
}
catch(SQLException ex){
JOptionPane.showMessageDialog(null,"Error : " + ex);
} */
getTableData();
}
//Action even of Auto Button
void btnAuto_actionPerformed(ActionEvent e) {
try{
Statement statement = con.createStatement();
String query = "select max(RMID) from tbl_rminfo_h";
ResultSet rs=statement.executeQuery(query);
int maxid=0;
if (rs.next()){
maxid =Integer.parseInt(rs.getString(1))+1;
}
txtRawMaterialsID.setText(""+maxid );
}
catch(SQLException ex){
JOptionPane.showMessageDialog(null,"Error : " + ex);
}
}
// Action event of delete Button
void btnDelete_actionPerformed(ActionEvent e) {
try{
Statement statement = con.createStatement();
String query = "DELETE from tbl_rminfo_h " + "where rmid = " + txtRawMaterialsID.getText()+ " ";
statement.executeQuery(query);
}
catch(SQLException ex){
JOptionPane.showMessageDialog(null,"Error : " + ex);
}
deleteTableData();
}
public void deleteTableData(){
try{
Statement statement = con.createStatement();
String query = "DELETE from tbl_rminfo_d " + "where rmid = " + txtRawMaterialsID.getText()+ " ";
statement.executeQuery(query);
}
catch(SQLException ex){
JOptionPane.showMessageDialog(null,"Error : " + ex);
}
JOptionPane.showMessageDialog(null,"Header and Detail Table Data successfully deleted ");
}//End of deleteTableData
void btnSave_actionPerformed(ActionEvent e) {
/*try{
Statement statement = con.createStatement();
String query = "insert into tbl_rmiinfo_h (" + "rmid, rmname, CATAGORY, storage, NOTE" +") VALUES (' " + txtRawMaterialsID.getText()+" ',' " + txtRawMaterialsName.getText() +" ',' " + txtStorageType.getText() +" ',' " + txtCategory.getText() +" ',' " + txaNote.getText() + " ')";
statement.executeUpdate(query);
}
catch(SQLException ex){
JOptionPane.showMessageDialog(null,"Error : " + ex);
}*/
setTableData();
}
//////////////////////// start of setTableData /////////////////////
public void setTableData(){
System.out.println("Prepstatement 100a");
getSwingFieldsData();
PreparedStatement prepStatement=null;
try{
prepStatement=con.prepareStatement("insert into tbl_rminfo_h values(?,?,?,?,?)");
System.out.println("Prepstatement 100");
prepStatement.setInt(1,Integer.parseInt(strRMID));
//System.out.println("Prepstatement 100a" +rs.getString(1));
prepStatement.setString(2,strRMName);
//System.out.println("Prepstatement 100b" +rs.getString(2));
prepStatement.setString(3,strCategory);
System.out.println("Prepstatement 101@");
prepStatement.setString(4,strStorageType);
prepStatement.setString(5,strNote);
System.out.println("Prepstatement 101");
prepStatement.execute();
System.out.println("Prepstatement 102");
prepStatement.clearParameters();
System.out.println("Prepstatement 103");
prepStatement.close();
System.out.println("Prepstatement 104");
JOptionPane.showMessageDialog(null,"Successfully Saved HeaderTable data");
/////////////////////Detail Table///////////////////////////
int numRows=tblRequiredAnalysis.getRowCount();
for(int i=0; i<numRows; i++){
PreparedStatement stmt = con.prepareStatement("INSERT INTO tbl_rminfo_d VALUES (?, ?, ?, ?)");
stmt.setInt(1, Integer.parseInt(strRMID));
if (Integer.parseInt(rowData[i][0].toString())>0)
stmt.setInt(2, Integer.parseInt(rowData[i][0].toString()));
//stmt.setInt(2, Integer.parseInt(rowData[i][1].toString()));
else break;
//donot store ananame
System.out.println("Prepstatement 1");
//stmt.setString(3, rowData[i][3].toString());
stmt.setString(3, rowData[i][2].toString());
System.out.println("Prepstatement 2");
//stmt.setString(4, rowData[i][4].toString());
stmt.setString(4, rowData[i][3].toString());
System.out.println("Prepstatement 2.1x");
stmt.execute();
System.out.println("Prepstatement 3");
stmt.clearParameters();
System.out.println("Prepstatement 4");
JOptionPane.showMessageDialog(null," Saved DetailTable data");
}
}
catch(SQLException e){
JOptionPane.showMessageDialog(null,"User caught"+e.getMessage());
}
catch(Exception e){
JOptionPane.showMessageDialog(null,"User caught"+e.getMessage()+"\nError !");
}
}
////////////////////// end of setTabledata //////////////////////
// Action event of Update button
void btnUpdate_actionPerformed(ActionEvent e) {
/*try{
Statement statement = con.createStatement();
String query = " UPDATE tbl_rmiinfo_h SET " + " rmname = ' " + txtRawMaterialsName.getText() + " ' where CATAGORY = " + txtCategory.getText()+ " ' where storage = " + txtStorageType.getText()+ " ' where NOTE = " + txaNote.getText()");
statement.executeQuery(query);
statement.close();
}
catch(SQLException ex){
JOptionPane.showMessageDialog(null,"Error : " + ex);
}*/
updateTableData();
}
/////////start of updateTableData//////////////
public void updateTableData(){
getSwingFieldsData();
PreparedStatement prepStatement=null;
PreparedStatement prepStatement2=null;
try{
prepStatement=con.prepareStatement("UPDATE tbl_rminfo_h SET rmname=?, category=?, storage=?, note=? where rmid=?");
prepStatement.setString(1,strRMName);
prepStatement.setString(2,strCategory);
prepStatement.setString(3,strStorageType);
prepStatement.setString(4,strNote);
prepStatement.setInt(5,Integer.parseInt(strRMID));
prepStatement.execute();
prepStatement.clearParameters();
prepStatement.close();
JOptionPane.showMessageDialog(null," Updated HeaderTable data");
/////////////////////// for jtableUpdate////////////////////
int numRows = tblRequiredAnalysis.getRowCount();
for(int i=0; i<numRows; i++){
// PreparedStatement stmt = con.prepareStatement("UPDATE tbl_rminfo_d SET rmid =?, anaid =?, expectedresult =?, comments =?");
PreparedStatement stmt = con.prepareStatement("UPDATE tbl_rminfo_d SET anaid=?, expectedresult=?, comments=? where rmid=?");
stmt.setInt(4, Integer.parseInt(strRMID));
// stmt.setInt(4, Integer.parseInt(rowData[i][0].toString()));//it is also ok
// stmt.setInt(1, Integer.parseInt(rowData[i][1].toString()));// it is also ok
if (Integer.parseInt(rowData[i][0].toString())>0)
stmt.setInt(1, Integer.parseInt(rowData[i][0].toString()));
//stmt.setInt(2, Integer.parseInt(rowData[i][1].toString()));
else break;
stmt.setString(2, rowData[i][2].toString());
stmt.setString(3, rowData[i][3].toString());
// stmt.setString(3, rowData[i][3].toString());
// stmt.setString(4, rowData[i][3].toString());
stmt.execute();
stmt.clearParameters();
stmt.close();
}
JOptionPane.showMessageDialog(null," Updated DetailTable data");
}
catch(SQLException e){
JOptionPane.showMessageDialog(null,"User caught"+e.getMessage());
}
catch(Exception e){
JOptionPane.showMessageDialog(null,"User caught"+e.getMessage()+"\nError !");
}
}
/////// end of update table data//////////////
//Action event of Cancel Button
void btnCancel_actionPerformed(ActionEvent e) {
/*txtRawMaterialsID.setText(" ");
txtRawMaterialsName.setText(" ");
txtStorageType.setText(" ");
txtCategory.setText(" ");
txaNote.setText(" ");*/
clearSwingFields();
}//JOptionPane.showMessageDialog(null," Updated DetailTable data"+stmt);
//Action event of Show Button
void btnShow_actionPerformed(ActionEvent e) {
try{
stmt = con.createStatement();
rs=stmt.executeQuery("select * from tbl_rminfo_h");
String str1="";
while(rs.next()){
str1 = str1 + "rmid : "+rs.getString(1)+" rmname : "+ rs.getString(2)+" CATEGORY : "+ rs.getString(3)+" storage : "+ rs.getString(4)+" NOTE : "+ rs.getString(5)+"\n";
}
JOptionPane.showMessageDialog(null,str1);
showTableData();
}
catch(Exception es){
JOptionPane.showMessageDialog(null,"Error while class not found:"+es);
}
}
/////////////// start of showTableData /////////////////////////////////////
public void showTableData(){
try{
stmt = con.createStatement();
rs=stmt.executeQuery("select * from tbl_rminfo_d");
String str2="";
while(rs.next()){
str2 = str2 + "RmID : "+rs.getString(1) + " AnaID : " + rs.getString(2)+ " Expected Result : " + rs.getString(3) + " Comments : " + rs.getString(4)+ "\n";
}
JOptionPane.showMessageDialog(null, str2);
}
catch(Exception es){
JOptionPane.showMessageDialog(null,"Error while class not found:"+ es);
}
}//end of showTableData
///////////////// End of showTableData ///////////////////////////////////
//Action event of Close Button
void btnClose_actionPerformed(ActionEvent e) {
dispose();
}
public void getTableData(){
boolean isDataFound = false;
Statement stmt = null;
ResultSet rs = null;
int iRMID = Integer.parseInt(txtRawMaterialsID.getText().trim());
try{
//"SELECT * from tbl_rmiinfo_h where rmid ="+Integer.parseInt(txtRawMaterialsID.getText()
String strSQL = "Select * from tbl_RMINFO_h where rmid = " + iRMID ;
stmt = con.createStatement();
rs = stmt.executeQuery(strSQL);
System.out.println("Line 4" + strSQL);
if (rs.next()){
/* txtRawMaterialsName.setText(rs.getString(2));
txtCategory.setText(rs.getString(3));
txtStorageType.setText(rs.getString(4));
txaNote.setText(rs.getString(5));
System.out.println("Line 6" + rs.getString(5));
}*/
isDataFound = true;
txtRawMaterialsID.setText("" + rs.getInt(1));
System.out.println("RM NAME" + rs.getString(2));
txtRawMaterialsName.setText(rs.getString(2));
txtCategory.setText(rs.getString(3));
txtStorageType.setText(rs.getString(4));
txaNote.setText(rs.getString(5));
System.out.println("Line 6" + rs.getString(5));
//JOptionPane.showMessageDialog(this,"Data Found");
}
else{
System.out.println("Data Not Found");
JOptionPane.showMessageDialog(this,"Data Not Found\n Try With Another One");
isDataFound = false;
}
}
catch(SQLException e){
System.out.print("User caught 1 " + e);
}
catch(Exception e){
System.out.print("User caught 2 " + e);
}
////////// find for DetailTable ///////////////////////
Statement stmt2 = null;
ResultSet rs2 = null;
try{
// String strSQLforTable = "Select * from tbl_RMINFO_d where rmid = " + iRMID ;
//String strSQLforTable = "select * from tbl_rminfo_d where tbl_rminfo_d.rmid="+iRMID+" and tbl_rminfo_d.anaid = tbl_analysis.anaid";
String strSQLforTable = "select tbl_rminfo_d.anaid, tbl_analysis.ananame,tbl_rminfo_d.expectedresult,tbl_rminfo_d.comments from tbl_analysis ,tbl_rminfo_d where tbl_rminfo_d.rmid=" +iRMID+ " and tbl_rminfo_d.anaid = tbl_analysis.anaid";
stmt2 = con.createStatement();
rs2 = stmt2.executeQuery(strSQLforTable);
int i = 0;
NRows = 0;////
while(rs2.next()){
if(!isDataFound) break;
// System.out.println("Line 12 NRows = " + NRows + " id = " + rs2.getInt(1));
rowData[NRows][0] = new Integer(rs2.getInt(1));
//System.out.println("Line 13" + rs.getString(5));
//rowData[NRows][1] = rs2.getString(2);
//rowData[NRows][1] =new Integer(rs2.getInt(2));
//ystem.out.println("Line 14" + rs.getString(2));
rowData[NRows][1] = rs2.getString(2);
System.out.println("Line 15" + rs.getString(3));
rowData[NRows][2] = rs2.getString(3);
rowData[NRows][3] = rs2.getString(4);
i++;
myModel.fireTableDataChanged();
NRows++;
}
}
catch(SQLException e){
System.out.print("User caught 2" + e);
}
catch(Exception e){
System.out.print("User caught 3" + e);
}
/////////////////////////////////////////////////
System.out.println("Line 12 NRows = " + NRows);
if(!isDataFound) {
clearSwingFields();
}
}
private void getSwingFieldsData(){
strRMID = txtRawMaterialsID.getText();
strRMName = txtRawMaterialsName.getText();
strCategory = txtCategory.getText();
strStorageType = txtStorageType.getText();
strNote = txaNote.getText();
}
private void clearSwingFields(){
txtRawMaterialsID.setText("");
txtRawMaterialsName.setText("");
txtCategory.setText("");
txtStorageType.setText("");
txaNote.setText("");
NRows = 1;
System.out.println("Line 1xx NRows = " + NRows );
rowData[0][0] = new Integer(0);
rowData[0][1] = "";
rowData[0][2] = "";
rowData[0][3] = "";
myModel.fireTableDataChanged();
}
/* void showId(String value,int row){
//System.out.println("Place 1");
String sql="SELECT anaid,anaName FROM tbl_analysis WHERE (anaName='"+value.toString()+"')";
try{
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
if (rs.next()){
rowData[row][0]=new Integer(rs.getInt(1));
rowData[row][2]="";
rowData[row][3]="";
myModel.fireTableCellUpdated(row,0);
}
rs.close();
stmt.close();
}
catch(SQLException sqlex){
sqlex.printStackTrace();
System.out.print(sqlex.getMessage()); // ??
}
}*/
public void initTable() {
myModel = new MyTableModel();
tblRequiredAnalysis.setModel(myModel);
//set the preferred column width
TableColumn column = null;
for (int i = 0; i < 4; i++) {
column = tblRequiredAnalysis.getColumnModel().getColumn(i);
switch (i){
case 0: column.setPreferredWidth(80); break;
case 1: column.setPreferredWidth(100); break;
case 2: column.setPreferredWidth(150); break;
case 3: column.setPreferredWidth(100); break;
case 4: column.setPreferredWidth(100); break;
default: column.setPreferredWidth(120); break;
}
}
}
/////////////////////// Start of loadAnalysisCombo ////////////////////////////
public void loadAnalysisCombo(){
JComboBox cmb= new JComboBox();
cmb.setEditable(true);
TableColumn idColumn = tblRequiredAnalysis.getColumnModel().getColumn(0);
idColumn.setCellEditor(new DefaultCellEditor(cmb));
// DefaultTableCellRenderer idCellRenderer = new DefaultTableCellRenderer();
// idColumn.setCellRenderer(idCellRenderer);
try{
Statement statement = con.createStatement();
// String sqlID = "SELECT VENID FROM TBL_VENDOR";
// String sqlID = "SELECT DISTID FROM TBL_DISTRIBUTOR";
String sqlID = "SELECT anaid,ananame FROM tbl_analysis";
// String sqlID = "SELECT fgid FROM tbl_fginfo";
rs= statement.executeQuery(sqlID);
// int i=0;
while (rs.next()){
// Int sqlId = rs.getInt(1);
// sqlId = rs.getString(1);
//String Ids[i]=rs.getString(1);
//cmb.getItemAt(i+1);
//cmb.addItem(rs.getString(2));
//i++;
////////////////////////////////
cmb.addItem(rs.getString(1));
}
}
catch(SQLException ex)
{
;
}
catch(Exception e)
{
;
}
}
//////////////////////// End of loadAnalysisCombo ///////////////////////////////////////
class MyTableModel extends AbstractTableModel {
String[] columnNames = {"Analysis ID","Analysis Name","Expected Result","Comments"}; //&&&&&
//String[] columnNames = {"RM ID","Ana ID", "Expected Result","Comments"}; //&&&&&
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return 2;
}
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;
return null;
}
public boolean isCellEditable(int row, int col) {
return true;
}
public void setValueAt(Object value, int row, int col) {
if (col==0){
if(!value.toString().equalsIgnoreCase("")){
String sql="SELECT anaid, anaName FROM tbl_analysis WHERE anaId="+Integer.parseInt(value.toString());
// String sql="SELECT * from FROM tbl_rminfo_d WHERE anaId="+Integer.parseInt(value.toString());
// String sql= "INSERT INTO tbl_rminfo_d VALUES"
try{
Statement stmt =con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
if(rs.next()){
rowData[row][0]=new Integer(rs.getInt(1));
// rowData[row][1]=new Integer(rs.getInt(2));
rowData[row][1]=new String(rs.getString(2));
// rowData[row][2]= new String(rs.getString(3)); //"";
// rowData[row][3]= new String(rs.getString(4)); //"";
// myModel.fireTableDataChanged();
}else{
JOptionPane.showMessageDialog(null,"Sorry, Invalid Analysis Id is entered");
}
rs.close();
stmt.close();
}
catch(SQLException sqlex){
sqlex.printStackTrace();
System.out.println( "" + sqlex);
}
}
rowData[row][0]=value;
System.out.println("Can not execute the query");
}
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][col]=value;
//rowData[row][3]=value;
//int r=tblRequiredAnalysis.getSelectedRow();
// int nr=tblRequiredAnalysis.getRowCount()-1;
}
}
}
void cmbStorageType_actionPerformed(ActionEvent e) {
}
void txtCategory_actionPerformed(ActionEvent e) {
}
void txtStorageType_actionPerformed(ActionEvent e) {
}
void cmbCategory_actionPerformed(ActionEvent e) {
}
}