Device
Device

Reputation: 567

JAVA miglayout way to set and get text to/from cell

I'm creating a Pac-man game in which I would like to use miglayout with a text file to set/get text out of cell. I have map (32x32) let's say. And I want to fill every cell with W (ok that is not a problem, I can create a function which every time is called creates new JLable and add it to the cell).

FUNCTION:

public void function(String rowColumn){
    JLabel lblH = new JLabel("W");
    contentPane.add(lblH, "cell " + rowColumn);
}

But then I can't read it.

Question: How do I read and write text in the cell?

EDIT: Or, is there any better layout which has cells and can set/get text from/to a cell?

Upvotes: 0

Views: 176

Answers (3)

Device
Device

Reputation: 567

The answer is this:

public static void start(){
    for(int a = 0; a <= 22; a++){
        for(int b = 0; b <= 37; b++){
            printToCell(String.valueOf(b), String.valueOf(a), "H");
        }
    }
}


public static void clearContentPane(){
    engine_design.Engine.contentPane.removeAll();
    engine_design.Engine.contentPane.setLayout(new MigLayout("", "", ""));
}


public static void printToCell(String column, String row, String letter){
    JLabel lblH = new JLabel(letter);
    lblH.setFont(new Font(lblH.getName(), Font.BOLD, 15));
    engine_design.Engine.contentPane.add(lblH, "cell " + column + " " + row);

}

And for the reading I will use list, how someones mentioned before.

Upvotes: 0

Branislav Lazic
Branislav Lazic

Reputation: 14826

As I mentioned in my comment. There is no need to use multiple JComponents and MigLayout. You can just use graphics and draw cells as shown in this demo:

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


public class GraphicsDemo {


    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                World world = new World();
                JFrame frame = new JFrame("Demo");
                frame.add(world);
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                frame.pack();
                // frame.setResizable(false);
                frame.setVisible(true);
            }
        });

    }

    static class World extends JPanel {

        public static final int SQUARE_SIZE = 48;
        public static final int CELL_SIZE = 50;

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.BLUE);
            for (int j = 0; j < getPreferredSize().getHeight() / CELL_SIZE; j++) {
                for (int i = 0; i < getPreferredSize().getWidth() / CELL_SIZE; i++) {
                    g.fillRect(i * CELL_SIZE, j * CELL_SIZE, SQUARE_SIZE, SQUARE_SIZE);
                }
            }

        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(350, 250);
        }
    }
}

Result:

enter image description here

Upvotes: 0

Dragan Bozanovic
Dragan Bozanovic

Reputation: 23562

Layout managers are meant for laying out components.

You should keep references to components which you intend to use after you lay them out. In your case it should be probably a matrix-style data structure in which you store references to labels like JLabel[][] or List<List<JLabel>> or whatever suits your use cases best.

Then, provided x and y coordinates, just write/read the text to/from the desired label:

cells[x][y].getText() // Array

or

cells.get(x).get(y).getText() // List

or

... // Your data structure

Upvotes: 0

Related Questions