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(); 
                                            }
                                            }
                                            }









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
adding data to the database with the use of jtable - Java Beginners
adding data to the database with the use of jtable  how can i add data to the database with the use of jtable. and also can able to view the records in the database in the table.. tnx :G
adding data to the database with the use of jtable - Java Beginners
adding data to the database with the use of jtable  how can i add data to the database with the use of jtable. and also can able to view the records in the database in the table.. tnx :G
how to refresh my GUI page
how to refresh my GUI page  how to refresh a GUI in java
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
How to add JTable in JPanel
How to add JTable in JPanel  How to add JTable in JPanel
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 insert row swing
jtable insert row swing  How to insert and refresh row in JTable?   Inserting Rows in a JTable example
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 edit a record in hibernate?
how to edit a record in hibernate?  how to edit a record in hibernate?   Hi Friend, Please visit the following link:ADS_TO_REPLACE_1 Hibernate Tutorials Thanks   Hi Friend,ADS_TO_REPLACE_2 Please visit
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 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 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 record qvga video using j2me - Applet
how to record qvga video using j2me  hello if it is possible how i can record qvga video usin j2me
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
JTable
JTable  Values to be displayed in JTextfield when Clicked on JTable Cells
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.
deleting file
deleting file   how to delete a file from a folder and its corresponding path in database using a servlet
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  need to add values to a JTable having 4 coloumns ,2 of them are comboboxes
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... to set value to a particular column. How can i do it? Please someone help me
Servlet refresh page
Servlet refresh page In this example we will show you how you can refresh your page from setting the refresh attribute from a servlet. response.setHeader("Refresh", "10"); The refresh attribute will direct
JTable
JTable  Hello, i cannot display data from my table in the database to the cells of my JTable. please help me
Refresh JFrame
Refresh JFrame  How to refersh a JFrame and its componets
how to fetch the record using AJAX? - Ajax
how to fetch the record using AJAX?  Can anyone tell me how to fetch the records from database using Ajax
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
jtable
jtable  hey i have build a form and i m also able to add data from database to jtable along with checkbox.the only problem is that if i select multiple checkboxes the data doesnt get inserted into new database and if only one
how to record qvga video using j2me - Applet
how to record qvga video using j2me  hello if it is possible how i can record qvga video usin j2me?   Hi Friend, Please visit the following links: http://www.roseindia.net/j2me/video-midlet.shtml http
refresh checkbox jframe
refresh checkbox jframe  How to refresh checkbox button in JFrame
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 table=new JTable(data,labels); JScrollPane pane=new JScrollPane
UIBarButtonItem Refresh
, refresh button using code in iPhone application?? If yes then how?   ... the refresh button... i'm providing you the example of Navigation Refresh UIBarbuttonItem...] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refresh
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
How To Page Refresh Using JavaScript In JSP
How To Page Refresh Using JavaScript In JSP       In this section, we develop...: Output when Successfully Submit the User Login refresh the parent window and show
how to read text file in jtable in netbeans7.0
how to read text file in jtable in netbeans7.0  text file... want to displaythe above .txt file in jtable as following format having 3... area which is design below table in netbeans IDE. how to do that ? pls help me
Part refresh without a page refresh
Part refresh without a page refresh  hello, How do I submit a form or a part of a form without a page refresh?   hii,ADS_TO_REPLACE_1 Ajax can do this. You can do partial update and upon a successful AJAX
How to delete and update from Jtable cell in swing app
How to delete and update from Jtable cell in swing app  Hii Sir, I am developing a swing app for adding,updating and deleting from jtable... is getting removed from the jtable but selected row is getting deleted from
coding for deleting file
coding for deleting file  coding for deleting file
how to write a query for adding records in database
how to write a query for adding records in database  How write fire query in JSP for adding records in database

Ads