简体   繁体   English

JTable与ScrollPane行为异常

[英]JTable with a ScrollPane misbehaving

I have a JPanel component with a JTable inside of it. 我有一个带有JTableJPanel组件。 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. 一旦我尝试使用scrollPane方法,该表将根本不会呈现。 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();
        }
    }

It appears that you are using a JTable as view of a TableModel in which each cell exists in one of two states. 看来您正在使用JTable作为TableModel视图,其中每个单元格都处于两种状态之一。 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. 特别地,在调用方法validate()repaint() 应该要求。 Their presence suggests that you are altering the view without the model's knowledge, which could explain the anomaly seen. 它们的存在表明您在不知道模型的情况下更改了视图,这可以解释所看到的异常。

Try 尝试

JScrollPane scrollPane = new JScrollPane(table);
this.add(scrollPane);

This may be stating the obvious, but remember that you can only add a JComponent to a container once . 这可能说明明显,但要记住,你只能添加一个JComponent到容器一次

After

this.setLayout(new BorderLayout());
this.model = new DataModel();
this.table = new JTable(model);

either you 要么你

this.add(table, BorderLayout.CENTER);
// note that table-headers should not be added explicitly
// commented out: this.add(table.getTableHeader(), BorderLayout.NORTH);

or you 或者你

JScrollPane scrollPane = new JScrollPane(table);
this.add(scrollPane);

but trying to do both will result in problems. 但是尝试两者都会导致问题。

I recommend using Swingx's JXTable, if at all possible. 我建议尽可能使用Swingx的JXTable。 Much more flexible, and out-of-the-box column sorting and hiding/reordering. 更灵活,开箱即用的列排序和隐藏/重新排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM