I tried to find the right answers, but so far nothing has helped. I am completely new to GUI programming for Java, in fact, for java itself. I have, however, executives to understand JPA how to extract, insert, and delete using JPA.
Now I want the data in my database to display in JTable.
I currently have the following mySQL table (which I want to show in JTable
games Id PK int title publisher Genre ReleaseDate
Regarding coding, I have successfully extracted the data contained in the table using the following:
public List<Game> getGames(){
List<Game> games;
try{
TypedQuery<Game> selectGamesQuery = entityManager.createQuery("SELECT g FROM Game g", Game.class);
games = selectGamesQuery.getResultList();
} catch(Exception e) {
System.out.println(e);
}
return games;
}
This successfully returns a list of games in which I can iterate over the trough.
Then, in my opinion, I have the following
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
tblGames = new JTable(new tblGamesModel());
tblGames.setShowVerticalLines(true);
tblGames.setShowHorizontalLines(true);
tblGames.setFillsViewportHeight(true);
scrollPane.setViewportView(tblGames);
Which of us leads us to the model of the table I'm stuck in.
public class tblGamesModel extends AbstractTableModel {
private GameRepository gameRepository;
private List<Game> games;
public tblGamesModel(){
gameRepository = new GameRepository();
games = gameRepository.getGames();
}
private static final long serialVersionUID = 1L;
@Override
public int getColumnCount() {
return 0;
}
@Override
public int getRowCount() {
return games.size();
}
@Override
public Object getValueAt(int arg0, int arg1) {
return null;
}
}
, , , . .
, , , , .
, , .. , .