Search code examples
javaarraysswingjbuttonactionlistener

how do i modify the params of an action listener attached to several JButtons?


I have a assignment that i had to create and array of buttons for a paint app project and everything is done but i am stuck on assigning/referencing/modifying my action listener.

The way it works(supposedly) is that i have a loop that creates, assigns a name, and adds to the action listener an Array of JButtons named myShapes. From this, I am supposed to have the actionlistener named listener(); give a int 1-6 to each jbutton, setting the current actionPerformed state.

int actionNum = 1;

ActionListener listener = new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        currentAction = actionNum;
    }
};  


//........................................................................

String[] myShapesName = {"brushBut", "lineBut", "ellipseBut", "rectBut", "strokeBut", "fillBut"};
//String[] myLisName = {"brushButL", "lineButL", "ellipseButL", "rectButL", "strokeButL", "fillButL"};

JButton[][] myShapes = new JButton[3][2];
for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 2; j++) {
        myShapes[i][j] = new JButton();
        myShapes[i][j].setBounds(0, 0, 50, 50);//(100, 200);
        myShapes[i][j].setName(myShapesName[i+j]);
        myShapes[i][j].setBackground(null);
        myShapes[i][j].putClientProperty("id",

        String.valueOf(myShapesName[i+j]).concat(String.valueOf(j)));
        MyShapesBox.add(myShapes[i][j]);
        System.out.println(listener);
    }
}

the problem is that i dont know how to change the actionlistener so that it gives all 6 Jbuttons a different actionPerformed state because each state represents a different brush. I tried several ways of like trying to make a Action list array and renaming them but that gives an error or breaks to loop which is needed for the assignment and ended with this final attempt. unfortunately, this currently is beyond me. If been on this for 2 days and Im stumped.


Solution

  • My first thought would be to create an action class, which took a int value as part of it's constructor, for simplicity sake, I'd make it a inner class, but you could use an outer class, but you'd need to pass more details to it, for example...

    public class TestPane extends JPanel {
    
        private int currenAction;
    
        public TestPane() {
            String[] myShapesName = {"brushBut", "lineBut", "ellipseBut", "rectBut", "strokeBut", "fillBut"};
            //String[] myLisName = {"brushButL", "lineButL", "ellipseButL", "rectButL", "strokeButL", "fillButL"};
    
            setLayout(new GridLayout(3, 2));
            JButton[][] myShapes = new JButton[3][2];
            int action = 0;
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 2; j++) {
                    myShapes[i][j] = new JButton();
                    myShapes[i][j].setBounds(0, 0, 50, 50);//(100, 200);
                    myShapes[i][j].setName(myShapesName[i + j]);
                    myShapes[i][j].setBackground(null);
                    myShapes[i][j].putClientProperty("id",
                            String.valueOf(myShapesName[i + j]).concat(String.valueOf(j)));
    
                    myShapes[i][j].addActionListener(new ButtonAction(action));
                    action++;
                    add(myShapes[i][j]);
                }
            }
        }
    
        public class ButtonAction implements ActionListener {
    
            private int actionNum;
    
            public ButtonAction(int actionNum) {
                this.actionNum = actionNum;
            }
    
            @Override
            public void actionPerformed(ActionEvent arg0) {
                currenAction = actionNum;
                System.out.println(actionNum);
            }
    
        }
    
    }