[HELP] Updating/Refreshing JTable connected to database

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 7323 Views - Last Post: 02 January 2013 - 01:17 AM Rate Topic: -----

#1 mr.nacho  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 28-December 12

[HELP] Updating/Refreshing JTable connected to database

Posted 28 December 2012 - 12:15 AM

I've been trying to update/refresh a JTable but still no success. I'm using an AbstractTableModel. What am I doing wrong here?

	   	
                model = new AbstractTableModel()
	        {


	        public int getColumnCount()
	        {
	        	return columnNames.length;
	        }

	        public String getColumnName(int column)
	        {

	        	return columnNames[column];
	        }

	        public int getRowCount()
	        {
	        	return rowData.length;
	        }

	        public Object getValueAt(int row, int column)
	        {
	        	return rowData[row][column];

	        }

	        public void setValueAt(Object value, int row, int column)
	        {
	        	rowData[row][column] = value;
	        	fireTableCellUpdated(row, column);
		    }

	        public boolean isCellEditable(int row, int column)
	        {
	        	return false;
	        }

   		 };

   		model.addTableModelListener(new TableModelListener()
   		{
	        public void tableChanged(TableModelEvent e)
	        {
				//Don't know what to add.
	        }
   		});


Thank you in advance for the help..

Is This A Good Question/Topic? 0
  • +

Replies To: [HELP] Updating/Refreshing JTable connected to database

#2 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: [HELP] Updating/Refreshing JTable connected to database

Posted 28 December 2012 - 07:56 AM

You surely don;t want that

model = new AbstractTableModel()

you want your class to extend AbstractTableModel so that your methods, well written, will be called

class MyModel extends AbstractTableModel {

   public int getColumnCount()  
   {  


Was This Post Helpful? 1
  • +
  • -

#3 mr.nacho  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 28-December 12

Re: [HELP] Updating/Refreshing JTable connected to database

Posted 28 December 2012 - 06:23 PM

I just did it but nothing happen. Still, I need to close and run the program again to see changes. What am I missing? Sorry sir, I'm a newbie in java came from visual basic 6.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: [HELP] Updating/Refreshing JTable connected to database

Posted 28 December 2012 - 09:43 PM

Post your updated code
Was This Post Helpful? 0
  • +
  • -

#5 mr.nacho  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 28-December 12

Re: [HELP] Updating/Refreshing JTable connected to database

Posted 29 December 2012 - 08:29 AM

public class manipulatingData extends JPanel implements ActionListener
{
	private JButton btnAdd, btnEdit, btnDelete, btnSearch, btnSave, btnCancel;
	private JFrame f;
	private JPanel p,panel;
	private JDialog addData;
	private JLabel lblEnterName;
	private JTextField txtName;

	JTable table;
	JTableHeader header;

	Connection con;
	Statement s;
	ResultSet rs;
	ResultSetMetaData rsmd;
	String cDriver = "sun.jdbc.odbc.JdbcOdbcDriver";


	int count = 0;
	int recordCount = 0;

	String strCount;
	String name = "";
	String sql;
	private int confirm = 0;
	
	MyModel model = new MyModel();
	
	public manipulatingData()
	{

		connectDB();

		f = new JFrame();
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		table = new JTable(model);

		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

	}

	public static void main (String [] args)
	{
		new manipulatingData();

	}

	public void actionPerformed (ActionEvent e)
	{

		if (e.getSource()==btnDelete)
		{
			confirm = JOptionPane.showConfirmDialog(null,"Do you want to delete this?","Confirmation",0);

			if(confirm == 0)
			{
				try
				{
					rs.beforeFirst();

					while(rs.next())
					{
						if(table.getSelectedRow() == 0 && rs.getRow() == 1)
						{
							rs.deleteRow();
						//	JOptionPane.showMessageDialog(null,"Record Deleted","Successful",1);

							break;


						}
						else if(table.getSelectedRow() == rs.getRow())
						{
							rs.next();
							rs.deleteRow();

						//	JOptionPane.showMessageDialog(null,"Record Deleted","Successful",1);



							break;

						}

					}


				}
				catch (SQLException a)
				{
					System.err.println(a);
				}



			}
		}

	}

		public void connectDB()
		{

			try
			{

				Class.forName(cDriver);

				con = DriverManager.getConnection("jdbc:odbc:testDBjava3.accdb","","");
				s = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
				rs = s.executeQuery("Select * from friends");
				rsmd = rs.getMetaData();

				recordCount = 0;
				count = rsmd.getColumnCount();

	 			String fieldNames[] = new String[count];

	 			for(int a=1;a<=count;a++)
	 			{
	 				fieldNames[a-1] = rsmd.getColumnName(a);
	 			}

				while(rs.next())
				{
	 				recordCount++;
				}



				Object data [][]  = new Object[recordCount][count];

				rs.beforeFirst();
				for(int x = 0;x<recordCount;x++)
				{
					rs.next();
					for(int y = 0;y<count;y++)
					{
						data[x][y] = rs.getObject(y+1);
					}
				}

				model.columnNames = fieldNames;
				model.rowData = data;

			}

			catch (ClassNotFoundException exp)
			{
				System.err.println(exp);
			}

			catch(SQLException exp)
			{
				System.err.println(exp);
			}
		}
}

	class MyModel extends AbstractTableModel
	{
		Object rowData[][] = {};
		String columnNames[] = {};

	    public int getColumnCount()
	    {
	       return columnNames.length;
	    }

	       public String getColumnName(int column)
	       {

	        	return columnNames[column];
	        }

	        public int getRowCount()
	        {
	        	return rowData.length;
	        }

	        public Object getValueAt(int row, int column)
	        {
	        	return rowData[row][column];
	        }

	        public void setValueAt(Object value, int row, int column)
	        {
	        	rowData[row][column] = value;
				fireTableCellUpdated(row, column);
		    }

	        public boolean isCellEditable(int row, int column)
	        {
	        	return false;
	        }
	}





I included some part of the constructor in the main.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: [HELP] Updating/Refreshing JTable connected to database

Posted 29 December 2012 - 09:50 AM

I think just one line missing :)
You have to inform the JTable that it has to redraw itself
      model.columnNames = fieldNames;  
      model.rowData = data;  

