I am trying to write a function that can input a matrix of any size using GridLayout, but I am stuck as I cannot find a suitable way to retrieve JTextField values to populate var 'mat' (see FIXME below).
public static int[][] inputMatrix(int[][] mat, int rows, int columns)
{
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(rows,columns));
for (int a=0; a<(rows*columns); a++)
{
panel.add(new JTextField(a));
}
if (JOptionPane.showConfirmDialog(null, panel, "Enter the matrix", JOptionPane.OK_CANCEL_OPTION)
== JOptionPane.OK_OPTION)
{
for(int a=0; a<(rows*columns); a++){
for(int b=0; b<rows; b++){
for(int c=0; c<columns; c++){
mat[b][c] = JTextField.a.getText();
}
}
}
}
return mat;
}
Thanks in advance for your help!
source
share