How to refresh a jTable On adding or deleting record ....

How to refresh a jTable On adding or deleting record ....

Hii Sir, I am developing a project in which a jtable is getting populated from database and displayed in panel... Now i have to add records in the database from same panel and i want jtable to be refreshed after every addition of records.... and new record should be showed in the jtable in the panel..

Plz sir give me the code ........ Thank you Sir....

View Answers

July 27, 2012 at 5:03 PM

Here is a java swing code that updates the jtable automatically after some interval of time.

import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*; 

public class UpdateJTable extends TimerTask {
    private static JTable table = new JTable(); 
    private static Vector columnNames = new Vector();
    private static Vector data = new Vector();
    private static JFrame f = new JFrame();
    private static JScrollPane scrollPane = new JScrollPane();
    public static void main(String[] args) {
        java.util.Timer timer = new java.util.Timer();
        timer.schedule(new UpdateJTable(), 0, 10000);
        } public void run() {
            remove();
            table = draw(); 
            scrollPane.getViewport().add(table, 0); 
            f.getContentPane().add(scrollPane);
            f.setVisible(true);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
            f.pack(); 
            } public static JTable draw() {
                try { Class.forName("com.mysql.jdbc.Driver").newInstance(); 
                Connection con = DriverManager.getConnection( "jdbc:mysql://localhost/test", "root", "root");
                String sql = "Select * from employee"; 
                Statement stmt = con.createStatement(); 
                ResultSet rs = stmt.executeQuery(sql); 
                ResultSetMetaData md = rs.getMetaData(); 
                int columns = md.getColumnCount(); 
                for (int i = 1; i <= columns; i++) {
                    columnNames.addElement(md.getColumnName(i)); }
                    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();
                            } 
                            catch (Exception e) {
                                System.out.println(e); 
                                e.printStackTrace(); 
                                } 
                                table.setModel(new DefaultTableModel(data, columnNames));
                                TableColumn col;
                                table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
                                for (int i = 0; i < table.getColumnCount(); i++) {
                                    col = table.getColumnModel().getColumn(i);
                                    col.setMaxWidth(col.getMaxWidth());
                                    } return table;
                                    }
                                    public static void remove() {
                                        columnNames.clear();
                                        if (table.getRowCount() != 0) {
                                            DefaultTableModel dm = (DefaultTableModel) table.getModel();
                                            dm.getDataVector().removeAllElements(); 
                                            }
                                            }
                                            }

Ads









Related Tutorials/Questions & Answers:
How to refresh a jTable On adding or deleting record ....
How to refresh a jTable On adding or deleting record ....   Hii Sir, I am developing a project in which a jtable is getting populated... of records.... and new record should be showed in the jtable in the panel.. Plz sir give
how update JTable after adding a row into database
how update JTable after adding a row into database  J have two... in JTable, and it's OK, but after adding a row into database table does't update. How update JTable after adding a row into database? package djile pak.java
Advertisements
Java: Adding Row in JTable
Java: Adding Row in JTable   how about if we already have the JTAble created earlier. And i just found nothing to get its DefaultTableModel, thus, I can't call insertRow() method. Is there any work around for this? I found
jtable-adding a row dynamically
jtable-adding a row dynamically  hi..i am doing a project for pharmacy .. *pblm:* when i want to enter the details in jtable while running the application there are 3 rows and 4 columns which is default bt when we we have
Adding JTable into existing Jframe.
Adding JTable into existing Jframe.  i need to add JTable into this code pls help me.. package Com; import Com.Details; import java.awt.Color...(100,60,1300,600); add(pane); //Action Listener for button //for adding new
Adding JTable into existing Jframe.
Adding JTable into existing Jframe.  i need to add JTable into this code pls help me.. package Com; import Com.Details; import java.awt.Color...(100,60,1300,600); add(pane); //Action Listener for button //for adding new
Adding JTable into existing Jframe.
Adding JTable into existing Jframe.  i need to add JTable into this code pls help me.. package Com; import Com.Details; import java.awt.Color...(100,60,1300,600); add(pane); //Action Listener for button //for adding new
how to refresh my GUI page
how to refresh my GUI page  how to refresh a GUI in java
How to add JTable in JPanel
How to add JTable in JPanel  How to add JTable in JPanel
JSP Delete Record From Table Using MySQL
JSP Delete Record From Table Using MySQL This tutorial explains you that how to write a JSP for deleting a record from database table. In this section you will see all the steps required for deleting a record of a table
jtable insert row swing
jtable insert row swing  How to insert and refresh row in JTable?   Inserting Rows in a JTable example
How to refresh Client only once by sending setHeader ("refresh" ,??? )
How to refresh Client only once by sending setHeader ("refresh" ,??? )  How can i send response from JSP that will make my client page to be refreshed ?? I just want to refresh my client page only once....! Not periodically
JTable
JTable  i want to delete record from JTable using a MenuItem DELETE. and values of JTable are fetched from database....please reply soon
How to insert rows in jTable?
How to insert rows in jTable?  Hi, I need to take input from user using JTable. I want an empty row to appear after clicking a insert button... not figure out how to. I used DefaultTableModel but wasnt able to insert a row
JTable
JTable   how to select a definite cell which containing a similar text containg to the one which the user entering from a jtable at runtime in java
How to retrieve record from table
How to retrieve record from table  Hi. I have a field in database...,kanchipuram for a single record. I have to retrieve these data from the field table.... how to do
how to store jtable value in multidimensional array?
how to store jtable value in multidimensional array?   i want to store the value of jtable in multidimensional array,with type double. how to store jtable value in multidimensional array
how to store jtable value in multidimensional array?
how to store jtable value in multidimensional array?   i want to store the value of jtable in multidimensional array,with type double. how to store jtable value in multidimensional array
how to create a header in jtable using java swing
how to create a header in jtable using java swing  how to create a header in jtable using java swing   d
How to use JTable with MS-Access
How to use JTable with MS-Access   I have Three Column in Database... this data in JTable. How can I do it. I also want to EDIT and DELETE this Data... and display it in JTable. import java.awt.*; import java.sql.*; import java.util.
How to use JTable with MS-Access
How to use JTable with MS-Access   I have Three Column in Database... this data in JTable. How can I do it. I also want to EDIT and DELETE this Data... and display it in JTable. import java.awt.*; import java.sql.*; import java.util.
How to prevent the page refresh while submitting the form??
How to prevent the page refresh while submitting the form??  Hi. Requirement: There are two drop down boxes.Dropdown2 should load corresponding to the select of Dropdown1. Im hitting a db for this every time.Hence the page loads
jtable
jtable  how to get the values from database into jtable and also add a checkbox into it and then when selected the checkbox it should again insert into database the selected chewckbox.plzz help
how to write a program to search a record using mvc2
how to write a program to search a record using mvc2   how can we write a code to search a record in table by using java bean as model, servlet as contoller and jsp as view
JTable
JTable  Values to be displayed in JTextfield when Clicked on JTable Cells
JTable
JTable  Hi I have problems in setting values to a cell in Jtable which is in a jFrame which implements TableModelListener which has a abstract.... And i'm not able to set value to a particular column. How can i do it? Please
How to create a JTable cell containing Image hyperlink?
How to create a JTable cell containing Image hyperlink?  I'm trying to find out how to create a JTable cell which contains Image, which should.... Can somebody explain me how to add hyperlink (mouse listener) for the each
JTable
JTable  Hello, i cannot display data from my table in the database to the cells of my JTable. please help me
deleting file
deleting file   how to delete a file from a folder and its corresponding path in database using a servlet
how to sort the text file and display it in jtable
how to sort the text file and display it in jtable   my text file is: contig00001 length=586 numreads=4... in jtable and sequence like "CGGGAATTAA..." in jtextarea in netbeans . pls suggest
jtable
jtable  hi Sir i am working netbeans IDE,I have a jtable when i insert values in jtable then i am unable to print all inserted values,For eg if i insert 1,2,3,4,5,6,7,8 values then , i am getting output

Ads