Question

Similar questions to my own have been asked, but I'm at a bit of a loss as to how to proceed. I really have a poor grasp of some of the more subtle nuances of java, so I apologize if anything isn't clear.

Say for example I wanted to compare one JButton within a 2D array with another. To be more specific, all of these JButton's would be stores within a 2D array and displayed in grid format. All of the buttons would have the same action listener that, upon the button being pressed, calls the setselected() method.

How would I go about comparing one of these selected JButton's with another selected JButton within the same array? And upon doing so, how could I swap the positions or more specifically, the icons of said buttons.

Below, I've included some example code and my own attempt on the subject. I understand that I can use .getSource() to grab a JButton object itself, but would this not only allow me to capture 1 selected button at a time. This is all considering the use of the same actionlistner code for each button, but a secular listener for each button.

The code below sets every icon to 1 of 7 randomly generated image icons. A frame is generated within secular main class. Upon being pressed or "selected" the image icons change to a selected iteration of the same image.

EDIT: Based on Ameer's suggestion, I've run into several nullpointer exceptions that are caused by my actionPerformed method. Is this as a result to my button array not being filled with button objects at this point, or am I simply presuming something within my code?

  public class SButtonGame extends JFrame implements ActionListener   {

    public static ImageIcon[] icons={

    new ImageIcon("img1.png"),
    new ImageIcon("img2.png"),
    new ImageIcon("img3.png"),
    new ImageIcon("img4.png"),

    new ImageIcon("img5.png"),

    new ImageIcon("img6.png"),
    new ImageIcon("img7.png"),

    };


    public static ImageIcon[] selectedIcons={

    new ImageIcon("simg1.png"),
    new ImageIcon("simg2.png"),
    new ImageIcon("simg3.png"),
    new ImageIcon("simg4.png"),

    new ImageIcon("simg5.png"),

    new ImageIcon("simg6.png"),
    new ImageIcon("simg7.png"),
    }; 





   int rowNum=0;
   int colNum=0;
   JButton[][] Buttons; 

   boolean swaptf=false;
   JButton CButton; // Selected button "holder". Doesn't accomplish anything I think it            should






    public SButtonGame(String title) {

    //Constructs frame
    super(title);
    getContentPane().setLayout(null)
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(578,634);









    int colLoc=10;
    int rowLoc=10;  
    this.colNum=0;
    this.rowNum=0;


    for(int r=0; r<8; r++)
    {

    this.Buttons= new JButton[9][9];

    this.rowNum++;

    for(int c=0; c<8; c++)
    {

    ActionListener listner = new ActionListener(){

    public void actionPerformed(ActionEvent e)
    {

    if(e.getSource() instanceof JButton)
    {




    ((JButton) e.getSource()).setSelected(true);

    CButton=(JButton)e.getSource();





    }

    }    


    };







    int ranImg;

    ranImg=0+(int)(Math.random()*7);
    int sranImg=ranImg;


    this.Buttons[this.colNum][this.rowNum]= new JButton(icons[ranImg]);
    this.Buttons[this.colNum][this.rowNum].setSelectedIcon(selectedIcons[sranImg]);
    this.Buttons[this.colNum][this.rowNum].addActionListener(listner);
    this.Buttons[this.colNum][this.rowNum].setSize(59,59);
    this.Buttons[this.colNum][this.rowNum].setLocation(rowLoc,colLoc);
    rowLoc=rowLoc+69;

    this.Buttons[this.colNum][this.rowNum].setVisible(true);

    this.Buttons[this.colNum]  [this.rowNum].setBorder(BorderFactory.createLineBorder(Color.black));
    add(this.Buttons[this.colNum][this.rowNum]);


    }
    this.colNum++;
    colLoc=colLoc+69;    
    rowLoc=10;
    } 




    JButton Newgame;
    Newgame= new JButton("NewGame");
    Newgame.setSize(100, 30);
    Newgame.setLocation(350, 560);
    Newgame.setVisible(true);
    add(Newgame);



    JButton Quit;
    Quit= new JButton("Quit");
    Quit.setSize(60, 30);
    Quit.setLocation(480, 560);
    Quit.setVisible(true);
    add(Quit);





    New.addActionListener(new ActionListener() 
    {
    //dispose of current frame and generates a new one;

    public void actionPerformed(ActionEvent e)
    {

    dispose();
    SButtonGame Frame;
    Frame = new SButtonGame("ShinyButtons");

    Frame.setVisible(true);  

    }


    });
    Quit.addActionListener(new ActionListener(){


    public void actionPerformed(ActionEvent e)

    {


    dispose();


    }





    });

    }


 @Override
            public void actionPerformed(ActionEvent ae){

                if(ae.getSource() instanceof JButton){

                    JButton sButton;
                   int rindex=0;
                   int cindex=0;

                    ((JButton) ae.getSource()).setSelected(true);

                   sButton=(JButton)ae.getSource();




                   if(SButtonGame.this.Buttons[(int)sButton.getClientProperty("rownum")][(int)sButton.getClientProperty("colnum")].isEnabled()==true){





                   }



                }



            }    







    public static void main(String[] args) 
    {

    SButtonGame Frame;
    Frame = new SButtonGame("ButtonsGame");

    Frame.setVisible(true);




    }

    }

No correct solution

OTHER TIPS

Inside actionPerformed(ActionEvent e) method, you can access the 2D array of buttons by using SButtonGame.this.Buttons (ideally variable name should be buttons starting with small b).

You can then compare the clicked button with the buttons from array and do rest of your stuff.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top