      table.tableChanged(new TableModelEvent(model));


Was This Post Helpful? 1
  • +
  • -

#7 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2652
  • View blog
  • Posts: 11,191
  • Joined: 20-September 08

Re: [HELP] Updating/Refreshing JTable connected to database

Posted 29 December 2012 - 09:58 AM

You'd probably be better off starting from JdbcRowSet - it's closer. See

http://www.brikkesys...ftware/doku.php
http://bobnguyen.wor.../category/java/
Was This Post Helpful? 1
  • +
  • -

#8 mr.nacho  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 28-December 12

Re: [HELP] Updating/Refreshing JTable connected to database

Posted 29 December 2012 - 10:02 AM

Sir, it's still not refreshing. The line "table.tableChanged(new TableModelEvent(model));", where should I really put it? Inside the constructor? I tried it on the delete button and it just unselect it without erasing it from the table.

Should I need a listener?
Was This Post Helpful? 0
  • +
  • -

#9 mr.nacho  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 28-December 12

Re: [HELP] Updating/Refreshing JTable connected to database

Posted 29 December 2012 - 11:57 AM

Can I just post the complete codes?
I'm really having a headache. :helpsmilie:
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: [HELP] Updating/Refreshing JTable connected to database

Posted 29 December 2012 - 03:15 PM

I show you exactly where to put it. Just after you modified your model y doing

model.columnNames = fieldNames;
model.rowData = data;
Was This Post Helpful? 0
  • +
  • -

#11 mr.nacho  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 28-December 12

Re: [HELP] Updating/Refreshing JTable connected to database

Posted 29 December 2012 - 08:25 PM

Sir, that part is from the void method establishing connection and passing the content of database to the model. If I put the tableChanged right after that, there would be an error stating null pointer exception. :sad3:
Was This Post Helpful? 0
  • +
  • -

#12 mr.nacho  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 28-December 12

Re: [HELP] Updating/Refreshing JTable connected to database

Posted 31 December 2012 - 02:01 AM

Sir PBL, do you think it has something to do with the scrollpane?
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: [HELP] Updating/Refreshing JTable connected to database

Posted 31 December 2012 - 07:54 AM

Why null pointer ? The JTable should already be created. Never a good idea to add component to a GUI on the fly (after user's intervention/click)

The steps are:
- create model with arrays of size 0 for rows and columns and column names
- create JTable for that model and put the JTable in scroll pane (nothing will be displayed)
- after user click:
- read the database
- update the model
- fire that table change

This post has been edited by pbl: 31 December 2012 - 07:57 AM

Was This Post Helpful? 0
  • +
  • -

#14 mr.nacho  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 28-December 12

Re: [HELP] Updating/Refreshing JTable connected to database

Posted 01 January 2013 - 03:00 AM

Sir, it's working after I call the method for database connection. I put it on the actionEvent but the problem is, there is a delay changes. When I try to delete or add, the data needs to be repeatedly deleted/added again for the model to adapt to changes. Why is it happening?

Sir, it's working after I call the method for database connection. I put it on the actionEvent but the problem is, there is a delay changes. When I try to delete or add, the data needs to be repeatedly deleted/added again for the model to adapt to changes. Why is it happening?

Sir, it's working after I call the method for database connection. I put it on the actionEvent but the problem is, there is a delay changes. When I try to delete or add, the data needs to be repeatedly deleted/added again for the model to adapt to changes. Why is it happening?
Was This Post Helpful? 0
  • +
  • -

#15 mr.nacho  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 28-December 12

Re: [HELP] Updating/Refreshing JTable connected to database

Posted 01 January 2013 - 03:10 AM

Sir, it's working after I call the method for database connection. I put it on the actionEvent but the problem is, there is a delay changes. When I try to delete or add, the data needs to be repeatedly deleted/added again for the model to adapt to changes. Why is it happening?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2