user667430
user667430

Reputation: 1547

Java jtable populate from list

I am trying to populate a jtable i have with data from a oracle table. I can successfully get the information from the table and into a list but how do i display this information in a jtable.

This is what i have

public class MainMenu extends javax.swing.JFrame {

    private DatabaseConnector dbConnector = new DatabaseConnector();

    DefaultTableModel userTableModel = new DefaultTableModel();

    public void refreshCustomersList() throws SQLException, ClassNotFoundException {

        UserBeanList userList = dbConnector.getUserData();

        userListModel.clear();

        for (int i = 0; i < userList.size(); i++) {
            UserBean userBean = userList.getUserBeanAt(i);

            String[] data = new String[3];

            data[0] = userBean.getCustomerID();
            data[1] = userBean.getFirstName();
            data[2] = userBean.getLastName();

            userTableModel.addRow(data);
        }
        tableCustomers.setModel(userTableModel);
    }

Am i doing something wrong cause why table is just all greyed out?

Upvotes: 0

Views: 2544

Answers (1)

Robin
Robin

Reputation: 36621

You forgot to specify the number of columns for your table model. I would suggest to use this constructor instead:

DefaultTableModel userTableModel = 
  new DefaultTableModel( new Object[]{ "Customer id", "First name", "Last name" }, 0 );

Upvotes: 3

Related Questions