user1435266
user1435266

Reputation:

SWT Button Grid

It's a problem that it's annoying me for 3 days now. I have to rewrite the UI of a little tictactoe(Gomoku of n x n) game. the problem is that when i created the swing GUI , i made a new class that inherits JButton properties and added an int for rows and an int for columns. I cannot do that with SWT(no inheritance). is there a way for me to add the values of i and j to the button.

Here is the example in Swing:

for (int i = 0; i < rows; i++) {
    for (int j = 0; j < cols; j++) {
        final MyJButton button = new MyJButton(i, j);
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                MoveResult move = game.move(button.getRow(), button.getCol());
                    switch (move) {
                        case ValidMove:
                            button.setBackground(game.getCurrentPlayer().getColor());
                            game.changePlayer();
                            break;
                    }
                }
            }
        }
    }
}

I give the i and j for the game class which give it to a table clas to check the move.

if (table.getElement(x, y) != PieceType.NONE) return MoveResult.InvalidMove;
private PieceType[][] table;

is there a way to do the same in SWT, any indication is welcomed .

this is what i made

buttonpanel = new Composite(shell, SWT.NONE);
buttonpanel.setLayout(new org.eclipse.swt.layout.GridLayout(cols, true));
buttonTable = new Button[rows][cols];

for (int i = 0; i < rows; ++i){
    for (int j = 0; j < cols; ++j) {
        gridData.heightHint = 45;
        gridData.widthHint = 45;

        Button button = new Button(buttonpanel, SWT.PUSH);
        button.setLayoutData(gridData);
        buttonTable[i][j] = button;
        buttonTable[i][j].addSelectionListener(new buttSelectionListener());    
        // buttonpanel.pack();
    }
}

Upvotes: 1

Views: 332

Answers (2)

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382454

I see two solutions :

  • use Button's setData method (defined in the Widget superclass) to associate an object containing your x and y (you'll found those data in the event object provided to your listener)
  • use different listeners for each button

In your case, the first solution seems the most natural one. This means creating a class holding x and y (let's call it Cell), and doing

button.setData(new Cell(i, j));

and in you listener using

game.move(e.data.x, e.data.y);

Upvotes: 2

Andy Thomas
Andy Thomas

Reputation: 86509

Options include:

  • Subclass Button, and override its checkSubclass() method to indicate that you're taking responsibility for not subclassing harmfully.
  • Make each Button a Composite, which allows subclassing, and put a Button in the Composite.
  • Make a separate listener for each button.
  • In a single listener, search through the buttonTable for the button that calls the listener.

Upvotes: 0

Related Questions