JTable与ScrollPane行为异常 [英] JTable with a ScrollPane misbehaving

查看:76
本文介绍了JTable与ScrollPane行为异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JPanel组件,其中有一个JTable.当我按照下面的代码运行代码时,该表将正确呈现和更新.一旦尝试使用scrollPane方法,该表将根本无法呈现.谁能向我解释为什么?

I have a JPanel component with a JTable inside of it. When I run the code as it's written below, the table renders and updates correctly. As soon as I try to use the scrollPane approach, the table does not render at all. Can anyone explain to me why this is?

    private static class GameHistoryPanel extends JPanel {

        private DataModel model;
        private JTable table;
        private int currentRow;
        private int currentColumn;
        private final Dimension HISTORY_PANEL_DIMENSION = new Dimension(190,460);


        public GameHistoryPanel() {
            this.setLayout(new BorderLayout());
            this.model = new DataModel();
            this.table = new JTable(model);
            this.add(table.getTableHeader(), BorderLayout.NORTH);
            this.add(table, BorderLayout.CENTER);
//            JScrollPane scrollPane = new JScrollPane();
//            scrollPane.setViewportView(table);
//            this.add(scrollPane);
            setPreferredSize(HISTORY_PANEL_DIMENSION);
            this.currentRow = 0;
            this.currentColumn = 0;
        }

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

推荐答案

看来,您正在使用JTable作为TableModel的视图,其中每个单元格都处于两种状态之一.对单元格的可见更改应仅由对模型的更改导致 ,可以在准备单元格的

It appears that you are using a JTable as view of a TableModel in which each cell exists in one of two states. Visible changes to a cell should result only from a change to the model, which may be examined in preparing the cell's renderer. In particular, invoking the methods validate() and repaint() should not be required. Their presence suggests that you are altering the view without the model's knowledge, which could explain the anomaly seen.

这篇关于JTable与ScrollPane行为异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