Matrix into JTable

前端 未结 1 1653
-上瘾入骨i
-上瘾入骨i 2021-01-26 01:39

I need to fill a JTable with a static 2D array. I\'ve created this model for the JTable:

 public class InsertMatToJTable extends AbstractTableModel{         


        
相关标签:
1条回答
  • 2021-01-26 02:02

    The problem is that you have written:

    public void InsertMatToJTable()
    

    but you should have written:

    public InsertMatToJTable()
    

    Notice that there is no void in the second snippet.

    You have declared a method called InsertMatToJTable and not the constructor of the class with the same name. Therefore, when you invoke new InsertMatToJTable() you invoke the default no-args constructor and your code is never run, leaving your matrix remains uninitalized, hence the NullPointerException.

    To avoid these kind of typo issues, add logs to your code and use a debugger to find problems.

    Here is an example demo of a working code.

    import java.util.Random;
    
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.SwingUtilities;
    import javax.swing.table.AbstractTableModel;
    
    public class TestTables {
    
        private static final int ROWS = 270;
        private static final String titre[] = { "age real", "sex real", "chest real", "resting_blood_pressure real", "serum_cholestoral real",
                "fasting_blood_sugar real", "resting_electrocardiographic_results real", "maximum_heart_rate_achieved real",
                "exercise_induced_angina real", "oldpeak real", "slope real", "number_of_major_vessels real", "thal real", "class" };
    
        public static class InsertMatToJTable extends AbstractTableModel {
    
            private float[][] matrice_normalise;
    
            public InsertMatToJTable(float[][] matrice_normalise) {
                this.matrice_normalise = matrice_normalise;
            }
    
            @Override
            public int getRowCount() {
                return matrice_normalise.length;
            }
    
            @Override
            public int getColumnCount() {
                return titre.length;
            }
    
            @Override
            public Object getValueAt(int rowIndex, int columnIndex) {
                return matrice_normalise[rowIndex][columnIndex];
            }
    
            @Override
            public String getColumnName(int columnIndex) {
                return titre[columnIndex];
    
            }
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    JFrame frame = new JFrame(TestTables.class.getSimpleName());
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    float[][] matrix = new float[ROWS][titre.length];
                    Random random = new Random();
                    for (int i = 0; i < matrix.length; i++) {
                        for (int j = 0; j < matrix[i].length; j++) {
                            matrix[i][j] = random.nextFloat() * 100;
                        }
                    }
                    InsertMatToJTable model = new InsertMatToJTable(matrix);
                    JTable table = new JTable(model);
                    JScrollPane scroll = new JScrollPane(table);
                    frame.add(scroll);
                    frame.pack();
                    frame.setVisible(true);
                }
            });
        }
    }
    
    0 讨论(0)
提交回复
热议问题