Oliver Muchai
Oliver Muchai

Reputation: 279

Creating a JTable

I've been trying to create a table as follows:

public class SearchArray {
    public String[] columnNames = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian"};
    public Object[] data = {"First Name", "Last Name", "Sport", "# of Years",};

    JTable table;

    JTable search() {
        table = new JTable(data, columnNames);
        return table;
    }
}

I, however, keep getting a "no suitable constructor found for JTable(Object[], String[]".

I'm not sure what it is that I'm getting wrong. I'd really appreciate some help. Thank you in advance.

Upvotes: 1

Views: 5667

Answers (4)

Lan
Lan

Reputation: 5

It maybe because the values if Object[] data doesn't match up to the reuqired values of String [] columnNames

public class SearchArray { public String[] columnNames = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian"}; public Object[] []data = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian"}; //two [] [] are required for data of the column //Add another data to Object[] data to fulfill the needed amount of data for String[] columnNames

JTable table;

JTable search() {
    table = new JTable(data, columnNames);
    return table;
}

}

Upvotes: 0

Vijay
Vijay

Reputation: 1034

JTable constructor requires two dimensional array for the data and you are passing one dimensional array as a parameter. Look JTable Doc for more details.

Two dimensional array should be initiated like this

  Object[][] data = {{"First Name", "Last Name", "Sport", "# of Years",""}};

Upvotes: 2

Suresh Atta
Suresh Atta

Reputation: 121998

Because there is no constructor for Jtable with provided args.

Actual construtor is you are trying to call is JTable(Object[][] rowData, Object[] columnNames).

which allows Object[][]

but your data is of type Object[]

Upvotes: 1

MadProgrammer
MadProgrammer

Reputation: 347204

There is no constructor for JTable that takes Object[], Object[], take a look at the JavaDocs

Instead you could use...

public String[] columnNames = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian"};
public Object[][] data = {{"First Name", "Last Name", "Sport", "# of Years", "No"}};

JTable table;

JTable search() {
    table = new JTable(data, columnNames);
    return table;
}

You may also want to take a look at How to use tables for more details and options

Upvotes: 1

Related Questions