在下面的示例中,窗口显示一个表,其中包含窗口本身的宽度。当窗口调整大小时,它的宽度值反映了当前的真实性。
这是怎么回事? 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);
}
});
}
}
答案 0 :(得分: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)
重绘帧将最终触发表的重绘。表模型将用于获取重绘某个单元格所需的值。
答案 1 :(得分:2)
只需以这种方式更改getValueAt()
方法,您就可以轻松找到方法调用链中发生的情况:
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
throw new RuntimeException("Trace!");
}