2012-10-13 152 views
1

在下面的例子中,一個窗口顯示一個包含窗口本身寬度的表格。當窗口被調整大小時,它的寬度值反映了當前的真相。TableModel如何知道值已更改?

這怎麼可能呢? 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

如果您在「getValueAt」功能(如'系統添加一個簡單的日誌記錄調用。 out.println(「value」)')你會看到什麼時候swing查詢你的函數。無論什麼時候發生變化,它都可能會調用該值,包括組件的大小。 – mercutio

回答

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) 

重繪將最終觸發表重繪。表模型將用於獲取重繪某個單元格所需的值。

2

只要改變這樣的getValueAt()方法,你可以很容易地找到什麼是從法的連鎖發生的來電:

@Override 
public Object getValueAt(int rowIndex, int columnIndex) { 
     throw new RuntimeException("Trace!"); 
} 
相關問題