Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Get Index Value of JButton Array

843807Jun 3 2010 — edited Jun 4 2010
Hey, I'm currently trying to make a minesweeper game for my final project in highschool, and I have an array of JButtons for the grid.The problem is, they all have the same value, and no text to identify them uniquely, and I just need to be able to get the index value of them when they are clicked. Any help would be awesome, please.

Here is the layout file for the game.
// The "Minesweeper" class.
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class Minesweeper extends JFrame
{
    MinesweeperData code = new MinesweeperData(this);
    
    JButton[] game = new JButton [225];
    int[] bombs = new int [225];
    JButton face = new JButton ();
    JPanel top = new JPanel ();
    GridLayout grid = new GridLayout (15, 15);
    FlowLayout flow = new FlowLayout ();
    JPanel buttons = new JPanel ();
    
    public Minesweeper ()
    {
        super ("Minesweeper");
        Color myColor = new Color(204,204,204);
        for (int x = 0 ; x < 225 ; x++)
        {
                bombs [x] = (int) Math.floor (Math.random () * 2 + 1);
        }
        ImageIcon blank = new ImageIcon ("Start.png");
        ImageIcon smile = new ImageIcon ("Playing.png");
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        setSize(1000,750);
        getContentPane ().setLayout (flow);
        face = new JButton (smile);
        top.add (face);
        getContentPane ().add (top);
        buttons.setLayout (grid);
        for (int x = 0 ; x <= 224 ; x++)
        {
                game [x] = new JButton(blank);
                buttons.add(game[x]);
            
        }
        getContentPane ().add (buttons);
        setVisible (true);
    }


    public static void main (String[] args)
    {
        Minesweeper frame = new Minesweeper ();
    } // main method
} // Minesweeper class
Here is all I have for the command file so far.
// The "MinesweeperData" class. By Thomas Coupland
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
public class MinesweeperData extends JFrame implements ActionListener
{
    Minesweeper gui;
    public MinesweeperData (Minesweeper in)
    {
        gui = in;
    }


    public void actionPerformed (ActionEvent event)
    {
    String command = event.getActionCommand ();
        
    }

} // MinesweeperData class
Any help is appreciated, thanks.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 2 2010
Added on Jun 3 2010
4 comments
1,122 views