0
String[] columnNames = {"Type", "Employee ID", "First/Last Name","DOB", "Gender", "HIre Date", "OnCall", "BaseSalary", "Commission/Hourly Rate"};
    Object[][] data = new Object[10][9];
table = new JTable(data,columnNames);

public ArrayList<Person> employeeList = new ArrayList<Person>();
public ArrayList<Client> clientList = new ArrayList<Client>();
public ArrayList<Stock> stockList = new ArrayList<Stock>();

I'm trying to create a JTable with information inside a array-list. I tried something along the lines of.

data[0][0] = "Account";
data[0][1] = 1; //int
data[0][2] = "Name";
data[0][3] = dob; //Date class
data[0][4] = gender; //Enum 
data[0][5] = hire //Date class
data[0][6] = true; //boolean
data[0][7] = 125.23; //double
data[0][8] =  0.015; //double

Which didn't work out really well, I try google but most of the examples have pre-made arrays so didn't help much.

5
  • It's unclear why it did not work, what were you expecting and what happened instead? Commented Nov 9, 2011 at 2:34
  • I was actually expecting it to work but apparently it didn't since the code I'm working with the person before had a exception handling and threw a error Commented Nov 9, 2011 at 2:37
  • Strangely enough now I create the table accept none of the data is going in at all... Commented Nov 9, 2011 at 2:56
  • Are you putting the objects in data before the JTable creation? Commented Nov 9, 2011 at 3:05
  • Yea, which I think might be the issue, I ended up just making a empty table, and just adding rows which seem easier. Commented Nov 9, 2011 at 3:49

2 Answers 2

1

I think your problem is with the data array. Arrays in java are not dynamic. Defining

Object[][] data = {};

you are creating an array of length 0.

You'd have to be able to know their lengths beforehand and use them in the creation or use the List's toArray method.

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

2 Comments

could I specify the size such as Object[row][col] data;
No. Object[][] data = new Object[rows][cols];
1

The JTable model documentation is confusingly in it's simplistic explanations. It's almost always a mistake to pass in data to the table constructor. You should implement your own table model that is derived form AbstractTableModel, and pass that model object into the constructor for the table.

Then your table model, can just have a private member variable like

public ArrayList<Person> employeeList = new ArrayList<Person>();

then you just need to implement a few methods, like

public int getRowCount() {
    return emplyeeList.size();
}

and such as

public Object getValueAt(int rowIndex, int columnIndex) {
    Person p = employeeList.get(rowIndex);
    switch (columnIndex) {
        case 1: 
              return p.getName();
        ....
    }
}

etc.

It makes your programming much simpler as the model uses your natural data structures to hold the data.

Comments

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.