简体   繁体   中英

Access outer class member from inner class

I am using Inner class for the first. I am trying to access a variable table which is declared in outer class, in the inner class MyTableModel . But netbeans is showing the error- Cannot find symbol

This is the complete code.

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

and this is the code in which I am getting the error -

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 the line - column = table.getColumnModel().getColumn(i); I am getting the error like - variable table is not found

Please help.

You are declaring variable only locally in the constructor. After the constructor is gone (out of scope), the variable is gone. To do it properly, you should define it in a field:

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

You define the variable in the constructor and not as an instance member. After the constructor code ends, the variable is out of scope. You need to do it like this:

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

You need an instance variable to access it from an inner class. Your variable has only local scope to the constructor therefore nothing outside the constructor could find it.

Change your code to:

public class TableDemo extends JPanel {
    private JTable table;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM