1

I am trying to write a function that can input a matrix of any size using a GridLayout, but I'm stuck since I can't find an appropriate way of extracting the JTextField values to fill the 'mat' var (see FIXME below).

    /**
     * @mat: matrix declared in main (e.g: mat[][] = new int[3][3];)
     * @rows: number of matrix rows (e.g: int rows = 3;)
     * @columns: number of matrix columns (e.g: int columns = 3;)
     */
    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++){
                        /* FIXME: find how to extract JTextField values. */
                        mat[b][c] = JTextField.a.getText();
                    }
                }
            }
        }

        return mat;
    }

Thanks in advance for your help!

2 Answers2

3
  • use JTable instead of bunch of JTextField layed by GridLayout

or

  • add there putClientProperty and to add identifier Row a Column from GridLayout

  • put JTextField to the HashMap

  • I would be preferring putClientProperty (you can to multiplay number or additional infos.., number of separate putClientProperty isn't somehow reduced)

  • depends of (not clear) desing, you can to add ActionListener to JTextField (accelerator is ENTER key) or DocumentListener

virtual example, code example for JButton and ActionListener, putClientProperty is accesible from all methods or Listeners added to JTextField

in the loop

buttons[i][j].putClientProperty("column", i);
buttons[i][j].putClientProperty("row", j);
buttons[i][j].addActionListener(new MyActionListener());

and get from ActionListener (for example)

public class MyActionListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton btn = (JButton) e.getSource();
        System.out.println("clicked column " + btn.getClientProperty("column")
                + ", row " + btn.getClientProperty("row"));
}
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Hi, the `JTable` way looks good, could you detail it a bit more ? I will look into the other one too. ty –  Nov 23 '12 at 19:31
  • 1
    @user1847810 Start from [this link](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html). All Swing components have a corresponding tutorial at Oracle/Sun which are almost all a very good start point. – Guillaume Polet Nov 23 '12 at 19:34
1

Let's say you have a 3-row x 3-column grid. Since GridLayout adds by rows, then the 1st item of the second row would be the 4th item that you added to the grid. You could retrieve this item by calling panel.getComponent(3) (zero index so 4th item is at index 3).

So - you could just use getComponent, doing a little math to figure out the right index based on the number of columns and the i,j coordinates in the matrix.

Guido Simone
  • 7,912
  • 2
  • 19
  • 21
  • Hi, I tried `mat[b][c] = Integer.parseInt(panel.getComponent(a));` but it says "Cannot convert from component to int." Am I missing something here ? ty –  Nov 23 '12 at 19:29
  • @user1847810 the component itself is not an `int` (but probably a `JTextField`). You could try this instead: `mat[b][c] = Integer.parseInt(((JTextField)panel.getComponent(a)).getText());`. But my advice would be to follow mKorbel solution and use a `JTable` which is actually meant for your purposes. – Guillaume Polet Nov 23 '12 at 19:32
  • @GuillaumePolet That works for me, but yeah looks very dirty. –  Nov 23 '12 at 19:41