A. Mesut Konuklar
A. Mesut Konuklar

Reputation: 609

JTable , update the table whenever click on a button

I am currently trying to learn JTable with MySQL. I've set my database into JTable and added it into my GUI with using scroll pane.

My question is, how can I make a function to renew my database or do a search operation (both are the same thing so..) whenever I click on my button.

Here is my code (I am reading the database informations into my .ini file)

final Vector columnNames = new Vector();
        final Vector data = new Vector();

        try
        {
            //  Connect to an Access Database



            driver = IniFonksiyon.iniSVNOkut(driver, "databaseBilgileri", "driver");//"com.mysql.jdbc.Driver";
            url = IniFonksiyon.iniOkut(url, "databaseBilgileri", "url");//"jdbc:mysql://localhost:3306/";
            userid = IniFonksiyon.iniOkut(userid, "databaseBilgileri", "kullanici");
            password = IniFonksiyon.iniOkut(password, "databaseBilgileri", "sifre");
            dbName = IniFonksiyon.iniOkut(dbName, "databaseBilgileri", "dbIsmi");

            Class.forName( driver );
            Connection connection = DriverManager.getConnection( url+dbName , userid, password );

            //  Read data from a table

            String sql = "select * from profildb.tbl_detailed";
            Statement stmt = connection.createStatement();
            ResultSet rs = stmt.executeQuery( sql );

            ResultSetMetaData md = rs.getMetaData();
            int columns = md.getColumnCount();

            //  Get column names

            for (int i = 1; i <= columns; i++)
            {
                columnNames.addElement( md.getColumnName(i) );
            }

            //  Get row data

            while (rs.next())
            {
                Vector row = new Vector(columns);

                for (int i = 1; i <= columns; i++)
                {
                    row.addElement( rs.getObject(i) );
                }

                data.addElement( row );
            }

            rs.close();
            stmt.close();
            connection.close();
        }
        catch(Exception e)
        {
            System.out.println( e );
        }

        //  Create table with database data

        final JTable table = new JTable(data, columnNames)
        {
            /**
             * 
             */
            private static final long serialVersionUID = 1L;

            public Class getColumnClass(int column)
            {
                for (int row = 0; row < getRowCount(); row++)
                {
                    Object o = getValueAt(row, column);

                    if (o != null)
                    {
                        return o.getClass();
                    }
                }

                return Object.class;
            }
        };

This is the code that handles creating the table, but its directly connected with my main GUI function, so How can I refresh/do another operation like search function?

Upvotes: 0

Views: 3699

Answers (1)

tenorsax
tenorsax

Reputation: 21243

It is easier to use a table model. See a related tutorial. You can use DefaultTableModel or your own implementation of TableModel. Extension of AbstractTableModel usually is a most common approach if DefaultTableModel does not satisfy your needs.

For better performance and responsiveness of Swing application the execution of long running tasks should not happen on EDT. So, it is best to access database from a thread other than EDT.

SwingWorker can be used for such purpose. Get data from database in SwingWorker.doInBackground(). Then, either build or update your model in SwingWorker.done().

There are many variations of such implementation. These are specific to your application's nature and requirements. For instance you can use SwingWorker's publish()/process() tandem to process data in chunks. You may choose to incorporate database access logic into your model or create commands that extend SwingWorker.

Upvotes: 2

Related Questions