Results 1 to 8 of 8
Like Tree2Likes
  • 2 Post By camickr

Thread: Refresh Jtable after data has been changed

  1. #1
    kevinn205 is offline Member
    Join Date
    Nov 2011
    Posts
    65
    Rep Power
    0

    Default Refresh Jtable after data has been changed

    Java Questoin

    Hi, thank you for taking the time to read my post I have a mysql database which uses a program to add data into the database; I want to have a seperate Jtable which then retrieves the data able to either auto update, update upon the press of a button, or update in reponse to the other program adding a new record to the database. I've read as much as possible about repaint and firetablemodelchanged but i'm unsure how these work. Also I have read into abstract table models. Do anyone know how I could get my table to refresh? if not does anyone know the answer to the questions below

    A few questions I would greatly appreciate the answer to are is the default table method capable of accomplishing my goals or will I need to create a abstract method?

    Will I need to use repaint or will i need to use firetablemodelchanged?

    and how do i call either methods if I need to use them?


    Thank you for your help!!!
    My current problem that i'm trying to solve is getting my working jtable to auto update.

    what are you recommendations on how to accomplish my goals?

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.GridBagConstraints;
    import java.awt.Insets;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Comparator;
    
    import javax.swing.*;
    import javax.swing.event.TableModelEvent;
    import javax.swing.table.*;
    
    public class jtable implements ActionListener{
    	JTable table;
    	JFrame frame;
    	JPanel panel;
    	DefaultTableModel model;
    	
    	
    	
    	
      public static void main(String[] args) {
      new jtable();
      StoreData.StoreData();
      
      }
    
      public jtable(){
    	  JButton homeButton = new JButton("Home");
    	  JButton exitButton = new JButton("Exit");
    	  JButton refreshButton = new JButton("Refresh");
      
      JFrame frame = new JFrame("NEI CRM INFO");
      JPanel panel = new JPanel();
      panel.add( homeButton, getConstraints(0,0,1,1, GridBagConstraints.SOUTH));
      homeButton.addActionListener(this);
      panel.add(exitButton,getConstraints(0,0,1,1, GridBagConstraints.SOUTH));
      exitButton.addActionListener(this);
      panel.add(refreshButton,getConstraints(0,0,1,1, GridBagConstraints.SOUTH));
      refreshButton.addActionListener(this);
    
      
    
      
      String data[][] = {};
      String col[] = {"InfoID","Company","Date/Time","Reason","Note"};
    
     
      //Creates default table model
      DefaultTableModel model = new DefaultTableModel(data,col);
      JTable table = new JTable(model);
      table.setAutoCreateRowSorter(true);
    
     
      //SetsTable Size Within the Pane
      table.setPreferredScrollableViewportSize(new Dimension(910, 420));
      table.setFillsViewportHeight(true);
      
      JTableHeader header = table.getTableHeader();
      header.setBackground(Color.YELLOW);
      JScrollPane pane = new JScrollPane(table);
      pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
      pane.setHorizontalScrollBarPolicy(JScrollPane .HORIZONTAL_SCROLLBAR_ALWAYS);
     
      //Set AutoResize 
      table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
      //Set Column Widths 
      table.getColumnModel().getColumn(0).setMinWidth(60);     
      table.getColumnModel().getColumn(1).setMinWidth(190);      
      table.getColumnModel().getColumn(2).setMinWidth(160);    
      table.getColumnModel().getColumn(3).setMinWidth(100);    
      table.getColumnModel().getColumn(4).setMinWidth(400);
    
      table.setAutoCreateRowSorter(true);
      panel.add(pane);
      frame.add(panel);
      frame.setSize(1000,550);
      frame.setUndecorated(true);
      frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
      table.repaint();
      
      
     
    
      
      //Insert first position
      
      
      math.connection = math.connect();
    //code to retrieve data goes here
      
    model.insertRow(0,new Object[]{InfoID,Company,DateNTime,Reason,Note});
         	}
    	
      }
      
    	 private GridBagConstraints getConstraints(int gridx, int gridy,
    			    int gridwidth, int gridheight, int anchor)
    			    {
    			        GridBagConstraints c = new GridBagConstraints();
    			        c.insets = new Insets(5, 5, 5, 5);
    			        c.ipadx = 0;
    			        c.ipady = 0;
    			        c.gridx = gridx;
    			        c.gridy = gridy;
    			        c.gridwidth = gridwidth;
    			        c.gridheight = gridheight;
    			        c.anchor = anchor;
    			        return c;
    			    }
    	 
    	  public void actionPerformed(ActionEvent e)
    	  {
    		  String command = e.getActionCommand();   
    		  if ("Exit".equals(command)) 
    			  System.exit(0);
    		  else  if("Home".equals(command));
    				 Home.Home();
    		if("Refresh".equals(command));
    		//model.fireTableDataChanged();
    	  
    		
    
    			
    		  
    	  
    		  
    		  
    	          }
    	  
    	  public void tableChanged(TableModelEvent e) {
    	        int row = e.getFirstRow();
    	        int column = e.getColumn();
    	        TableModel model = (TableModel)e.getSource();
    	        String columnName = model.getColumnName(column);
    	        Object data = model.getValueAt(row, column);
    	        
    	        
    	  
    
    
    	        // Do something with the data...
    	    }
    	  
    
    
    
    }

  2. #2
    kevinn205 is offline Member
    Join Date
    Nov 2011
    Posts
    65
    Rep Power
    0

    Default Re: Refresh Jtable after data has been changed

    Out of curousity do people not want to help or do most people not know the answer :/

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: Refresh Jtable after data has been changed


  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Refresh Jtable after data has been changed

    Quote Originally Posted by kevinn205 View Post
    Out of curousity do people not want to help or do most people not know the answer :/
    Just asking a question doesn't guarantee an answer (especially on a forum where there are unpaid volunteers with hundreds of other posts to deal with)...getting answers is intimately tied to how you ask the question. Make the question easy to understand and answer (succinct, posting an SSCCE, and one single question), you get an answer faster. Make someone work that much harder and your chances drop.

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default Re: Refresh Jtable after data has been changed

    Quote Originally Posted by kevinn205 View Post
    Out of curousity do people not want to help or do most people not know the answer :/
    Most people probably looked at your wall of text and code and stopped reading. Call us lazy, but there are hundreds of posts here waiting on an answer, and we don't have time to get to all of them. So we can either answer 10 well-asked questions (see the advice doWhile already gave you, or see the link in my signature on asking questions the smart way), or we could take the same amount time to answer just 1 code dump. Which do you think is more effective?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: Refresh Jtable after data has been changed

    Out of curiosity are you going to reply and thank people when they give you help? Its been 11 hours since you got a reply.

    You only waited 5 hours before you expected people to answer your question!
    KevinWorkman and kevinn205 like this.

  7. #7
    kevinn205 is offline Member
    Join Date
    Nov 2011
    Posts
    65
    Rep Power
    0

    Default Re: Refresh Jtable after data has been changed

    Sorry I was finishing up a job for my company;

    I was just looking for any help, I didnt post a SSCE because my code is so interactive that it would not even work; therefore i'd have to create from scratch a brand new code that shows the problem; yes perhaps thats what I should have done

    with the answer to the few questions-- I would easily be able to solve this

    i'm not looking for a handout unless someone convinently had something similiar to post, rather im just looking for a ohh, well it looks like you used the add function to add to your list and then try to update it , your problem is that you need to use a arraylist and then reupdate the arraylist using a comand ; in order to fill the array list try using
    string [] = new array list (data)

    I was asking for merely statistical reason
    Last edited by kevinn205; 11-17-2011 at 05:13 PM.

  8. #8
    kevinn205 is offline Member
    Join Date
    Nov 2011
    Posts
    65
    Rep Power
    0

    Default Re: Refresh Jtable after data has been changed

    Thank you for the post Camerick, It helped a quite a bit.

Similar Threads

  1. Refresh JTable data
    By pink123 in forum AWT / Swing
    Replies: 1
    Last Post: 03-31-2011, 06:18 PM
  2. Could not able to refresh JTable.
    By pratim in forum AWT / Swing
    Replies: 3
    Last Post: 03-28-2011, 01:52 PM
  3. Ye Olde jTable Refresh Question...
    By Cynot in forum New To Java
    Replies: 4
    Last Post: 05-26-2010, 11:09 PM
  4. Dynamic Refresh in JTable every 5 Minutes
    By britto_bicsjohn in forum AWT / Swing
    Replies: 3
    Last Post: 08-07-2009, 05:16 AM
  5. how to refresh data of the JTable
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-17-2008, 12:01 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •