Results 1 to 4 of 4
Like Tree2Likes
  • 1 Post By doWhile
  • 1 Post By JosAH

Thread: JTable content refreshing

  1. #1
    ContoNick is offline Member
    Join Date
    Dec 2011
    Location
    Croatia,Zagreb
    Posts
    24
    Rep Power
    0

    Default JTable content refreshing

    Hi.
    I have JTable which contains two collumns: PID , Processname.

    So how should i refresh table content if some process is killed / terminated / quited / crashred or something else.

    Right now im trying to update JTable content using special thread to take care only about it.

    This is my ArrayList which contains Process object's
    Java Code:
    private ArrayList<Process> arListaProcesa;
    When init panel / frame im doing this:
    Java Code:
    arListaProcesa = getListProcesa();
    		int position = 0;
    		for (Process pro : arListaProcesa) {
    			panelFirstTab.getDefProcTableModel().insertRow(position, new Object[]{pro.getPid(),pro.getName()});
    		}
    and for refreshing it i have special thread which do this:
    Java Code:
    @Override
    		public void run() {
    			
    			
    			numProcess = arListaProces.size(); // first time when i init JPanel i fill DefTableModel with for loop thru arrayList of process objects and i add every element into Dtm and then create JTable
    			
    			while(true){
    				try{
    					if(end)
    						break;
    					
    					int numProcessNew = getListProcesa().size();
    					
    					if(numProcess != numProcessNew){
    						
    						DefaultTableModel dtm = new DefaultTableModel();
    						arListaProcesa = getListProcesa();
    						
    						
    						int position = 0;
    						for (Process pp : arListaProcesa) {
    							dtm.insertRow(position, new Object[]{pp.getPid(),pp.getName()});
    						}
    						
    						panelFirstTab.setDefProcTableModel(dtm);
    						panelFirstTab.getTableProces().repaint();
    						numProcess = numProcessNew;
    						
    					}
    					sleep(800);
    				}catch (Exception e) { }
    			}
    		}
    Is this the correct way how to refresh JTable content or did i missed whole idea ?

    Thank u.

    ( if u can please provide some example :) Thanks )

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: JTable content refreshing

    Consider creating a new TableModel object with each change, and do this off of the EDT -- the Swing event dispatch thread -- and then set your JTable with the new model on the EDT.

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

    Default Re: JTable content refreshing

    If I understand correctly - why reinsert all rows possibly only a small number of your data changed? Extend DefaultTableModel and have it backed by your List<Process> data. This way, any changes to the list are reflected in the TableModel - all you need is to notify the listeners that the model has changed (eg fireTableDataChanged)
    Fubarable likes this.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,730
    Blog Entries
    7
    Rep Power
    21

    Default Re: JTable content refreshing

    Quote Originally Posted by doWhile View Post
    Extend DefaultTableModel and have it backed by your List<Process> data. This way, any changes to the list are reflected in the TableModel - all you need is to notify the listeners that the model has changed (eg fireTableDataChanged)
    Better extend from the AbstractTableModel class.

    kind regards,

    Jos
    Fubarable likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 1
    Last Post: 05-25-2012, 05:05 PM
  2. Content from Database at runtime in Jtable
    By Aryanraj in forum AWT / Swing
    Replies: 1
    Last Post: 02-07-2012, 04:40 PM
  3. Replies: 1
    Last Post: 04-19-2011, 09:52 AM
  4. Data not refreshing in Jtable
    By pink123 in forum AWT / Swing
    Replies: 5
    Last Post: 03-22-2011, 10:43 PM
  5. Refreshing Jtable once again...
    By Norther in forum AWT / Swing
    Replies: 2
    Last Post: 06-29-2010, 08:46 PM

Posting Permissions

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