5 Replies - 5063 Views - Last Post: 18 January 2011 - 06:25 PM Rate Topic: -----

#1 leduyquang  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 18-January 11

Binding data into Jtable

Posted 18 January 2011 - 03:27 AM

Hi guys! I've had a little problem with how to bind record from database into Jtable :) Writing code by line by line is ok and work well but when i try to write this code in design by draging control to the JFrame ,it doesn't work! Hope you guys help me to fix this problem.Thank so much !
Here is my code:
  //connection database is Ok
  // problem is following here:
     Resultset rs=bll.getdata(proname, param); // this line work well                
     DefaultTableModel model=new DefaultTableModel();
     String Id="";
     String Name="";
                 while(rs.next())
                 {
                   Id=rs.getString("Emp_ID");
                   Name=rs.getString("Emp_Name");
                   temp=new Vector();
                   temp.addElement(Id);
                   temp.addElement(Name);
                   data.addElement(temp);  //data and heading are vector                
                   model.addColumn(heading);
                   model.addRow(data);
                   jTable1.setModel(model);
                 }


This post has been edited by macosxnerd101: 18 January 2011 - 09:15 AM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Binding data into Jtable

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Binding data into Jtable

Posted 18 January 2011 - 05:47 AM

What exactly is the problem? do you get any error messages? if yes, please post them.
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10387
  • View blog
  • Posts: 38,439
  • Joined: 27-December 08

Re: Binding data into Jtable

Posted 18 January 2011 - 09:16 AM

Also, Vector is generic. So it should be Vector<Object>, Vector<String>, etc., for whatever type you're storing.
Was This Post Helpful? 0
  • +
  • -

#4 redhotfire0  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 210
  • Joined: 13-July 09

Re: Binding data into Jtable

Posted 18 January 2011 - 09:20 AM

temp=new Vector();


You are initializing a new vector everytime the resultset changes row. Is that what you really need? Just a question.

This post has been edited by redhotfire0: 18 January 2011 - 09:21 AM

Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10387
  • View blog
  • Posts: 38,439
  • Joined: 27-December 08

Re: Binding data into Jtable

Posted 18 January 2011 - 09:23 AM

JTable accepts a Vector<Vector<?>>, so there should be a Vector for each row. That being said, you need to add the Vector from each iteration to a main Vector.
Was This Post Helpful? 1
  • +
  • -

#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: Binding data into Jtable

Posted 18 January 2011 - 06:25 PM

A lot easier to make your own class to implement the model
	class MyModel extends AbstractTableModel {

		Vector<String> vId, vName;
		String[] header = {"Id", "Name"};
		// constructor
		MyModel() {
			vId = new Vector<String>();
			vName = new Vector<String>();
		}
		// to empty the vector
		void clear() {
			vId.clear();
			vName.clear();
		}
		// to add an element
		void add(String id, String name) {
			vId.add(id);
			vName.add(name);
		}
		public int getColumnCount() {
			return 2;
		}
		public int getRowCount() {
			return vId.size();
		}

		public String getColumnName(int col) {
			return header[col];
		}
		
		public Object getValueAt(int row, int col) {
			if(col == 0)
				return vId.get(row);
			else
				return vName.get(row);
		}
		
	}


Then you can have something like that to create model and JTable
	{
		MyModel tm = new MyModel();
		JTable table = new JTable(tm);
                ....
                loadFromBD(tm);
	}
	
	private void loadFromBD(MyModel tm) {
		// empty model data
		tm.clear();
		// loop on resultset
		while(rs.next()) {
			...
			// add new row
			tm.add(id, name);
		}
	}




Now you can call back loadFromBD() as often as you wish like after database change
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1