JTable and JavaBeans

Hello everybody.

I'm trying to build dinamically a JTable, reading data from a JavaBean.

Everythings works fine, but :

1) if I click outside of my JFrame, when I reselect it the JTableis completely filled with the last row inserted

2)If I scroll the JTable (that is contained in a JScrollPane), rows that momentanely disappear because of the scroll, when I rescroll to the original position I discover that all rows that were "out of sight" as been filled up ith last row's data

3)If I select a column, a row, or simply a cell, when I click on another one this another one's row is filled up with last row data

import java.util.ArrayList;

import java.util.Iterator;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.table.AbstractTableModel;

import beans.ArticoloBean;

class TableModelextends AbstractTableModel{

private String colonna1;

private String colonna2;

private ArrayList list;

private String[] Columns ={

"Articolo","n癱olli","Note"

};

Iterator Ilista_articoli;

ArticoloBean myBean = (ArticoloBean)Ilista_articoli;

int rowcount=0;

public TableModel(ArrayList list){

this.list=list;

Ilista_articoli=list.iterator();

if (Ilista_articoli.hasNext()){

myBean = (ArticoloBean)Ilista_articoli.next();

}

}

publicint getRowCount(){

return list.size();

}

publicint getColumnCount(){

return Columns.length;

}

public Object getValueAt(int riga,int colonna){

if (rowcount==3){

if (Ilista_articoli.hasNext()){

myBean = (ArticoloBean)Ilista_articoli.next();

rowcount=0;

}

}

rowcount++;

if (colonna==0)return myBean.getCodice();

if (colonna==1)return myBean.getColli();

if (colonna==2)return myBean.getNote();

returnnull;

}

publicboolean isCellEditable(int riga,int colonna){

returnfalse;

}

public String getColumnName(int colonna){

return Columns[colonna];

}

[4222 byte] By [Pentolinoa] at [2007-11-27 8:32:06]
# 1
up
pentolinioa at 2007-7-12 20:27:50 > top of Java-index,Desktop,Core GUI APIs...
# 2
Well, your TableModel getValueAt(...) method doesn't look correct. Althought I don't know what you bean looks like to it hard to say what the code should be. Why don't you just use the DefaultTableModel and copy the data from the bean to the model?
camickra at 2007-7-12 20:27:50 > top of Java-index,Desktop,Core GUI APIs...