problem when adding a new row into xTable interactivly using vector

There are two screens involved. On one screen, it displays the xTable. On xTable screen, there is a button called "add new item". After clicking "add new item", a pop up window comes up (this is the other screen). On the pop up window, you can input the values for the provided text fields. After input, click the button "add". The added values are supposed to display on the xTable.

But I am facing to difficulty to display the added values on the xTable. The added values are not shown on the xTable. Also the vector is not adding the new value continuously. When I add first time, the vector size became 1. But when I continuously add new items, the vector size remains 1.

Can anybody help me with that? Thank you.

Here is my code:

PumpFoodDB.java, PumpFoodTableModel.java, PumpFoodCollection.java

import java.awt.Dimension;

import java.awt.Point;

import java.awt.Toolkit;

import javax.swing.*;

import javax.swing.event.TableModelEvent;

import javax.swing.event.TableModelListener;

publicclass PumpFoodDBextends javax.swing.JDialog{

private MainWindow parent;

private PumpFoodTableModel tableModel;

/** Creates new form PumpFoodDB */

public PumpFoodDB(MainWindow p){

initComponents();

this.parent = p;

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

Dimension frameSize = getSize();

setLocation(new Point((screenSize.width - frameSize.width) / 2,

(screenSize.height - frameSize.height) / 2));

tableModel =new PumpFoodTableModel();

xTable1.setModel(tableModel);

tableModel.addTableModelListener(xTable1);

}

/** This method is called from within the constructor to

* initialize the form.

* WARNING: Do NOT modify this code. The content of this method is

* always regenerated by the Form Editor.

*/

// <editor-fold defaultstate="collapsed" desc=" Generated Code ">

privatevoid initComponents(){

jScrollPane1 =new javax.swing.JScrollPane();

xTable1 =new edu.northwestern.at.utils.swing.XTable();

jButton1 =new javax.swing.JButton();

getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

xTable1.setModel(new javax.swing.table.DefaultTableModel(

new Object [][]{

},

new String []{

}

));

xTable1.addMouseListener(new java.awt.event.MouseAdapter(){

publicvoid mouseClicked(java.awt.event.MouseEvent evt){

xTable1MouseClicked(evt);

}

});

jScrollPane1.setViewportView(xTable1);

getContentPane().add(jScrollPane1,new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 80, 710, 310));

jButton1.setText("Add new item");

jButton1.addActionListener(new java.awt.event.ActionListener(){

publicvoid actionPerformed(java.awt.event.ActionEvent evt){

jButton1ActionPerformed(evt);

}

});

getContentPane().add(jButton1,new org.netbeans.lib.awtextra.AbsoluteConstraints(530, 410, -1, -1));

pack();

}// </editor-fold>

publicvoid updateTable(PumpFoodCollection collection){

tableModel.updateTable(collection);

}

privatevoid jButton1ActionPerformed(java.awt.event.ActionEvent evt){

//pop up window to enter the values for each text fields to be added into xTable

}

}

//**PumpFoodTableModel.java

import java.util.Vector;

import javax.swing.table.AbstractTableModel;

publicclass PumpFoodTableModelextends AbstractTableModel{

private String[] columnNames =new String[]{"col1","col2","col3","col4","col5","col6","col7"};

private Vector dataVector;

privateint numRows;

privateint nextEmptyRow;

/** Creates a new instance of PumpFoodTableModel */

public PumpFoodTableModel(){

dataVector =new Vector();

}

publicint getRowCount(){

return dataVector.size();

}

publicint getColumnCount(){

return columnNames.length;

}

public String getColumnName(int columnIndex){

return columnNames[columnIndex];

}

publicboolean isCellEditable(int rowIndex,int columnIndex){

returnfalse;

}

public Object getValueAt(int rowIndex,int columnIndex){

PumpFoodCollection collection = (PumpFoodCollection) dataVector.get(rowIndex);

switch (columnIndex){

case 0

return collection.getCol1();

case 1:

return collection.getCol2();

case 2:

return collection.getCol3();

case 3:

return collection.getCol4();

case 4:

return collection.getCol5();

case 5:

return collection.getCol6();

case 6:

return collection.getCol7();

default:

returnnew Object();

}

}

publicvoid setValueAt(Object value,int rowIndex,int columnIndex){

PumpFoodCollection collection = (PumpFoodCollection) dataVector.get(rowIndex);

switch (columnIndex){

case 0:

collection.setCol1(String) value);

break;

case 1:

collection.setCol2((String) value);

break;

case 2

collection.setCol3((String) value);

break;

case 3:

collection.setCol4((String) value);

break;

case 4:

collection.setCol5((String) value);

break;

case 5

collection.setCol6((String) value);

break;

case 6:

collection.setCol7(String) value);

break;

default:

System.out.println("invalid index");

}

fireTableCellUpdated(rowIndex, columnIndex);

}

publicvoid updateTable(PumpFoodCollection collection){

dataVector.addElement(collection);

fireTableRowsInserted(dataVector.size - 1, dataVector.size - 1);

}

}

//**PumpFoodCollection.java

publicclass PumpFoodCollection{

private String col1;

private String col2;

private String col3;

private String col4;

private String col5;

private String col6;

private String col7;

/** Creates a new instance of PumpFoodCollection */

public PumpFoodCollection(){

col1 ="";

col2 ="";

col3 ="";

col4 ="";

col5 ="";

col6 ="";

col7 ="";

}

public String getcol1(){

return col1;

}

publicvoid setcol1(String col1){

this.col1 = col1;

}

public String getcol2(){

return col2;

}

publicvoid setcol2(String col2){

this.col2 = col2;

}

public String getcol3(){

return col3;

}

publicvoid setcol3(String col3){

this.col3= col3;

}

public String getcol4(){

return col4;

}

publicvoid setcol4(String col4){

this.col4 = col4;

}

public String getcol5){

return col5;

}

publicvoid setcol5(String col5){

this.col5 = col5;

}

public String getcol6(){

return col6;

}

publicvoid setcol6(String col6){

this.col6 = col6;

}

public String getcol7){

return col7;

}

publicvoid setcol7(String col7){

this.col7 = col7;

}

}

Message was edited by:

vivian22

[14895 byte] By [vivian22a] at [2007-11-26 17:41:18]
# 1

The code looks reasonable, However I can't help you because:

a) the code you posted is incomplete. (the updateTable() method is never invoked). Add some debug code to the updateTable() method so make sure it is invoked.

b) your code uses IDE specific classes

c) your code uses not standard classes XTable

camickra at 2007-7-9 0:09:29 > top of Java-index,Desktop,Core GUI APIs...