Search code examples
javaswinggridjbuttonchess

Grid of JButtons Label


grid=new JButton[width][length];
for(y=0;y<length;y++){
    for(x=0;x<width;x++){
        final Border border=new LineBorder(Color.red, 5);
            grid[x][y]=new JButton(" ");
            grid[x][y].addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                ((JButton)e.getSource()).setBorder(border);;
                System.out.println("Where do you want to move this piece");
            }
        });
        frame.add(grid[x][y]);

    }
}
grid[1][1]='R';

I am trying to get the first spot of the grid to say the letter R as in Rook; I am not sure how I can label specific JButton in a grid. Please help me... I am trying to make a GUI chess game.


Solution

  • Use methods instead of = to set component properties in Swing

    grid[0][0].setText("R");