user1937623
user1937623

Reputation: 15

JTable: adding one dimensional array as rows

String[] names = app1.getSimplifiedMovieRecordsList();

I have this string array with names of movies. I need to display this as the rows of a table having one column. Can someone please tell me how to do this.

Upvotes: 0

Views: 1358

Answers (1)

copeg
copeg

Reputation: 8348

need to display this as the rows of a table having one column

Presuming you mean JTable as your title suggests:

  1. Convert the String array into a 2D array with names.length rows, and 1 column.
  2. Create the column name array (length 1).
  3. Create a JTable based upon the data

For example:

Object[][] tableData = new Object[names.length][1];
for ( int i = 0; i < names.length; i++ ){
    tableData[i][0] = names[i];
}
Object[] colnames = {"MyColumnNmae"};
JTable table = new JTable(tableData, colnames);

You can alternatively:

  1. Create a JTable based upon a DefaultTableModel constructed with the same data (facilitates modifications)
  2. Use the String array directly to create a JList

Upvotes: 2

Related Questions