The ButtonGroup class manages an array of Button objects, only 1 button can be selected at any time. The variable: activeButton provides the index of the currently selected Button object.
Example Usage
//main tab //create buttons by specifying x, y, w, h and 2 colors for each Button.Button[] buttons =newButton[3]; //declare button array buttons[0] =newButton( 10,10,100,100, color1, color2,"P1"); buttons[1] =newButton( 10,120,100,100, color1, color2,"P2");ButtonGroup buttonGroup =newButtonGroup( buttons);}buttonGroup.display(); //displays each buttonbuttonGroup.clicked( mouseX, mouseY); //manages button selection logic
ButtonGroup Class Definition
//add commentsclassButtonGroup{//PROPERTIESButton[] buttons;int activeBtnIndex;//CONSTRUCTORS//add commentsButtonGroup(Button[] buttons){this.buttons= buttons; activeBtnIndex =0; //start with no button selected }//METHODS//add commentsvoiddisplay(){for( int i=0; i<buttons.length; i++){ buttons[i].display(); } }//add commentsbooleanclicked(int mx,int my){boolean isChanged =false; //has a new button been selectedfor( int i=0; i<buttons.length; i++){if( buttons[i].selected==false){ //if buttons[i] was not previously selected buttons[i].clicked(mx, my); //check to see if it's been clickedif( buttons[i].selected==true){ // if it's now selected isChanged =true; activeBtnIndex = i;for( int j=0; j<buttons.length; j++){ //for all other buttonsif( i != j){ buttons[j].reset() ; //turn the buttons off } } } } } return isChanged; }//end clicked method } // end class ButtonGroup