get data from textfield to Jtable

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

30 Replies - 8146 Views - Last Post: 24 June 2012 - 11:02 AM Rate Topic: -----

#16 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2656
  • View blog
  • Posts: 11,203
  • Joined: 20-September 08

Re: get data from textfield to Jtable

Posted 21 June 2012 - 07:44 AM

There's some confusion here. Before the click, are you intending to have multiple ids in the same text field? If so, are they going to be space-separated?
Was This Post Helpful? 0
  • +
  • -

#17 chenaz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 82
  • Joined: 24-November 11

Re: get data from textfield to Jtable

Posted 21 June 2012 - 09:04 AM

View Postg00se, on 21 June 2012 - 07:44 AM, said:

There's some confusion here. Before the click, are you intending to have multiple ids in the same text field? If so, are they going to be space-separated?

nono.
example. first i put in productid 123 in the textfield.and i click button. jtable will show the information of product 123.
then i put in productid 111 again in the textfield and click on button. i want jtable to keep the product 123 as well. cos current code will replace product 111 over 123 and only show information of product 111. i wan jtable to show both 123 and 111.
Was This Post Helpful? 0
  • +
  • -

#18 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10396
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

Re: get data from textfield to Jtable

Posted 21 June 2012 - 09:18 AM

Then you should add a new entry into your TableModel.
Was This Post Helpful? 0
  • +
  • -

#19 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2656
  • View blog
  • Posts: 11,203
  • Joined: 20-September 08

Re: get data from textfield to Jtable

Posted 21 June 2012 - 09:35 AM

Quote

i want jtable to keep the product 123 as well.


Keeping an additional TableModel to which you can just add the current one is probably the easiest way to do that.
Was This Post Helpful? 0
  • +
  • -

#20 chenaz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 82
  • Joined: 24-November 11

Re: get data from textfield to Jtable

Posted 22 June 2012 - 08:46 AM

View Postmacosxnerd101, on 21 June 2012 - 09:18 AM, said:

Then you should add a new entry into your TableModel.



hi mac what do u mean add a new entry? i not good in java so i don understand wat do u mean by that.

g00se

i have been find ways on google,editing the current Dbutils and youtube videos to make a new table model for days . i just cant get a solution on this. could u help me on this?

thanks
Was This Post Helpful? 0
  • +
  • -

#21 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10396
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

Re: get data from textfield to Jtable

Posted 22 June 2012 - 08:51 AM

See the DefaultTableModel addRow() method.
Was This Post Helpful? 0
  • +
  • -

#22 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2656
  • View blog
  • Posts: 11,203
  • Joined: 20-September 08

Re: get data from textfield to Jtable

Posted 22 June 2012 - 09:11 AM

You don't need anything other than a DefaultTableModel. Just do

DefaultTableModel currentTableModel = (DefaultTableModel)DbUtils.resultSetToTableModel(rs);
cumulativeTableModel.getDataVector().addAll(currentTableModel.getDataVector()); // (again of type DefaultTableModel)

This post has been edited by g00se: 22 June 2012 - 09:13 AM
Reason for edit:: error corrected

Was This Post Helpful? 0
  • +
  • -

#23 chenaz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 82
  • Joined: 24-November 11

Re: get data from textfield to Jtable

Posted 22 June 2012 - 09:34 AM

ok i will try when i get hoes. thanks mac and g00se
Was This Post Helpful? 0
  • +
  • -

#24 chenaz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 82
  • Joined: 24-November 11

Re: get data from textfield to Jtable

Posted 22 June 2012 - 11:44 AM

cumulativeTableModel.getDataVector().addAll(currentTableModel.getDataVector()); // (again of type DefaultTableModel)


where is cumulativeTableModel came from?
Was This Post Helpful? 0
  • +
  • -

#25 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10396
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

Re: get data from textfield to Jtable

Posted 22 June 2012 - 02:07 PM

g00se is saying that cumulativeTableModel is your TableModel that you have created somewhere.
Was This Post Helpful? 1
  • +
  • -

