CSpadawan
CSpadawan

Reputation: 115

How do I change button labels in a grid?

I'm trying to make a game which involves moving pieces by dragging and dropping them from square to square. However, I'm trying to do so using buttons with labels (as an exercise). So, for instance, a button with the label "W" should change its label to "" (blank) when I press the mouse on it and release on a second valid button (one with another blank label). Then that second button should change its label from "" (blank) to "W".

Using graphics in Java is entirely new to me. Suffice it to say, I'm not sure how to accomplish the aforementioned task. Here's my code so far:

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

class Boardgame extends JFrame implements MouseListener {
    JFrame frame = new JFrame("Boardgame");
    JButton[][] bogrid;

public Boardgame ()
{
    frame.setLayout(new GridLayout(8,8));

    bogrid = new JButton[8][8];
    for (int i=0;i<8;i++)
    {
        for (int j=0;j<2;j++)
        {
            bogrid[j][i] = new JButton("B");
            frame.add(bogrid[j][i]);
        }
        for (int j=2;j<6;j++)
        {
            bogrid[j][i] = new JButton();
            frame.add(bogrid[j][i]);
        }
        for (int j=6;j<8;j++)
        {
            bogrid[j][i] = new JButton("W");
            frame.add(bogrid[j][i]);
        }
    }

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setSize(405, 450);
    frame.setVisible(true);
}

public void mouseClicked(MouseEvent e)
{
}

public void mouseReleased(MouseEvent e)
{
}

public void mouseEntered(MouseEvent e)
{
}

public void mouseExited(MouseEvent e)
{
}

public void mousePressed(MouseEvent e)
{
}

class MyAction implements ActionListener {
    Boardgame bo;

    MyAction(Boardgame b)
    {
        bo = b;
    }
}

}

Upvotes: 0

Views: 154

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

First off, you'll need to use a MouseListener to listen for mousePressed and mouseReleased actions. This means that you most definitely should not be using JButtons for this type of program but rather either JLabels, or a logical grid of images, since JButtons should respond to ActionListeners, not MouseListeners (with rare exceptions). I suggest using JLabels, since it will be easy for them to hold and set text and to give you the text they hold.

Next, you would add your MouseListener to your JLabels, and on mousePressed, get the text held by the pressed JLabel. The MouseEvent parameter's getSource() method will return to you the pressed (and released) JLabel.

So inside of your for loops, you will need to add something like:

bogrid[j][i] = new JLabel("B");
bogrid[j][i].addMouseListener(myMouseListener);
frame.add(bogrid[j][i]);

Where myMouseListener is your MouseListener object.


Edit
You ask in comment:

Initial question: using JLabel instead of JButton yields a window that appears to be a large empty field with a bunch of floating labels. How do I make it appear as a grid with lines demarcating the individual spaces?

Consider giving your JLabel a border, and consider giving the GridLayout some horizontal and vertical gaps.

For the layout use the GridLayout constructor that takes 4 int parameters, not 2, with the 3rd and 4th parameters being for the horizontal and vertical gaps:

int gap = 4; // or whatever number looks nice
frame.setLayout(new GridLayout(8, 8, gap, gap));

For the border, consider using a LineBorder that is added to the JLabel in the loop where you create it. If that Border crowds your text too much, you could use a CompoundBorder where the inner border is an EmptyBorder with suitable constants and the outer border is a LineBorder.

Upvotes: 3

Related Questions