Java tutorial
//package com.java2s; /* * Copyright (C) 2015 Miquel Sas * * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with this program. If not, see * <http://www.gnu.org/licenses/>. */ import java.awt.Component; import java.awt.Container; import java.awt.event.KeyListener; import java.util.ArrayList; import java.util.List; import javax.swing.JDialog; import javax.swing.JFrame; public class Main { /** * Installs the key listener in the tree of components where the argument component is included, starting in the * first parent <i>JFrame</i> or <i>JDialog</i> parent, without removing previous key listeners. * * @param cmp The starting components in the tree. * @param keyListener The key listener to install. */ public static void installKeyListener(Component cmp, KeyListener keyListener) { installKeyListener(cmp, keyListener, false); } /** * Installs the key listener in the tree of components where the argument component is included, starting in the * first parent <i>JFrame</i> or <i>JDialog</i> parent. * * @param cmp The starting components in the tree. * @param keyListener The key listener to install. * @param removePrevious A boolean indicating whether previous key listeners should be removed. */ public static void installKeyListener(Component cmp, KeyListener keyListener, boolean removePrevious) { Component parent = getFirstParentFrameOrDialog(cmp); if (parent == null) { parent = cmp; } List<Component> components = getAllComponents(parent); for (Component component : components) { if (removePrevious) { removeKeyListeners(component); } component.addKeyListener(keyListener); } } /** * Return the first parent that is a <code>JFrame</code> or a <code>JDialog</code>. * * @return The parent frame or dialog. * @param cmp The start search component. */ public static Component getFirstParentFrameOrDialog(Component cmp) { while (cmp != null) { if (cmp instanceof JFrame || cmp instanceof JDialog) { return cmp; } cmp = cmp.getParent(); } return null; } /** * Returns the list of all components contained in a component and its subcomponents. * * @return The list of components. * @param parent The parent component. */ public static List<Component> getAllComponents(Component parent) { List<Component> list = new ArrayList<>(); fillComponentList(parent, list); return list; } /** * Returns the array of all components contained in a component and its subcomponents. * * @param parent The parent component. * @param clazz The class to filter components. * @return The array of components. */ public static List<Component> getAllComponents(Component parent, Class<?> clazz) { return getAllComponents(parent, new Class[] { clazz }); } /** * Returns the list of all components contained in a component and its subcomponents. * * @param parent The parent component. * @param classes an array of possible classes. * @return The list of components. */ public static List<Component> getAllComponents(Component parent, Class<?>[] classes) { List<Component> list = new ArrayList<>(); List<Component> components = getAllComponents(parent); for (Component component : components) { for (int j = 0; j < classes.length; j++) { if (classes[j].isInstance(component)) { list.add(component); } } } return list; } /** * Remove key listeners form the argument component. * * @param cmp The component. */ public static void removeKeyListeners(Component cmp) { KeyListener[] listeners = cmp.getKeyListeners(); if (listeners != null) { for (KeyListener listener : listeners) { cmp.removeKeyListener(listener); } } } /** * Fills the array list with the all the components contained in the parent component and its sub-components. * * @param list An <code>ArrayList</code>. * @param cmp The parent component. */ public static void fillComponentList(Component cmp, List<Component> list) { list.add(cmp); if (cmp instanceof Container) { Container cnt = (Container) cmp; for (int i = 0; i < cnt.getComponentCount(); i++) { fillComponentList(cnt.getComponent(i), list); } } } /** * Returns the component with the given name contained in the top component, or null if it does not contain a * component with that name. * * @param topComponent The top component. * @param name The name of the component to search. * @return The component with the name or null. */ public static Component getComponent(Component topComponent, String name) { List<Component> components = getAllComponents(topComponent); for (Component component : components) { if (component.getName() != null && component.getName().equals(name)) { return component; } } return null; } }