×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Dread Morpheus
Added: Aug 14, 2017 12:34 AM
Modified: Sep 7, 2017 7:16 PM
Views: 10
I had to set a lot of buttons. Get the panel they're in and use that getComponents method.
  1. /**
  2.      * Listeners.
  3.      */
  4.     private void setListeneres() {
  5.         // Add listeners to components in a specific panel.
  6.         for (Component comp : jpSecond.getComponents()) {
  7.             if (comp instanceof JButton) {
  8.                 ((JButton) comp).addActionListener(new ActionListeners());
  9.                 ((JButton) comp).addKeyListener(new KeyListeners());
  10.             }
  11.         }
  12.  
  13.     }