简体   繁体   中英

Changing the components inside a JPanel with GridLayout

This is the following code.

import javax.swing.*;

import java.awt.Color;
import java.awt.GridLayout;
import java.util.ArrayList;

public class Grid extends JPanel {

    private static final long serialVersionUID = 1L;

    private ArrayList<Cell> cells;
    private int width = 20;
    private int height = 20;

    public Grid() {
        cells = new ArrayList<Cell>();
    }

    public void drawGrid() {
        this.setLayout(new GridLayout(width, height, 5, 5));
        this.setBackground(Color.RED);

        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                Cell cell = new Cell(i, j);
                cells.add(cell);
            }
        }
        for (Cell c : cells) {
            this.add(c);
        }

    }

    public ArrayList<Cell> getCells() {
        return cells;
    }

    public void setCells(ArrayList<Cell> cells) {
        this.cells = cells;
    }

    public void changeCell(Cell c) {
        for (Cell cell : cells) {
            if (cell.getx() == c.getx() && cell.gety() == c.gety()) {
                cell = c;

                /*
                System.out.println(c.getx() + " " + c.gety()
                                    + c.getBackground().toString());
                                    */

            }
        }
    }

The problem in this code is found in the changeCell(Cell c) , where I would like to add this new cell to the JPanel cells. Currently the line of code that adds the cells in the grid is the enhanced for loop found beneath the for each loop to draw a grid ( this.add(c) ).

I have trouble to add/update this passed parameter found in the changeCell(Cell c) method to the cells found in this current JPannel. All I need it to do is, update the ArrayList so that the Cells on the JPanel correspond to the cells found in the ArrayList.

You're changing your model ( ArrayList<Cell> ) but no change is done for its view (your Grid JPanel ). Try something like this:

public void changeCell(Cell c) {
    this.removeAll(); //erase everything from your JPanel
    this.revalidate; this.repaint();//I always do these steps after I modify my JPanel
    for (Cell cell : cells) {
        if (cell.getx() == c.getx() && cell.gety() == c.gety()) {
              this.add(c);
        else this.add(cell);
    }
}

In a nutshell, remove everythings from your panel and add again your cells, but when you find the cell to be changed, add that instead of the other.

Or, better, you should use Model-View-Controller pattern , where your model is your ArrayList, your view is your Grid JPanel and your Controller is the JApplet\\JFrame or something else that creates the view and the model.

Let me know. Bye!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM