30 Replies - 3534 Views - Last Post: 24 June 2012 - 11:02 AM
#16
Re: get data from textfield to Jtable
Posted 21 June 2012 - 07:44 AM
#17
Re: get data from textfield to Jtable
Posted 21 June 2012 - 09:04 AM
g00se, on 21 June 2012 - 07:44 AM, said:
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.
#18
Re: get data from textfield to Jtable
Posted 21 June 2012 - 09:18 AM
#19
Re: get data from textfield to Jtable
Posted 21 June 2012 - 09:35 AM
Quote
Keeping an additional TableModel to which you can just add the current one is probably the easiest way to do that.
#20
Re: get data from textfield to Jtable
Posted 22 June 2012 - 08:46 AM
macosxnerd101, on 21 June 2012 - 09:18 AM, said:
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
#21
Re: get data from textfield to Jtable
Posted 22 June 2012 - 08:51 AM
#22
Re: get data from textfield to Jtable
Posted 22 June 2012 - 09:11 AM
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
#23
Re: get data from textfield to Jtable
Posted 22 June 2012 - 09:34 AM
#24
Re: get data from textfield to Jtable
Posted 22 June 2012 - 11:44 AM
where is cumulativeTableModel came from?
#26
Re: get data from textfield to Jtable
Posted 22 June 2012 - 09:35 PM
macosxnerd101, on 22 June 2012 - 02:07 PM, said:
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
#27
Re: get data from textfield to Jtable
Posted 23 June 2012 - 05:13 AM
DefaultTableModel cumulativeTableModel = (DefaultTableModel)jTable1.getModel(); DefaultTableModel currentTableModel = (DefaultTableModel)DbUtils.resultSetToTableModel(rs); cumulativeTableModel.getDataVector().addAll(currentTableModel.getDataVector());
#28
Re: get data from textfield to Jtable
Posted 23 June 2012 - 10:20 AM
g00se, on 23 June 2012 - 05:13 AM, said:
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
#29
Re: get data from textfield to Jtable
Posted 24 June 2012 - 01:20 AM
Quote
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
#30
Re: get data from textfield to Jtable
Posted 24 June 2012 - 09:05 AM
g00se, on 24 June 2012 - 01:20 AM, said:
Quote
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




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.
|
|

New Topic/Question
Reply




MultiQuote






|