从内部类访问外部类成员

时间:2013-07-01 13:21:58

标签: java jtable inner-classes

我正在使用Inner类作为第一个。我试图访问在内部类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();
            }
        });
    }
}

这是我收到错误的代码 -

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);
    }

行 - column = table.getColumnModel().getColumn(i);我收到的错误如 - variable table is not found

请帮忙。

3 个答案:

答案 0 :(得分:0)

您只在构造函数中声明变量 local 。构造函数消失后(超出范围),变量就消失了。要正确完成,您应该在字段中定义它:

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

答案 1 :(得分: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

答案 2 :(得分:0)

您需要一个实例变量才能从内部类访问它。你的变量只有构造函数的局部范围,因此构造函数之外的任何东西都找不到它。

将您的代码更改为:

public class TableDemo extends JPanel {
    private JTable table;

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