Java JTable binding via JPA

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() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public int getRowCount() {
    // TODO Auto-generated method stub
    return games.size();
}

@Override
public Object getValueAt(int arg0, int arg1) {
    // TODO Auto-generated method stub


    return null;
}

}

, , , . .

, , , , .

, , .. , .

+3
2

- - :

@Override
public int getColumnCount() {
    return 5;
}

...

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
    Game game = games.get(rowIndex);
    switch (columnIndex) {
        case 0:
            return game.getId();
        case 1:
            return game.getTitle();
        case 2:
            return game.getPublisher();
        case 3:
            return game.getGenre();
        case 4:
            return game.getReleaseDate();
    }
    return null;
}

- - :

enum GameTableColumn {
    ID, TITLE, PUBLISHER, GENRE, RELEASE_DATE;
}

, GameTableColumn.values ​​() [columnIndex].

- tblGamesModel - Java, . Java- GameTableModel. (, "tbl" ) .

, , . Swing , , , . getGames() retrieveGames(). GamesRepository . JPA . .

+5

setter. :

public class TblGamesModel extends AbstractTableModel {

    private static final String[] COLUMNS = {"id", "title",
    ...........
    private static final int COL_ID = 0;
    private static final int COL_TITLE = 1;

    private List<Game> list;  //list that is injected via constructor or setter method

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

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

    public Object getValueAt(int rowIndex, int columnIndex) {
        Game game = list.get(rowIndex);
        switch (columnIndex) {
            case COL_ID:
                return game.getId();
            ........
        }
    }

    public String getColumnName(int column) {
        return COLUMNS[column];
    }
+2

Source: https://habr.com/ru/post/1795971/


All Articles