#26 chenaz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 82
  • Joined: 24-November 11

Re: get data from textfield to Jtable

Posted 22 June 2012 - 09:35 PM

View Postmacosxnerd101, on 22 June 2012 - 02:07 PM, said:

g00se is saying that cumulativeTableModel is your TableModel that you have created somewhere.

really thanks for the help from u 2.
public class DbUtils {
    public static TableModel resultSetToTableModel(ResultSet rs) {
	try {
	    ResultSetMetaData metaData = rs.getMetaData();
	    int numberOfColumns = metaData.getColumnCount();
	    Vector<String> columnNames = new Vector<String>();

	    // Get the column names
	    for (int column = 0; column < numberOfColumns; column++) {
		columnNames.addElement(metaData.getColumnLabel(column + 1));
	    }

	    // Get all rows.
	    Vector<Vector<Object>> rows = new Vector<Vector<Object>>();

	    while (rs.next()) {
		Vector<Object> newRow = new Vector<Object>();

		for (int i = 1; i <= numberOfColumns; i++) {
		    newRow.addElement(rs.getObject(i));
		}

		rows.addElement(newRow);
	    }

	    return new DefaultTableModel(rows, columnNames);
	} catch (Exception e) {
	    e.printStackTrace();

	    return null;
	}
    }


so this is my code for table model.
and below is code for my button and added the
DefaultTableModel currentTableModel = (DefaultTableModel)DbUtils.resultSetToTableModel(rs);

cumulativeTableModel.getDataVector().addAll(currentTableModel.getDataVector());
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/testing");
PreparedStatement ps = con.prepareStatement("select * from product where productid = ?;");
ps.setString(1,pricetext.getText());
ResultSet rs = ps.executeQuery();

jTable1.setModel(DbUtils.resultSetToTableModel(rs));

DefaultTableModel currentTableModel = (DefaultTableModel)DbUtils.resultSetToTableModel(rs);

DbUtils.getDataVector().addAll(currentTableModel.getDataVector());
                         
} 
catch (Exception e){
JOptionPane.showMessageDialog(this, e.getMessage());
                                 
}



if i change the cumulativeTableModel to DbUtils the getVector will have error. and if i change to DbUtils.getDataVector().addAll(currentTableModel.getDataVector());
i will have error on getDataVector.

This post has been edited by chenaz: 22 June 2012 - 09:36 PM

Was This Post Helpful? 0
  • +
  • -

#27 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2656
  • View blog
  • Posts: 11,203
  • Joined: 20-September 08

Re: get data from textfield to Jtable

Posted 23 June 2012 - 05:13 AM

For lines 08-13, try
	DefaultTableModel cumulativeTableModel =  (DefaultTableModel)jTable1.getModel();
	DefaultTableModel currentTableModel = (DefaultTableModel)DbUtils.resultSetToTableModel(rs);
	cumulativeTableModel.getDataVector().addAll(currentTableModel.getDataVector());


Was This Post Helpful? 0
  • +
  • -

#28 chenaz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 82
  • Joined: 24-November 11

Re: get data from textfield to Jtable

Posted 23 June 2012 - 10:20 AM

View Postg00se, on 23 June 2012 - 05:13 AM, said:

For lines 08-13, try
	DefaultTableModel cumulativeTableModel =  (DefaultTableModel)jTable1.getModel();
	DefaultTableModel currentTableModel = (DefaultTableModel)DbUtils.resultSetToTableModel(rs);
	cumulativeTableModel.getDataVector().addAll(currentTableModel.getDataVector());


u mean add to my button table?
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/testing");
PreparedStatement ps = con.prepareStatement("select * from product where productid = ?;");
ps.setString(1,pricetext.getText());
ResultSet rs = ps.executeQuery();
                  
