Jak z listy typu LinkedList stworzyć JTable ?
Tutaj próbuję przerobić przykład znaleziony w necie, żeby wczytać wszystkie trzeba użyć pętli. Pytanie dlaczego to nie wyświetla nagłówków (widać na załączniku). Pytanie nr dwa jak napiszę tak: String[] columnNames = {"Tytuł", "Autor","Wydawnictwo","Rok","Opis","ISBN"}; czyli dodam jeszcze jedną kolumnę to wtedy program się nie włączy.
KomunikacjaDB kDB = new KomunikacjaDB(ustawienia, sciezkaDoBazy);
List<CDane> lista = new LinkedList<CDane>();
lista = kDB.wczytajWszystkieDane();
String[] columnNames = {"Tytuł Autor","Wydawnictwo","Rok","Opis","ISBN"};
Object[][] data = { //dlaczego to jest tablicą dwuwymiarową ?
{lista.get(0).getKsiazka().getTytul(), lista.get(0).getAutor().getImieINazwisko(),lista.get(0).getKsiazka().getWydawnictwo(),
lista.get(0).getKsiazka().getRok(), lista.get(0).getKsiazka().getOpis(), lista.get(0).getKsiazka().getIsbn()},
{"John", "Doe",
"Rowing", new Integer(3), new Boolean(true)},
{"Sue", "Black",
"Knitting", new Integer(2), new Boolean(false)},
{"Jane", "White",
"Speed reading", new Integer(20), new Boolean(true)},
{"Joe", "Brown",
"Pool", new Integer(10), new Boolean(false)}
};
JTable table = new JTable(data, columnNames);
Znalazłem w necie, że trzeba zrobić model, niżej jakiś przykład. Tylko nie za bardzo wiem jak mam go zmienić, żeby pasował pod moją listę. Czy ten model jest na prawdę konieczny i nie ma innego prostszego sposobu ?
import java.util.ArrayList;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
public class TableModelPerson extends AbstractTableModel
{
/**
* List of event listeners. These listeners wait for something to happen
* with the table so that they can react. This is a must!
*/
private ArrayList<TableModelListener> listeners = new ArrayList<TableModelListener>();
private PersonList iPersonList;
private String[] iColumnNames =
{
"Identifier",
"Lastname",
"Firstname"
};
public TableModelPerson( PersonList pPersonList )
{
this.iPersonList = pPersonList;
}
/**
* Returns the number of columns in the model. A JTable uses this method to
* determine how many columns it should create and display by default.
*
* @return the number of columns in the model
* @see #getRowCount
*/
@Override
public int getColumnCount( )
{
return 3;
//return iColumnNames.size( );
}
/**
* Returns the name of the column at columnIndex. This is used to initialize
* the table's column header name. Note: this name does not need to be
* unique; two columns in a table can have the same name.
*
* @param columnIndex
* the index of the column
* @return the name of the column
*/
public String getColumnName( int columnIndex )
{
return iColumnNames[ columnIndex ];
}
/**
* Returns the most specific superclass for all the cell values in the
* column. This is used by the JTable to set up a default renderer and
* editor for the column.
*
* @param columnIndex
* the index of the column
* @return the common ancestor class of the object values in the model.
*/
@Override
public Class<?> getColumnClass(int columnIndex)
{
switch (columnIndex)
{
case 0: return String.class;
case 1: return String.class;
case 2: return String.class;
}
return null;
}
/**
* Returns the number of rows in the model. A JTable uses this method to
* determine how many rows it should display. This method should be quick,
* as it is called frequently during rendering.
*
* @return the number of rows in the model
* @see #getColumnCount
*/
@Override
public int getRowCount( )
{
return this.iPersonList.size( );
}
/**
* Returns true if the cell at rowIndex and columnIndex is editable.
* Otherwise, setValueAt on the cell will not change the value of that cell.
*
* @param rowIndex
* the row whose value to be queried
* @param columnIndex
* the column whose value to be queried
* @return true if the cell is editable
* @see #setValueAt
*/
@Override
public boolean isCellEditable(int rowIndex, int columnIndex)
{
if( columnIndex == 0 )
{
return false;
}
else
{
return true;
}
}
/**
* Returns the value for the cell at columnIndex and rowIndex.
*
* @param rowIndex
* the row whose value is to be queried
* @param columnIndex
* the column whose value is to be queried
* @return the value Object at the specified cell
*/
@Override
public Object getValueAt( int row, int col )
{
switch(col)
{
case 0 : return this.iPersonList.get( row ).getIdentifier( );
case 1 : return this.iPersonList.get( row ).getLastname( );
case 2 : return this.iPersonList.get( row ).getFirstname( );
default : throw new RuntimeException("no such column");
}
}
/**
* This method is used for adding a new row into your JTable
*
* @param checked
* first column value
* @param oldName
* second column value
* @param newName
* third column value
*/
public void addRow( String pIdentifier, String pLastname, String pFirstname )
{
this.iPersonList.add( pIdentifier, pLastname, pFirstname );
TableModelEvent event = new TableModelEvent( this, this.iPersonList.size() - 1, this.iPersonList.size() - 1, TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT );
for(TableModelListener l: listeners)
{
l.tableChanged(event);
}
}
/**
* This method is used for adding a new row into your JTable
*
* @param checked
* first column value
* @param oldName
* second column value
* @param newName
* third column value
*/
public void updateRow( int pIndex, String pIdentifier, String pLastname, String pFirstname )
{
this.iPersonList.set( pIndex, pIdentifier, pLastname, pFirstname );
TableModelEvent event = new TableModelEvent( this, pIndex, pIndex, TableModelEvent.ALL_COLUMNS, TableModelEvent.UPDATE );
for(TableModelListener l: listeners)
{
l.tableChanged(event);
}
}
/**
* This method is used for adding a new row into your JTable
*
* @param checked
* first column value
* @param oldName
* second column value
* @param newName
* third column value
*/
public void moveUp( int pIndex )
{
this.iPersonList.swap( pIndex, (pIndex - 1) );
TableModelEvent event = new TableModelEvent( this, pIndex, (pIndex - 1), TableModelEvent.ALL_COLUMNS, TableModelEvent.UPDATE );
for(TableModelListener l: listeners)
{
l.tableChanged(event);
}
}
/**
* This method is used for adding a new row into your JTable
*
* @param checked
* first column value
* @param oldName
* second column value
* @param newName
* third column value
*/
public void moveDown( int pIndex )
{
this.iPersonList.swap( pIndex, (pIndex + 1) );
TableModelEvent event = new TableModelEvent( this, pIndex, (pIndex + 1), TableModelEvent.ALL_COLUMNS, TableModelEvent.UPDATE );
for(TableModelListener l: listeners)
{
l.tableChanged(event);
}
}
/**
* This method is used for adding a new row into your JTable
*
* @param checked
* first column value
* @param oldName
* second column value
* @param newName
* third column value
*/
public void removeRow( int pIndex )
{
this.iPersonList.remove( pIndex );
TableModelEvent event = new TableModelEvent( this, pIndex, pIndex, TableModelEvent.ALL_COLUMNS, TableModelEvent.DELETE );
for(TableModelListener l: listeners)
{
l.tableChanged(event);
}
}
/**
* Adds a listener to the list that is notified each time a change to the
* data model occurs.
*
* @param l
* the TableModelListener
*/
@Override
public void addTableModelListener( TableModelListener pTableModelListener )
{
if( listeners.contains( pTableModelListener ) )
{
return;
}
listeners.add( pTableModelListener );
}
/**
* Removes a listener from the list that is notified each time a change to
* the data model occurs.
*
* @param l
* the TableModelListener
*/
@Override
public void removeTableModelListener(TableModelListener l)
{
listeners.remove(l);
}
}