JTable.setValueAt () не отображает изменения указанной ячейки в GUI - PullRequest
0 голосов
/ 29 февраля 2020

У меня в основном есть часть gui с таблицей, содержащей данные, модель, представляющая эти данные, и контроллер, управляющий обоими. RepoController.addHit вызывается базовым контроллером, который запускает все. Таблица иногда будет меняться, но не то, что на самом деле нужно менять, и, исходя из моей отладки, это происходит далеко после вызова метода setValueAt (). Я смотрел на подобные проблемы, но не могу понять, как применить эти решения к моей проблеме

RepoConrtoller


import Repositories.HittingRepo;
import Views.HittingStatsView;

public class RepoController {

    private static final int[]  players = {1, 4, 8, 9, 11, 13, 15, 16, 18, 19, 21};
    private static final int[] hitResult = {'K', 'D', 'E'};
    public HittingRepo hitRepo;
    public HittingStatsView hitView;

    public RepoController() {
        hitRepo = new HittingRepo(players);
        hitView = new HittingStatsView(hitRepo.hittingStats);
    }

    public void addHit(int player, char result) {
        int iresult=0;
        if(result=='K')
            iresult=2;
        else if (result=='D')
            iresult=3;
        else if(result=='E')
            iresult=4;
        else 
            iresult=5;
        int iplayer = indexOfPlayer(player);
        hitRepo.updateStat(iplayer, iresult);
        hitView.setValueAt(hitRepo.hittingStats[iplayer][1], iplayer, iresult);
        hitView.setValueAt(hitRepo.hittingStats[iplayer][iresult], iplayer, iresult);
        hitView.setValueAt(hitRepo.hittingStats[iplayer][6], iplayer, iresult);

    }

    private int indexOfPlayer(int player){
        for(int i=0; i<players.length; i++) {
            if(players[i]==player)
                return i;
        }
        return 0;
    }

}

HittingRepo модель для данных, содержащихся


public class HittingRepo {

    public Integer[][] hittingStats = new Integer[11][7];
    //columns player[0], total hits[1], kills[2], continuations[3], errors[4], blocks[5], +/- [6]

    public HittingRepo(int[] players) {
        for(int i = 0; i<11; i++) {
            for(int j=0; j<7; j++) {
                if(j==0)
                    hittingStats[i][j] = players[i];
                else
                    hittingStats[i][j] = 0;
            }
        }
    }

    public void updateStat(int playerIndex, int result) {
        hittingStats[playerIndex][result]+=1;
        hittingStats[playerIndex][1]+=1;
        if(result==2)
            hittingStats[playerIndex][6]+=1;
        else if(result==4||result==5)
            hittingStats[playerIndex][6]-=1;
    }


}

HittingStatsView, класс расширяет JTable и добавляется во фрейм в другом месте

package Views;

import java.awt.Dimension;
import java.awt.Font;

import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;


import Controllers.BaseController;

public class HittingStatsView extends JTable {

    private static final String[] columns = { "Player#", "Hits", "Kills", "Conts", "Error", "Blocks", "+/-" };
    public DefaultTableModel model;

    public HittingStatsView(Integer[][] data) {
        super(new DefaultTableModel(data, columns));
        DefaultTableCellRenderer centerRender = new DefaultTableCellRenderer();
        centerRender.setHorizontalAlignment(JLabel.CENTER);
        for(int i=0; i<7; i++) {
            this.getColumnModel().getColumn(i).setCellRenderer( centerRender );
        }
        this.setRowHeight(40);
        model = (DefaultTableModel) this.getModel();
    }

}
...