How To Add Action To All Buttons In A JPanel Using 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 : 
 
 
 
 
 
 
 
 

 

 
 
Post A Comment:
0 comments: