12 Replies - 1621 Views - Last Post: 10 July 2012 - 01:24 PM Rate Topic: ***-- 2 Votes

#1 jess131  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 07-July 12

JTable problem - How to refresh JTable when I click on the button

Posted 07 July 2012 - 01:27 AM

Hello there,

I am currently working on JTable with using mysql on it. My problem is , I've tried ANY way that could be possible to refresh my JTable but none of them worked and I decided to disturb you guys if you wouldnt mind.

So, I am using vectors to get my data from MYSQL Database, and I only have a single fucntion to handle all the process.Here is my JTable creation code,

How can I make a fucntion to refresh my JTable according to this code?

Also here is the whole code of mine : http://pastebin.com/GSvjV4Va

final Vector columnNames = new Vector();
    		final Vector data = new Vector();
    
    		try
    		{
    			//  Connect to an Access Database
    
    
    
    			driver = IniFonksiyon.iniSVNOkut(driver, "databaseBilgileri", "driver");//"com.mysql.jdbc.Driver";
    			url = IniFonksiyon.iniOkut(url, "databaseBilgileri", "url");//"jdbc:mysql://localhost:3306/";
    			userid = IniFonksiyon.iniOkut(userid, "databaseBilgileri", "kullanici");
    			password = IniFonksiyon.iniOkut(password, "databaseBilgileri", "sifre");
    			dbName = IniFonksiyon.iniOkut(dbName, "databaseBilgileri", "dbIsmi");
    
    			Class.forName( driver );
    			Connection connection = DriverManager.getConnection( url+dbName	, userid, password );
    
    			//  Read data from a table
    
    			String sql = "select * from profildb.tbl_detailed";
    			Statement stmt = connection.createStatement();
    			ResultSet rs = stmt.executeQuery( sql );
    
    			ResultSetMetaData md = rs.getMetaData();
    			int columns = md.getColumnCount();
    
    			//  Get column names
    
    			for (int i = 1; i <= columns; i++)
    			{
    				columnNames.addElement( md.getColumnName(i) );
    			}
    
    			//  Get row data
    
    			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();
    			connection.close();
    		}
    		catch(Exception e)
    		{
    			System.out.println( e );
    		}
    
    		//  Create table with database data
    
    		final JTable table = new JTable(data, columnNames)
    		{
    			/**
    			 * 
    			 */
    			private static final long serialVersionUID = 1L;
    
    			public Class getColumnClass(int column)
    			{
    				for (int row = 0; row < getRowCount(); row++)
    				{
    					Object o = getValueAt(row, column);
    
    					if (o != null)
    					{
    						return o.getClass();
    					}
    				}
    
    				return Object.class;
    			}
    		};




Thanks in advance!

Is This A Good Question/Topic? 1
  • +

Replies To: JTable problem - How to refresh JTable when I click on the button

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2110
  • View blog
  • Posts: 8,776
  • Joined: 20-September 08

Re: JTable problem - How to refresh JTable when I click on the button

Posted 07 July 2012 - 04:07 AM

Quote

I've tried ANY way that could be possible to refresh my JTable

I don't know what you mean by that. Currently, nothing (afaics) in your code attempts to alter the table since the time it was filled ...
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8019
  • View blog
  • Posts: 31,126
  • Joined: 06-March 08

Re: JTable problem - How to refresh JTable when I click on the button

Posted 07 July 2012 - 08:00 AM

Is the JTable refreshed if you resize yor JFrame ?

You haven't show us any of your multiple unsuccesful attempts to refresh the JTable

Usually table.tableChanged(TableModelEvent e); works
Was This Post Helpful? 1
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2110
  • View blog
  • Posts: 8,776
  • Joined: 20-September 08

Re: JTable problem - How to refresh JTable when I click on the button

Posted 07 July 2012 - 08:01 AM

Essentially, you will have to issue the query each time. JTable is not 'live' or bound
Was This Post Helpful? 1
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 9029
  • View blog
  • Posts: 33,490
  • Joined: 27-December 08

Re: JTable problem - How to refresh JTable when I click on the button

Posted 07 July 2012 - 04:09 PM

Also, as of Java 1.5, Collections like Vectors are generic. You should use generics to avoid a deprecation warning.
Was This Post Helpful? 1
  • +
  • -

#6 jess131  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 07-July 12

Re: JTable problem - How to refresh JTable when I click on the button

Posted 08 July 2012 - 12:44 AM

View Postg00se, on 07 July 2012 - 04:07 AM, said:

Quote

I've tried ANY way that could be possible to refresh my JTable

I don't know what you mean by that. Currently, nothing (afaics) in your code attempts to alter the table since the time it was filled ...


I've tried the following ways ,

///////////////
        revalidate(),
/////////////// 
        repaint(), 
//////////////

	DefaultTableModel tableModel = (DefaultTableModel) table.getModel();

       // tableModel.setRowCount(0);
	tableModel.fireTableDataChanged();

/////////////

      dtm = new DefaultTableModel();
      table.setModel(dtm);
      table.tableChanged(new TableModelEvent(table.getModel())) ;
      SwingUtilities.updateComponentTreeUI(scrollPane);

/////////////

       DefaultTableModel model = new DefaultTableModel(rowData, colNames);
       table = new JTable(model);

////////////







View Postpbl, on 07 July 2012 - 08:00 AM, said:

Is the JTable refreshed if you resize yor JFrame ?

You haven't show us any of your multiple unsuccesful attempts to refresh the JTable

Usually table.tableChanged(TableModelEvent e); works



View Postmacosxnerd101, on 07 July 2012 - 04:09 PM, said:

Also, as of Java 1.5, Collections like Vectors are generic. You should use generics to avoid a deprecation warning.


View Postg00se, on 07 July 2012 - 04:07 AM, said:

Quote

I've tried ANY way that could be possible to refresh my JTable

I don't know what you mean by that. Currently, nothing (afaics) in your code attempts to alter the table since the time it was filled ...


I've tried the following ways ,

///////////////
        revalidate(),
/////////////// 
        repaint(), 
//////////////

	DefaultTableModel tableModel = (DefaultTableModel) table.getModel();

       // tableModel.setRowCount(0);
	tableModel.fireTableDataChanged();

/////////////

      dtm = new DefaultTableModel();
      table.setModel(dtm);
      table.tableChanged(new TableModelEvent(table.getModel())) ;
      SwingUtilities.updateComponentTreeUI(scrollPane);

/////////////

       DefaultTableModel model = new DefaultTableModel(rowData, colNames);
       table = new JTable(model);

////////////

       tableScroll.getViewport().add(documentTable);
 




View Postpbl, on 07 July 2012 - 08:00 AM, said:

Is the JTable refreshed if you resize yor JFrame ?

You haven't show us any of your multiple unsuccesful attempts to refresh the JTable

Usually table.tableChanged(TableModelEvent e); works



Sir, I am quite new about JTable, I really still didnt get the main point about tables and how to use them so I am still newbie.
My tableChanged actually won't do anything; (I dont actually know how to connect them )

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



View Postmacosxnerd101, on 07 July 2012 - 04:09 PM, said:

Also, as of Java 1.5, Collections like Vectors are generic. You should use generics to avoid a deprecation warning.



Thank you, I will do that sir. :)
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2110
  • View blog
  • Posts: 8,776
  • Joined: 20-September 08

Re: JTable problem - How to refresh JTable when I click on the button

Posted 08 July 2012 - 01:15 AM

I think you have a misunderstanding about what JTable does. It doesn't monitor the state of a database table. All it does is present the results of a query in tabular format, which is why i made this comment. You can then (normally) edit what's in the cells, but that has no effect on the table from which the data originally came.

This post has been edited by g00se: 08 July 2012 - 01:16 AM
Reason for edit:: link

Was This Post Helpful? 0
  • +
  • -

#8 jess131  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 07-July 12

Re: JTable problem - How to refresh JTable when I click on the button

Posted 08 July 2012 - 01:53 AM

View Postg00se, on 08 July 2012 - 01:15 AM, said:

I think you have a misunderstanding about what JTable does. It doesn't monitor the state of a database table. All it does is present the results of a query in tabular format, which is why i made this comment. You can then (normally) edit what's in the cells, but that has no effect on the table from which the data originally came.



Hey, unfortunately I am still trying to understand it fully :) Could I implement DefaultTableModel to my code without changing any code ? and Would it work ?
Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2110
  • View blog
  • Posts: 8,776
  • Joined: 20-September 08

Re: JTable problem - How to refresh JTable when I click on the button

Posted 08 July 2012 - 10:58 AM

I don't know what you mean by 'would it work?'

This post has been edited by g00se: 08 July 2012 - 10:59 AM

Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8019
  • View blog
  • Posts: 31,126
  • Joined: 06-March 08

Re: JTable problem - How to refresh JTable when I click on the button

Posted 08 July 2012 - 12:22 PM

View Postjess131, on 08 July 2012 - 03:44 AM, said:

My tableChanged actually won't do anything; (I dont actually know how to connect them )

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

Don't overload tableChanged(), remove that method use the standard one provided my the JTable class
Was This Post Helpful? 1
  • +
  • -

#11 RCR  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 33
  • Joined: 04-July 12

Re: JTable problem - How to refresh JTable when I click on the button

Posted 08 July 2012 - 01:56 PM

Don't really know about Jtables but I think this will work for you:

DefaultTableModel modelName = (DefaultTableModel)table.getModel();
modelName.fireTableChanged(new TableModelEvent(column.name());


column.name() is random but just as an example.

This post has been edited by RCR: 08 July 2012 - 01:59 PM

Was This Post Helpful? 1
  • +
  • -

#12 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2110
  • View blog
  • Posts: 8,776
  • Joined: 20-September 08

Re: JTable problem - How to refresh JTable when I click on the button

Posted 09 July 2012 - 03:23 AM

Quote

modelName.fireTableChanged(new TableModelEvent(column.name());

Why would you need to write such code? There's no indication from the code posted that anything has made an internal change (to the TableModel) that the table wouldn't already know about.
Was This Post Helpful? 1
  • +
  • -

#13 jess131  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 07-July 12

Re: JTable problem - How to refresh JTable when I click on the button

Posted 10 July 2012 - 01:24 PM

View Postpbl, on 08 July 2012 - 12:22 PM, said:

View Postjess131, on 08 July 2012 - 03:44 AM, said:

My tableChanged actually won't do anything; (I dont actually know how to connect them )

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

Don't overload tableChanged(), remove that method use the standard one provided my the JTable class


Thank you sir, it worked.


View PostRCR, on 08 July 2012 - 01:56 PM, said:

Don't really know about Jtables but I think this will work for you:

DefaultTableModel modelName = (DefaultTableModel)table.getModel();
modelName.fireTableChanged(new TableModelEvent(column.name());


column.name() is random but just as an example.


After fixing overload, my code started to work. But tried your way too, it didnt work:( Thanks anyway!


View Postg00se, on 09 July 2012 - 03:23 AM, said:

Quote

modelName.fireTableChanged(new TableModelEvent(column.name());

Why would you need to write such code? There's no indication from the code posted that anything has made an internal change (to the TableModel) that the table wouldn't already know about.


Probably I forgot it over there while trying everything :) Thank you tho!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1