Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
660 views
in Technique[技术] by (71.8m points)

swing - Java: input a matrix using GridLayout

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  • 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"));
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...