I have a lot of buttons in the project (JButton). For each button I get a separate object. Next - I want to put on all buttons setFocusable (false). I have to add 50 extra lines of code (Maybe there is a simpler way? I tried using UIManager.put (), but did not find the necessary key

    1 answer 1

    ArrayList<JButton> list = new ArrayList<>(); for (Component component : frame.getComponents()) // frame - главный фрейм { getAllBtns(component, list); } list.forEach(btn -> btn.setFocusable(false)); 

    and here is the getAllBtns method itself

     private void getAllBtns(Component component, ArrayList<JButton> list) { if (component instanceof JButton) { list.add((JButton) component); } if (component instanceof Container) { for (Component c : ((Container) component).getComponents()) { getAllBtns(c, list); } } } 

    Essence - you go through all the components in the frame (recursively), add to arrayList , then take each element from this sheet and make setFocusable(false)