In an example below a window displays a table, containing window itself width. When window is resized, the value of it's width is reflecting the current truth.
How this can be? How Swing informed a table, that it should rerequest model? Or maybe model is receiving information that value was changed?
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);
}
});
}
}
JTable is just a view which means it does not hold any values. A will generate an exception for you to let you see the whole trace:
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)
Repaint of the frame will eventually trigger repaint of the table. Table model will be used to get a value needed to repaint certain cell.