1

What i want to accomplish is:

*A window containing a matrix of buttons. Let's say 10x10.

*The buttons should say either "1" or "0", and change when I click them.

*The values of the buttons (1 or 0) should be stored in a String[][] matrix.

At the moment I have a String[][] 2D-array containing values. I can show it in a window with clickable buttons using the following code:

//dim = 10
//matrix is the 10x10 String[][] matrix containing 1s or 0s

private static void convertMatrixToGUI() {
    JFrame f = new JFrame("Window containing a matrix");
    JPanel p = new JPanel();
    p.setLayout(new GridLayout(dim, dim));

    for(int r = 0; r < dim; r++){
        for(int c = 0; c < dim; c++){
            p.add(new JButton(matrix[r][c]));
        }
    }
    f.add(p);
    f.pack();
    f.setVisible(true);
}

The next step is to change the values in the matrix when clicking the buttons. If i click a 0 it should change it to a 1 and vice versa. The values have to be stored in the String[][] at all times.

How do i change things in the string matrix by clicking a button in the graphical matrix? If i click the button at position [5][2], how should the program know that I want to change the string matrix as position [5][2]?

Best regards Goatcat

Goatcat
  • 1,133
  • 2
  • 14
  • 31

5 Answers5

4

GridButtonPanel illustrates the basic principle. Substitute JToggleButton to get the effect of a binary selected/not-selected state.

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
2

what you need is an ActionListener.

this Tutorial give you an idea how to use it.

http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

Philipp Sander
  • 10,139
  • 6
  • 45
  • 78
2

I give you an example of a JButton extension called ChangingButton that has matrix position and matrix assigned to it. Also, it creates an ActionListener that will change the name on the click.

public static class ChangingButton extends JButton {

    private final int[][] fModel;
    private final int fX;
    private final int fY;

    public ChangingButton(final int x, final int y, final int[][] model) {
        fX= x;
        fY= y;
        fModel= model;

        addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                fModel[fX][fY] = fModel[fX][fY] == 1 ? 0 : 1;
                updateNameFromModel();
            }
        });
        updateNameFromModel();
    }

    private void updateNameFromModel() {
        setText(String.valueOf(fModel[fX][fY]));
    }

}

This is your main test class.

public static void main(String[] args) {

    int dim=10;
    int matrix[][] = new int[10][10];

    JFrame f = new JFrame("Window containing a matrix");
    JPanel p = new JPanel();
    p.setLayout(new GridLayout(dim, dim));

    for(int r = 0; r < dim; r++){
        for(int c = 0; c < dim; c++){
            ChangingButton button= new ChangingButton(r, c, matrix);
            p.add(button);
        }
    }
    f.add(p);
    f.pack();
    f.setVisible(true);

}

Hope it helps. If you don't understand something, please ask.

darijan
  • 9,725
  • 25
  • 38
2

Try it

        int DIM = 10;
    String [][]matrix = new String[DIM][DIM];
    JButton [][]butt = new JButton[DIM][DIM];

    JFrame f = new JFrame("Window containing a matrix");
    JPanel p = new JPanel();

    for(int r=0; r<DIM; r++){
        for(int c=0; c<DIM; c++){
            butt[r][c] = new JButton(matrix[r][c]);
            p.add(butt[r][c]);
            butt[r][c].addActionListener(this); //if your class extends ActionListener
        }
    }

    f.add(p);
    f.pack();
    f.setVisible(true);

and override the actionPerformed method. Implementing your code :)

@Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub

    }
giovybus
  • 349
  • 1
  • 3
  • 10
1
 for(int r = 0; r < dim; r++) {
       for(int c = 0; c < dim; c++){
           JButton temp = new JButton(matrix[r][c])
           temp.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                matrix[r][c] = (matrix[r][c].equals("0")) ? "1" : "0"; //or for changing text of the button this.setText("1") or "0"
              }
           })
          p.add(temp);
       }
   }
lummycoder
  • 588
  • 1
  • 7
  • 20