Welcome to Dream.In.Code
Become a Java Expert!

Join 149,807 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,812 people online right now. Registration is fast and FREE... Join Now!




Refresh JTable contents

 
Reply to this topicStart new topic

Refresh JTable contents, JTable not refreshing when criterea changes

gyron
14 Feb, 2007 - 02:26 AM
Post #1

D.I.C Head
**

Joined: 9 Jan, 2007
Posts: 61


My Contributions
I have a JTable on a frame, whose contents i want to change when user chooses a different item in a combo box. The function that loads the table is as follows: -
CODE

public void populateSubCategories( String strSQL){
        Vector columnNames=new Vector();
        Vector data=new Vector();
        Connection con = null;
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            con = DriverManager.getConnection("jdbc:mysql://localhost/Pricing",
                    usr, pwd);
            if(!con.isClosed()){
                Statement stmt=con.createStatement();
                ResultSet rs=stmt.executeQuery(strSQL);
                ResultSetMetaData md=rs.getMetaData();
                int columns=md.getColumnCount();
                for (int i=1;i<=columns;i++){
                    columnNames.addElement(md.getColumnName(i));
                }
                
                while(rs.next()){
                    System.out.println("records found..." + strSQL);
                    Vector row=new Vector(columns);
                    for(int i=1;i<=columns;i++){
                        row.addElement(rs.getObject(i));
                    }
                    data.addElement(row);
                }
                rs.close();
                stmt.close();
                tblSubMainCategories=new JTable(data,columnNames);
            }


        } catch(Exception e) {
            System.err.println("Exception: " + e);
        } finally {
            try {
                if(con != null)
                    con.close();
            } catch(SQLException e) {}
        }

    }


What i want is to refresh the table with code as follows: -
CODE

populateSubCategories("SELECT * FROM sub_categories WHERE Id=" + Id)


The JTable only loads on the first call to this function and then never refreshes on subsequent calls with a different criteria. How should i make it refresh?
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Refresh JTable Contents
14 Feb, 2007 - 12:09 PM
Post #2

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
The Swing Tutorial > How to Use Tables > Detecting Data Changes
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 08:01AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month