Home > OS >  How can I fill an object matrix
How can I fill an object matrix

Time:03-24

I have an object matrix, and I have to fill it with string. How can I do this? Let me explain well, I have declared a matrix of objects of fixed size, now I have to fill it through for loops, how can I access the cells of the matrix. I put the code of matrix down. Sorry but is in Italian. Giorno = day, ore = Hour

tabMedie = new JTable();
tabMedie.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
tabMedie.setFont(new Font("Times New Roman", Font.PLAIN, 10));
tabMedie.setModel(new DefaultTableModel(
    new Object[][] {
        {"Orari", "Giorno 1", "Giorno 2", "Giorno 3", "Giorno 4", "Giorno 5", "Giorno 6", "Giorno 7"},
        {"1", "", "", "", "", "", "", ""},
        {"2", "", "", "", "", "", "", ""},
        {"3", "", "", "", "", "", "", ""},
        {"4", "", "", "", "", "", "", ""},
        {"5", "", "", "", "", "", "", ""},
        {"6", "", "", "", "", "", "", ""},
        {"7", "", "", "", "", "", "", ""},
        {"8", "", "", "", "", "", "", ""},
        {"9", "", "", "", "", "", "", ""},
        {"10", "", "", "", "", "", "", ""},
        {"11", "", "", "", "", "", "", ""},
        {"12", "", "", "", "", "", "", ""},
        {"13", "", "", "", "", "", "", ""},
        {"14", "", "", "", "", "", "", ""},
        {"15", "", "", "", "", "", "", ""},
        {"16", "", "", "", "", "", "", ""},
        {"17", "", "", "", "", "", "", ""},
        {"18", "", "", "", "", "", "", ""},
        {"19", "", "", "", "", "", "", ""},
        {"20", "", "", "", "", "", "", ""},
        {"21", "", "", "", "", "", "", ""},
        {"22", "", "", "", "", "", "", ""},
        {"23", "", "", "", "", "", "", ""},
        {"24", "", "", "", "", "", "", ""},
    },
    new String[] {
        "Ore", "Giorno 1", "Giorno 2", "Giorno 3", "Giorno 4", "Giorno 5", "Giorno 6", "Giorno 7"
    }
) {
    Class[] columnTypes = new Class[] {
        String.class, String.class, String.class, String.class, String.class, String.class, String.class, String.class
    };
    public Class getColumnClass(int columnIndex) {
        return columnTypes[columnIndex];
    }
    boolean[] columnEditables = new boolean[] {
        true, false, false, false, true, true, true, true
    };
    public boolean isCellEditable(int row, int column) {
        return columnEditables[column];
    }
});
tabMedie.getColumnModel().getColumn(0).setResizable(false);
tabMedie.getColumnModel().getColumn(1).setResizable(false);
tabMedie.getColumnModel().getColumn(2).setResizable(false);
tabMedie.getColumnModel().getColumn(3).setResizable(false);
tabMedie.getColumnModel().getColumn(4).setResizable(false);
tabMedie.getColumnModel().getColumn(5).setResizable(false);
tabMedie.getColumnModel().getColumn(6).setResizable(false);
tabMedie.getColumnModel().getColumn(7).setResizable(false);
tabMedie.setBounds(503, 36, 662, 401);
frmTemperature.getContentPane().add(tabMedie);

CodePudding user response:

First, store the “matrix” (actually a two-dimensional array) in a variable:

String[][] texts = { { "Ora", "Giorno 1", … }, … };

And then, you can use it in a for loop:

for (int x = 1; x < 8; x  ) {
    for (int y = 1; y < texts.length; y  ) {
        texts[y][x] = "Your Text Here";
    }
}

And finally, you can pass it to the method as an argument:

new DefaultTableModel(texts, new String[] { … }) { … };
  • Related