1

在下面的示例中,窗口显示了一个表格,其中包含窗口本身的宽度。当窗口调整大小时,它的宽度值反映了当前的事实。

这怎么可能?Swing 如何通知表,它应该重新请求模型?或者模型可能正在接收值已更改的信息?

public class JTableDynamicUpdate extends JFrame {



private AbstractTableModel tableModel = new AbstractTableModel() {

    private String[] columnNames = new String[] {"Parameter", "Value"};

    public String getColumnName(int column) {
        return columnNames[column];
    };

    @Override
    public int getRowCount() {
        return 1;
    }

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

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if( rowIndex == 0 ) {
            if( columnIndex == 0 ) {
                return "Window width:";
            }
            else if( columnIndex==1) {
                return getSize().width;
            }
        }
        throw new IndexOutOfBoundsException();
    }

};

private JTable table = new JTable(tableModel);

private JScrollPane tableScroll = new JScrollPane(table);

private Container contentPane = getContentPane();
{
    contentPane.setLayout(new BorderLayout());
    contentPane.add(tableScroll, BorderLayout.CENTER);

    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {

            JTableDynamicUpdate frame = new JTableDynamicUpdate();
            frame.pack();
            frame.setVisible(true);

        }
    });
}
}
4

2 回答 2

2

JTable 只是一个视图,这意味着它不包含任何值。A 将为您生成一个异常,让您查看整个跟踪:

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException
at TestClass$1.getValueAt(TestClass.java:36)
at javax.swing.JTable.getValueAt(JTable.java:2686)
at javax.swing.JTable.prepareRenderer(JTable.java:5703)
at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2072)
at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:1974)
at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1770)
at javax.swing.plaf.ComponentUI.update(ComponentUI.java:143)
at javax.swing.JComponent.paintComponent(JComponent.java:752)
at javax.swing.JComponent.paint(JComponent.java:1029)
at javax.swing.JComponent.paintChildren(JComponent.java:862)
at javax.swing.JComponent.paint(JComponent.java:1038)
at javax.swing.JViewport.paint(JViewport.java:747)
at javax.swing.JComponent.paintChildren(JComponent.java:862)
at javax.swing.JComponent.paint(JComponent.java:1038)
at javax.swing.JComponent.paintChildren(JComponent.java:862)
at javax.swing.JComponent.paint(JComponent.java:1038)
at javax.swing.JComponent.paintChildren(JComponent.java:862)
at javax.swing.JComponent.paint(JComponent.java:1038)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:567)
at javax.swing.JComponent.paintChildren(JComponent.java:862)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5131)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1491)
at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1422)
at javax.swing.RepaintManager.paint(RepaintManager.java:1225)
at javax.swing.JComponent.paint(JComponent.java:1015)
at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
at java.awt.Container.paint(Container.java:1778)
at java.awt.Window.paint(Window.java:3379)

框架的重绘最终将触发表格的重绘。表格模型将用于获取重绘某个单元格所需的值。

于 2012-10-14T00:31:06.290 回答
2

只需以这种方式更改getValueAt()方法,您就可以轻松地从方法调用链中找到发生了什么:

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
        throw new RuntimeException("Trace!");
}
于 2012-10-14T00:42:04.013 回答