This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Unable to refresh the table data wheen I select a item from the COMBOBOX Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Unable to refresh the table data wheen I select a item from the COMBOBOX" Watch "Unable to refresh the table data wheen I select a item from the COMBOBOX" New topic
Author

Unable to refresh the table data wheen I select a item from the COMBOBOX

ritesh r
Greenhorn

Joined: Jul 19, 2001
Posts: 24
Hi techies:
I am facing a problem in the development of a GUI where I am interacting with the DataBase(SQL SERVER).
I have a combobox with values 10,20 and so on
When I select any one of them ,the SQL statement with the combao value is executed.
The problem is whenever I change my combo-value the table is not refreshed.
Here is the SOurce Code:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//Declare the Packages going to be imported here below
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;
import java.util.*;
import java.sql.*;
//<Applet code="JApplicationSql.class" width=500 height=500></Applet>
public class JApplicationSql extends JFrame implements ItemListener
{
//Initialize the Components
//DefaultTableModeldefaulttablemodel=new DefaultTableModel();
JTablejtable;//=newJTable(defaulttablemodel);//A Dynamic Modifiable JTable
Vectorrow,column,row_1,column_1;//No. of rows and columns in a JTable
JLabel label_combo=new JLabel();
JComboBox comboBox =new JComboBox();
JPanel jpanel;
//comboBox.setEditable(true);

//Declare the Container1
Container contentPane;

//Database Declarations
//Connection connection;
//String dbUrl = "jdbc dbc:mydsn";
//String user = "sa";
//String password = "";

private Statement stmt;
private ResultSet rs;
private ResultSetMetaData rmeta;
String combo_value="";
//String row_value="";


//Initialization of Constructor
public JApplicationSql()
{
jpanel=new JPanel();

label_combo.setText("Select the Department");
label_combo.setFont(new Font("Verdana",1,17));

comboBox.addItem(" 10 ");
comboBox.addItem(" 20 ");
comboBox.addItem(" 30 ");
comboBox.addItem(" 40 ");
comboBox.addItem(" 50 ");

//contentPane.setLayout(new FlowLayout());
jpanel.add(label_combo);
jpanel.add(comboBox);
//Add the Listeners
comboBox.addItemListener(this);

contentPane=getContentPane();
contentPane.add(jpanel,BorderLayout.NORTH);

//Load the Drivers and Connection Objects and Establish the connection
try
{
System.out.println("Locating Drivers");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Connecting to Database");
//connection = DriverManager.getConnection(dbUrl,user,password);
Connection connection = DriverManager.getConnection("jdbc dbc:mydsn;UID=sa;PWD=");
System.out.println("Got Connection");
stmt = connection.createStatement();
}
catch (Exception sqlex)
{
System.out.println("Unable to Connect!!");
sqlex.printStackTrace();
}

}
public static void main(String args[])
{
try
{
JApplicationSql app=new JApplicationSql();
app.setSize(500,500);
app.setVisible(true);
}catch(Exception e)
{}
}
public void itemStateChanged(ItemEvent e)
{
try
{
if (e.getStateChange()==ItemEvent.SELECTED)
{
combo_value=(String)comboBox.getSelectedItem();
populate(combo_value);
jtable.repaint();

/*combo_value=(String)comboBox.getSelectedItem();
//int index=(int)comboBox.getSelectedIndex();
System.out.println("Value of the combo is "+combo_value);
String sql_query="SELECT A.DeptId,A.EmpId,A.Name,A.Designation, B.DeptName FROM EmployeeDetails A,Master_Department B where A.DeptId = B.DeptId and A.DeptId ="+combo_value;
//The Resultset Based Sql Query
//rs = stmt.executeQuery("SELECT EmployeeDetails.EmpId,EmployeeDetails.Name,EmployeeDetails.Designation, EmployeeDetails.DeptId,Master_Department.DeptName FROM EmployeeDetails INNER JOIN Master_Department ON EmployeeDetails.DeptId = 'index'");
rs=stmt.executeQuery(sql_query);
rmeta=rs.getMetaData();

//Retrieve the number of columns and rows and add the number to Vectors
column_1= retrieveColumns();

//Row Data
row_1=retrieveRows();
System.out.println(row_1.size());
//Display the ResultSet data in a JTable
DefaultTableModel defaulttablemodel =new DefaultTableModel(row_1,column_1);
jtable =new JTable(defaulttablemodel);
JScrollPane scrolltab=new JScrollPane(jtable);
//jpanel.add(scrolltab);
contentPane.add(scrolltab,BorderLayout.CENTER);
//contentPane.add(jpanel,BorderLayout.CENTER);
//contentPane.validate(true);
jtable.repaint();*/
}
}
catch(Exception SqlE)
{
System.out.println("Error in retrieving");
}
}

//The Column Data
public Vector retrieveColumns()
{
column=new Vector();
try
{
for(int i=1;i<=rmeta.getColumnCount();i++)<br /> column.addElement(rmeta.getColumnName(i));<br /> //System.out.println(column.size());<br /> }<br /> catch(Exception e)<br /> {<br /> System.out.println("The column Exception is :"+e);<br /> }<br /> return column;<br /> }<br /> <br /> //The Row Data<br /> public Vector retrieveRows()<br /> {<br /> row=new Vector();<br /> try<br /> {<br /> while(rs.next())<br /> {<br /> Vector currow=new Vector();//Current Row<br /> for(int i=1;i<=rmeta.getColumnCount();i++)<br /> currow.addElement(rs.getString(i));<br /> System.out.println(currow.size());<br /> row.addElement(currow);<br /> }<br /> }<br /> catch(SQLException se)<br /> {<br /> System.out.println("The Exception is :"+se);<br /> }<br /> return row;<br /> }<br /> public void populate(String val)<br /> {<br /> try<br /> {<br /> System.out.println("Value of the combo is "+val);<br /> String sql_query="SELECT A.DeptId,A.EmpId,A.Name,A.Designation, B.DeptName FROM EmployeeDetails A,Master_Department B where A.DeptId = B.DeptId and A.DeptId ="+val;<br /> <br /> //The Resultset Based Sql Query<br /> //rs = stmt.executeQuery("SELECT EmployeeDetails.EmpId,EmployeeDetails.Name,EmployeeDetails.Designation, EmployeeDetails.DeptId,Master_Department.DeptName FROM EmployeeDetails INNER JOIN Master_Department ON EmployeeDetails.DeptId = 'index'");<br /> rs=stmt.executeQuery(sql_query);<br /> rmeta=rs.getMetaData();<br /> <br /> //Retrieve the number of columns and rows and add the number to Vectors<br /> column_1= retrieveColumns();<br /> <br /> //Row Data<br /> row_1=retrieveRows();<br /> <br /> //Display the ResultSet data in a JTable<br /> DefaultTableModel defaulttablemodel =new DefaultTableModel(row_1,column_1);<br /> jtable =new JTable(defaulttablemodel);<br /> JScrollPane scrolltab=new JScrollPane(jtable);<br /> //jpanel.add(scrolltab);<br /> contentPane.add(scrolltab,BorderLayout.CENTER);<br /> //contentPane.add(jpanel,BorderLayout.CENTER);<br /> //contentPane.validate(true);<br /> //jtable.repaint();<br /> }<br /> catch(Exception eop){System.out.println(eop);}<br /> }<br /> }<br /> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Kindly help me where I am going wrong.With Modifications in the SOurce Code
Thanking you.
Regards
Ritesh
Software Engineer
HYDERABAD
INDIA
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
Why are you creating a new DefaultTableModel and Jtable every time? Extend DefaultTableModel with your own and update the model.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unable to refresh the table data wheen I select a item from the COMBOBOX
 
Similar Threads
regarding JTable
jtable not refreshing
Error in Connection
JTable
Columns on JTable