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

Ads

 
 
 

Share on Google+Share on Google+

vikas pandey
How to refresh a jTable On adding or deleting record ....
1 Answer(s)      4 years and 9 months ago
Posted in : Swing AWT

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....

Ads
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
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 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
How to add JTable in JPanel
How to add JTable in JPanel  How to add JTable in JPanel
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: Hibernate Tutorials Thanks   Hi Friend, Please visit the following link: Hibernate
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
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 insert row swing
jtable insert row swing  How to insert and refresh row in JTable?   Inserting Rows in a JTable example
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
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 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 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
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
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   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 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
Refresh JFrame
Refresh JFrame  How to refersh a JFrame and its componets
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 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
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
Tutorials   
Java Spring Hibernate Struts Training Change JPanel background color from mouse entered/exit event JTable Pagination Jcombobox JAVA SWING how can i display a pdf file in a jtextarea Crop Image Code in Applet Crop Image Code in Applet Image Manipulation in Swing AWT Applet Ho to get virtual key code. how to show set of images continously using JScrollpane in java how to show set of images continously using JScrollpane in java How to connect on screen keyboard to system. How to connect on screen keyboard to system. query java client server program for playing video file(stored in folder in the same workspace) using swings java client server program for playing video file(stored in folder in the same workspace) using swings java client server program for playing video file(stored in folder in the same workspace) using swings java client server program for playing video file(stored in folder in the same workspace) using swings Download. how to open one Jframe from main method call Urgent requirement display shapes i am getting an error in adding a menu item to a menu. I am trying to develop a Image Sliding application but unfortunately its not working... Can any one help me in this Sliding window protocol how to update the value of jslider how to update the value of jslider how to update the value of jslider how to update the value of jslider how to update the value of jslider Jbutton in JTable cells program code java textfield problem swing retrieve data from database in java swing form using prev n next buttons retrieve data from database in java swing form using prev n next buttons How to close a frame on action event of a button from a different class ? searching the data swings for webnms Jpanel allignment pass data from java frame to file code for traffic control system simulation Bank Management System how to maKE Jcombox editable after saveing value init. i havei) aJTextField and one button. when place the cursor or enter anything in the test field then only button should be enabled.by default the button should be disabled. i havei) a jtable with few cloumns and few rows andii) one button. when place the cursor in any of the table's column cell. i need to enable the button.by default the button should be disabled. PDF Comparator i have JTextfield and JComboBox. there are several values in combobox.when i select a value from combobox how to make textfiled as a combobox. issue on jcombobox

Ads

 
Advertisement null

Ads