0

I'm trying to show a 2D matrix when fill two text fields (rows and columns) but I don't know how to print the 2D matrix when this two text field are filled and the "Draw" button is pushed.

This is my code:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

import java.io.*;

public class InputOutput extends JFrame{

JTextField mtf;
JTextField ntf;
JButton button;

public InputOutput(){

    setLayout (new FlowLayout ());



    mtf = new JTextField(10);
    ntf = new JTextField(10);
    add(mtf);
    add(ntf);

    button = new JButton("Dibujar");
    add(button);

    event e = new event();

    button.addActionListener(e);

}

public class Cuadricula extends JComponent{

    final int height = 30;
    final int width = 30;
    int posicionx=0;
    int posiciony=0;


     public Cuadricula(int x,int y){             
         setBounds(x,y,width+1, height+1);  

     }

@Override
    public void paintComponent (Graphics g){       

        super.paintComponent(g);           
        g.drawRect(0,0,width,height); 
    }
}



public class event implements ActionListener{

    public void actionPerformed(ActionEvent e){

        String Sm = mtf.getText();
        String Sn = ntf.getText();

        int m = Integer.parseInt(Sm);
        int n = Integer.parseInt(Sn);

        Cuadricula casillas[] = new Cuadricula[m*n];

        int i,j;
        int k = 0;

        while (k < m*n){
            for(i=0; i < m; i++){
                for(j=0; j < n; j++){

                    casillas[k] = new Cuadricula(i*30,j*30);
                    k++;
                }
            } 
        }


        //HERE IS WHAT I DON'T KNOW HOW TO DO

        for(int i = 0; i < 16; i++){

            gui.getContentPane().add(casillas[i]);
        }        
    }
}

public static void main(String[] args){

    InputOutput gui = new InputOutput();
    gui.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    gui.setSize(300,150);
    gui.setTitle("Writer");
    gui.setVisible(true);



    gui.getContentPane().setLayout(null);

}

}
  • I'm assuming the matrix is suppose to be drawn be the `Cuadricula` class? – MadProgrammer Nov 12 '14 at 23:44
  • 1
    Avoid using `null` layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify. Also have a look at [Why is it frowned upon to use a null layout in SWING?](http://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing) for more details – MadProgrammer Nov 12 '14 at 23:44
  • Thanks for reply MadProgrammer. I don't know how to draw the matrix inside Cuadricula class, because Cuadricula class don't know the "gui" element, that's the problem... – Gonzalo García Nov 13 '14 at 00:29
  • Yes, but that's where the output is suppose to go (if you could get it to work) yes? – MadProgrammer Nov 13 '14 at 00:31

0 Answers0