0

I'm showing the method were the JTable is constructed, the error is when adding the rows inside the for (int i = 1; i <= numero_columnas; i++) loop, or the way the DefaultTableModel model = new DefaultTableModel(); is declared, I can't find the error.


public void verTablaTable (Connection db, String nombre) throws Exception{
    Statement stmt=db.createStatement();
    ResultSet sst_ResultSet = stmt.executeQuery("SELECT * FROM "+nombre);
    ResultSetMetaData md = sst_ResultSet.getMetaData();
    int numero_columnas = md.getColumnCount();
    DefaultTableModel model = new DefaultTableModel();
    for (int i=1;i<=numero_columnas; i++){
        model.addColumn(md.getColumnName(i));
    }
    JTable tabla =new JTable(model);
    DefaultTableModel model1 = (DefaultTableModel) tabla.getModel();
    Vector row = new Vector();
    row.setSize(numero_columnas);
    while (sst_ResultSet.next()){
        for (int i = 1; i <= numero_columnas; i++){
            row.set(i-1,sst_ResultSet.getString(i));
        }
        model1.addRow(row);
    }
    tabla.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    JScrollPane sp_vertabla = new JScrollPane(tabla);
    sp_vertabla.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    sp_vertabla.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    sp_vertabla.setBounds(50,30,700,500);
    JPanel cont_vertabla = new JPanel(null);
    cont_vertabla.setPreferredSize(new Dimension(750,600));
    cont_vertabla.add(sp_vertabla);
    f_vertabla.setContentPane(cont_vertabla);
    f_vertabla.pack();
    f_vertabla.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    //f_vertabla.setResizable(false);
    f_vertabla.setVisible(true);
    f_vertabla.addWindowListener(this);
}

this is the way the JTable looks JTable displaying the same row


The row listed in the above pic, is the last one in the mysql table

1
  • You're using a single instance Vector and simply updating it's contents, instead you should be using a new Vector for each row Commented Dec 11, 2015 at 7:30

1 Answer 1

2

Try adding the line

Vector row = new Vector();

inside the while loop.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Parasau that was the problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.