2013-07-01 32 views
0

我使用第一个内部类。我正尝试访问在外部类中声明的变量table,内部类MyTableModel。但netbeans显示错误 - Cannot find symbol从内部类访问外部类成员

这是完整的代码。

Import Statements 
public class TableDemo extends JPanel { 
    private boolean DEBUG = true; 

    public TableDemo() { 
     super(new GridLayout(1,0)); 

     JTable table = new JTable(new MyTableModel()); 
     table.setPreferredScrollableViewportSize(new Dimension(500, 70)); 
     table.setFillsViewportHeight(true); 

     JScrollPane scrollPane = new JScrollPane(table); 

     add(scrollPane); 
    } 

    class MyTableModel extends AbstractTableModel { 
     private String[] columnNames = {"First Name","Last Name","Sport","# of Years","Dada","Vegiterian"}; 
     private Object[][] data = { 
     {"Kathy", "Smith", 
     "Snowboarding", new Integer(5), new Boolean(false),new Boolean(false)}, 

     }; 

     public int getColumnCount() { 
      return columnNames.length; 
     } 

     public int getRowCount() { 
      return data.length; 
     } 

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

     public Object getValueAt(int row, int col) { 
      return data[row][col]; 
     } 
     public Class getColumnClass(int c) { 
      return getValueAt(0, c).getClass(); 
     } 

     public boolean isCellEditable(int row, int col) { 
      if (col < 2) { 
       return false; 
      } else { 
       return true; 
      } 
     } 

     public void setValueAt(Object value, int row, int col) { 

     } 

     private void printDebugData() { 

      TableColumn column = null; 
      for (int i = 0; i < 5; i++) { 
      column = table.getColumnModel().getColumn(i); 
      if (i == 2) { 
       column.setPreferredWidth(100); //third column is bigger 
      } else { 
       column.setPreferredWidth(50); 
    } 
} 
      int numRows = getRowCount(); 
      int numCols = getColumnCount(); 

      for (int i=0; i < numRows; i++) { 
       System.out.print(" row " + i + ":"); 
       for (int j=0; j < numCols; j++) { 
        System.out.print(" " + data[i][j]); 
       } 
       System.out.println(); 
      } 
      System.out.println("--------------------------"); 
     } 
    } 

    private static void createAndShowGUI() { 
     JFrame frame = new JFrame("TableDemo"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     TableDemo newContentPane = new TableDemo(); 
     newContentPane.setOpaque(true); //content panes must be opaque 
     frame.setContentPane(newContentPane); 

     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGUI(); 
      } 
     }); 
    } 
} 

,这是在我收到错误的代码 - 我收到错误,如column = table.getColumnModel().getColumn(i); - - variable table is not found

请帮

for (int i = 0; i < 5; i++) { 
      column = table.getColumnModel().getColumn(i); 
      if (i == 2) { 
       column.setPreferredWidth(100); //third column is bigger 
      } else { 
       column.setPreferredWidth(50); 
    } 

INT线。

回答

0

您在构造函数中,而不是作为一个实例成员定义变量。构造函数代码结束后,该变量超出了范围。你需要做的是这样的:

public class TableDemo extends JPanel { 
    private boolean DEBUG = true; 
    private JTable table; 

    public TableDemo() { 
     super(new GridLayout(1,0)); 

     table = new JTable(new MyTableModel()); 
     table.setPreferredScrollableViewportSize(new Dimension(500, 70)); 
     table.setFillsViewportHeight(true); 

     JScrollPane scrollPane = new JScrollPane(table); 

     add(scrollPane); 
    } 

    // Rest of code 
+0

请不,这不是一个类('static')成员,你的意思,但一个实例成员。 –

+0

你说得对。但我认为这个例子足够描述。无论如何,我会编辑。 – Avi

0

您在构造函数中仅声明变量本地。构造函数消失后(超出范围),变量消失了。要正确地做到这一点,你应该在一个字段定义它:

public class TableDemo extends JPanel { 

    private boolean DEBUG = true; 
    private JTable table; //define here 

    public TableDemo() { 
     super(new GridLayout(1,0)); 
     table = new JTable(new MyTableModel()); //remove Type here 
0

你需要一个实例变量在一个内部类访问它。你的变量对构造函数只有局部范围,因此构造函数之外没有任何东西可以找到它。

你的代码更改为:

public class TableDemo extends JPanel { 
    private JTable table; 

    public TableDemo() { 
     table = new JTable(new MyTableModel()); 
     //more code 
    } 
    // more code 
}