Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.awt.Component;
import java.awt.Container;

import javax.swing.JMenu;

public class Main {
    /**
     * Attempts to locate the {@link Component} within the {@link Container}.
     * Returns {@code -1} if unable to locate in this {@link Container}. 
     * 
     * @param container
     * @param child
     * @return
     */
    public static int indexOf(Container container, Component child) {
        final int count = container instanceof JMenu ? ((JMenu) container).getMenuComponentCount()
                : container.getComponentCount();

        for (int i = 0; i < count; i++) {
            if (container instanceof JMenu) {
                if (((JMenu) container).getMenuComponent(i).equals(child)) {
                    return i;
                }
            } else if (container.getComponent(i).equals(child)) {
                return i;
            }
        }
        return -1;
    }
}