Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Swing-AWT How to refresh a jTable On adding or deleting record ....
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


vikas pandey
How to refresh a jTable On adding or deleting record ....
1 Answer(s)      2 years and 2 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....


Advertisement
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 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 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
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 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
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 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
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
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 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
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
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
Refresh JFrame
Refresh JFrame  How to refersh a JFrame and its componets
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.
 

 

 

DMCA.com