2012-12-14 29 views
1

我的應用程序的JFrame的邏輯後的樣子:的JTable內的ScrollPane中只顯示了調整

public Table() { 
    super("Chess"); 
    thisFrame = this; 
    tableMenuBar = new JMenuBar(); 
    populateMenuBar(); 
    setJMenuBar(tableMenuBar); 
    getContentPane().setLayout(new BorderLayout()); 
    chessBoard = new Board(new StandardBoardConfigurator()); 
    gamePanel = new GameHistoryPanel(); 
    chatPanel = new ChatPanel(); 
    takenPiecesPanel = new TakenPiecesPanel(); 
    boardPanel = new BoardPanel(chessBoard); 
    gameProgress = 1; 
    highlightLegalMoves = true; 
    moveLog = new ArrayList<Move>(); 
    gameOver = false; 
    getContentPane().add(takenPiecesPanel, BorderLayout.WEST); 
    getContentPane().add(boardPanel, BorderLayout.CENTER); 
    getContentPane().add(gamePanel, BorderLayout.EAST); 
    getContentPane().add(chatPanel, BorderLayout.SOUTH); 
    // Make sure we have nice window decorations. 
    setDefaultLookAndFeelDecorated(true); 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 
    setSize(OUTER_FRAME_DIMENSION); 
    pack(); 
    setVisible(true); 
} 

和含有JTable的是「GameHistoryPanel」,裏面有這個,因爲它的構造邏輯JPanel

public GameHistoryPanel() { 
     this.setLayout(new BorderLayout()); 
     this.model = new DataModel(); 
     this.table = new JTable(model); 
     this.table.setRowHeight(15); 
     final JScrollPane scrollPane = new JScrollPane(this.table); 
     scrollPane.setColumnHeaderView(table.getTableHeader()); 
     scrollPane.setPreferredSize(HISTORY_PANEL_DIMENSION); 
     this.add(scrollPane, BorderLayout.CENTER); 
     this.currentRow = 0; 
     this.currentColumn = 0; 
     this.setVisible(true); 
    } 

GamePanel具有這就要求setValueAt以下更新程序,只要一動即發:

public void increment(final Board board, 
          final Move move) { 
     this.model.setValueAt(move, currentRow, currentColumn); 
     if(board.currentPlayer().getAlliance() == Alliance.WHITE) { 
      currentColumn++; 
     } else if (board.currentPlayer().getAlliance() == Alliance.BLACK) { 
      currentRow++; 
      currentColumn = 0; 
     } 
    } 

啓動遊戲後,GamePanel灰顯。當我調整它垂直時,它突然出現與所有正確的值。我不明白爲什麼。我注意到調整大小會導致getValueAt被調用很多次。有人能幫助我理解這一點嗎?

編輯2:如果我加入這一行:

 this.model.fireTableDataChanged(); 

遞增,似乎很好地工作。我完全糊塗了......

編輯:這裏是我的TableModel類:

private static class DataModel extends AbstractTableModel { 

     private static final String[] names = {"White", "Black"}; 
     private final List<Row> values; 

     public DataModel() { 
      values = new ArrayList<Row>(); 
     } 

     @Override 
     public int getRowCount() { 
      return values.size(); 
     } 

     @Override 
     public int getColumnCount() { 
      return names.length; 
     } 

     @Override 
     public Object getValueAt(int row, int col) { 
      final Row currentRow = values.get(row); 
      if(col == 0) { 
       return currentRow.getWhiteMove(); 
      } else if (col == 1) { 
       return currentRow.getBlackMove(); 
      } 
      return null; 
     } 

     @Override 
     public void setValueAt(Object aValue, int row, int col) { 
      final Row currentRow; 
      if(values.size() <= row) { 
       currentRow = new Row(); 
       values.add(currentRow); 
      } else { 
       currentRow = values.get(row); 
      } 
      if(col == 0) { 
       currentRow.setWhiteMove((Move) aValue); 
      } else if(col == 1) { 
       currentRow.setBlackMove((Move)aValue); 
      } 
      this.fireTableCellUpdated(row, col); 
     } 

     @Override 
     public Class<?> getColumnClass(int col) { 
      return Move.class; 
     } 

     @Override 
     public String getColumnName(int col) { 
      return names[col]; 
     } 
    } 
} 
+0

您調整哪個組件以使JTable出現? –

+0

遊戲的JFrame –

+0

如果有幫助,我很樂意將您指向我的gitrepo? –

回答

2

如果我將這一行,this.model.fireTableDataChanged()increment(),它似乎工作正常。

你在​​實施setValueAt()是有缺陷的,因爲它可能會增加的實例模型,而只是調用fireTableCellUpdated()單個rowcol。您需要觸發適合實際修改的事件。

+0

另請參閱此相關的[答案](http://stackoverflow.com/a/13863334/230513)。 – trashgod

+0

感謝您的回答。我懷疑這樣的事情。我還沒有弄清楚還有什麼需要修改的。只要我能理清如何修正impl,我會將其標記爲正確的答案。 –

+0

@AmirAfghani:如果你更新你的問題,請給我打電話。我發現創建[sscce](http://sscce.org/)可以更容易地孤立地研究這些問題。您可能會發現在[tag:JTable]標記中檢查我以前的答案會很有幫助,其中許多標記包含來自用戶kleopatra的相關評論。 – trashgod

1

嘗試在任的JPanel,JScrollPane的,JTable中

我的猜測稱

.invalidate(); 
.repaint(); 

是作爲最後一行添加increment()

JTable.invalidate(); 
JTable.repaint(); 

應該足夠了。如果沒有,請檢查每個組件。

+0

那沒做到。 –

+0

@AmirAfghani,如果找到合適的解決方案,我會刪除它。請告訴我。 –

+0

謝謝 - 我會讓你知道的。 –