0

I have a LinkedHashSet of Book objects. The Book objects have the following fields:

private int id;
private String author = "";
private String title = "";
private boolean isRead;
private String dateStamp = "";
private static int counter = 0;

I want them to go into my JTable which has the following columns:

String [] columnNames = {"ID","Title", "Author", "Status", "Date Read"};

How can I do this? And is it possible to have the isRead field being editable through a checkbox in the table?

Laf
  • 7,965
  • 4
  • 37
  • 52
user1808348
  • 79
  • 1
  • 1
  • 4
  • 1
    how about iterating over your linkedHashSet and populating your Jtable ?? – PermGenError Nov 08 '12 at 14:31
  • public static void LibToArray(){ rowData = new Object[Book.bookList.size()][5]; int i = 0; Iterator it = Book.bookList.iterator(); while(it.hasNext()){ Book book1 = (Book)it.next(); rowData[i][0] = (Integer)book1.getId(); rowData[i][1] = book1.getTitle(); rowData[i][2] = book1.getAuthor(); rowData[i][3] = (Boolean)book1.getIsRead(); rowData[i][4] = book1.getDateStamp(); i++; } } – user1808348 Nov 09 '12 at 06:06

3 Answers3

1

You need to have a class that extends AbstractTableModel. This class should use your LinkedHashSet as the data source for your table. The basic implementation provided by AbstractTableModel should suit most of your needs. If not, then you can override the methods you need to customize.

This tutorial should help you understand how JTable objects work.

Laf
  • 7,965
  • 4
  • 37
  • 52
1

This is a sample model I have created for the table.

public class CustomModel extends AbstractTableModel {

private Object[] colNames ={"ID","Title", "Author", "Status", "Date Read"};
private LinkedHashSet<CustomClass> data; 

public TableModelTop() {
    this.data = getDataForDropList();
}

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

public int getColumnCount() {
    return colNames.length;
}

@Override
public String getColumnName(int columnIndex) {
    return (String) colNames[columnIndex];
}

@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
    // Set Values here;
}

public Object getValueAt(int rowIndex, int columnIndex) {
    // Get row Values here;
}

@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
    return true;
}

@Override
public Class<?> getColumnClass(int columnIndex) {
    // Depending on the type of the column. Return data type;
}


/**
 * Populate the data from here.
 * @return LinkedHashSet<CustomClass>
 */
private LinkedHashSet<CustomClass> getDataForDropList() {
    LinkedHashSet<CustomClass> modelList = new  LinkedHashSet<CustomClass>();
    for(int i = 0; i< 5; i++) {

    // Create custom Object and add them to the LinkedHashSet.
            // Create a CustomClass object and add it to the LinkedHashSet
             modelList.add(customClassObject);
    }
           // Finally return the llinkedhashset
    return modelList;
}
}

After this just call the table model and assign this to the JTable.

JTable table = new JTable(new CustomModel());
Amarnath
  • 8,736
  • 10
  • 54
  • 81
  • 1
    I've suggested a way to implement `getValueAt()` [here](http://stackoverflow.com/a/13301413/230513). – trashgod Nov 09 '12 at 02:52
1

As a concrete example of using AbstractTableModel, you can leverage the toArray() method inherited by LinkedHashSet to simplify the implementation of getValueAt(), as shown in this related EnvTableTest. The default JTable renderer and editor use JCheckBox for TableModel elements of type Boolean.class, as shown in this example.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045