Java Add Action To All JButtons

How To Add Action To All Buttons In A JPanel Using Java

add action to all buttons in java



In this Java Tutorial we will see How To Create A Function To Set An ActionListener To All The JButtons Inside A JFrame Using JPanel In Java Programming Language NetBeans IDE .




Project Source Code:

public void addAction()
    {
        Component[] components = jPanel1.getComponents();
        for(Component component : components)
        {
            if(component instanceof JButton)
            {
                JButton button = (JButton) component;
                button.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println(button.getText());
                    }
                });
            }
        }
        
    }                                         



////// OUTPUT : 

add ActionListener to all jbuttons in java



Share this

Related Posts

Previous
Next Post »