DefaultTableModel cumulativeTableModel = (DefaultTableModel)jTable1.getModel();
DefaultTableModel currentTableModel = (DefaultTableModel)DbUtils.resultSetToTableModel(rs);
cumulativeTableModel.getDataVector().addAll(currentTableModel.getDataVector());
 
} 
catch (Exception e){
JOptionPane.showMessageDialog(this, e.getMessage());
                                     
}




i replaced it with wat u instructed. no luck, it does not show anything. when i clicked on button, it does not show anything.
so i added jTable1.setModel(DbUtils.resultSetToTableModel(rs)); before ur code. it still replace the first product.
if i add the jTable1.setModel(DbUtils.resultSetToTableModel(rs)); after ur code. it will not display anything.

gonna give up soon T_T sorry for my late reply. cos gonna work and study and handle etc also
Was This Post Helpful? 0
  • +
  • -

#29 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2656
  • View blog
  • Posts: 11,203
  • Joined: 20-September 08

Re: get data from textfield to Jtable

Posted 24 June 2012 - 01:20 AM

Quote

u mean add to my button table?


Yes. I don't see why that code shouldn't work, though it would be better to close the Connection and the rest each time
Was This Post Helpful? 0
  • +
  • -

#30 chenaz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 82
  • Joined: 24-November 11

Re: get data from textfield to Jtable

Posted 24 June 2012 - 09:05 AM

View Postg00se, on 24 June 2012 - 01:20 AM, said:

Quote

u mean add to my button table?


Yes. I don't see why that code shouldn't work, though it would be better to close the Connection and the rest each time

i have no idea also
Posted Image
Posted Image
Posted Image
Posted Image




first image shows the result of using

DefaultTableModel cumulativeTableModel =  (DefaultTableModel)jTable1.getModel();

DefaultTableModel currentTableModel = (DefaultTableModel)DbUtils.resultSetToTableModel(rs);

cumulativeTableModel.getDataVector().addAll(currentTableModel.getDataVector());


second and third image using the old codes.
i also change the table model to
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

/**
 *
 * @author aizhong
 */

import javax.swing.table.*;
import java.sql.*;
import java.util.*;

public class TableModelFromRs extends AbstractTableModel {
	// the Column names based on the DB
	private String[] name;
	// ArrayList to hold each row composed of an array of Object
	private ArrayList<Object[]> al;
	
	public  TableModelFromRs(ResultSet rs) {
		try {
			// meta data to get info on the database
			ResultSetMetaData metaData = rs.getMetaData();
			
			// number of columns present in the ResultSet
			int nbCol = metaData.getColumnCount();
			name = new String[nbCol];
			// Load the Array of column names
			for (int i = 0; i < nbCol; ++i) {
				name[i] = metaData.getColumnLabel(i + 1); // +1 SQL columns start at 1
			}

			// Now get the rows
			al = new ArrayList<Object[]>();
			// while there are still rows in the ResultSet
			while (rs.next()) {
				Object[] row = new Object[nbCol];
				// retreive each column
				for (int i = 0; i < nbCol; i++) {
					row[i] = rs.getObject(i+1);				// +1 SQL columns start at 1
				}
				// insert into arrayList
				al.add(row);
			}

		} catch (Exception e) {
			System.out.println("Error building TableModel: " + e);
			e.printStackTrace();
		}
	}

    // the number of rows is the number of entries in the ArrayList
	public int getRowCount() {
		return al.size();
	}

	// the number of columms is the size of the the Array of column name
	public int getColumnCount() {
		return name.length;
	}

	// we retreive the array of Object[] at that index in the ArrayList
	// then the object at the required column
	public Object getValueAt(int rowIndex, int columnIndex) {
		return al.get(rowIndex)[columnIndex];
	}
	
	// the column name is in the String[] array of column name
	public String getColumnName(int columnIndex) {
		return name[columnIndex];
	}

}



and add the
TableModelFromRs tmfr = new TableModelFromRs(rs);                  
                  jTable1.setModel(tmfr);

to link the button and table together.
still result still same as second and third image.

image 4 shows what i wan the result to be. but it always nv show wat i want it to be.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3